Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * socket_client.c - API message handling over sockets, client code. |
| 4 | * |
| 5 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 21 | #define __USE_GNU |
| 22 | #include <sys/socket.h> |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 23 | |
Florin Coras | 4d9b9d8 | 2018-01-14 12:25:50 -0800 | [diff] [blame] | 24 | #include <svm/ssvm.h> |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 25 | #include <vlibmemory/socket_client.h> |
| 26 | #include <vlibmemory/memory_client.h> |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 27 | |
| 28 | #include <vlibmemory/vl_memory_msg_enum.h> |
| 29 | |
| 30 | #define vl_typedefs /* define message structures */ |
| 31 | #include <vlibmemory/vl_memory_api_h.h> |
| 32 | #undef vl_typedefs |
| 33 | |
| 34 | #define vl_endianfun /* define message structures */ |
| 35 | #include <vlibmemory/vl_memory_api_h.h> |
| 36 | #undef vl_endianfun |
| 37 | |
| 38 | /* instantiate all the print functions we know about */ |
| 39 | #define vl_print(handle, ...) clib_warning (__VA_ARGS__) |
| 40 | #define vl_printfun |
| 41 | #include <vlibmemory/vl_memory_api_h.h> |
| 42 | #undef vl_printfun |
| 43 | |
| 44 | socket_client_main_t socket_client_main; |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 45 | __thread socket_client_main_t *socket_client_ctx = &socket_client_main; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 46 | |
| 47 | /* Debug aid */ |
| 48 | u32 vl (void *p) __attribute__ ((weak)); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 49 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 50 | u32 |
| 51 | vl (void *p) |
| 52 | { |
| 53 | return vec_len (p); |
| 54 | } |
| 55 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 56 | static socket_client_main_t * |
| 57 | vl_socket_client_ctx_push (socket_client_main_t * ctx) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 58 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 59 | socket_client_main_t *old = socket_client_ctx; |
| 60 | socket_client_ctx = ctx; |
| 61 | return old; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | vl_socket_client_ctx_pop (socket_client_main_t * old_ctx) |
| 66 | { |
| 67 | socket_client_ctx = old_ctx; |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | vl_socket_client_read_internal (socket_client_main_t * scm, int wait) |
| 72 | { |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 73 | u32 data_len = 0, msg_size; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 74 | int n, current_rx_index; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 75 | msgbuf_t *mbp = 0; |
| 76 | f64 timeout; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 77 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 78 | if (scm->socket_fd == 0) |
| 79 | return -1; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 80 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 81 | if (wait) |
| 82 | timeout = clib_time_now (&scm->clib_time) + wait; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 83 | |
| 84 | while (1) |
| 85 | { |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 86 | while (vec_len (scm->socket_rx_buffer) < sizeof (*mbp)) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 87 | { |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 88 | current_rx_index = vec_len (scm->socket_rx_buffer); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 89 | vec_validate (scm->socket_rx_buffer, current_rx_index |
| 90 | + scm->socket_buffer_size - 1); |
| 91 | _vec_len (scm->socket_rx_buffer) = current_rx_index; |
| 92 | n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index, |
| 93 | scm->socket_buffer_size); |
| 94 | if (n < 0) |
| 95 | { |
Florin Coras | 66a1003 | 2018-12-21 16:23:09 -0800 | [diff] [blame] | 96 | if (errno == EAGAIN) |
| 97 | continue; |
| 98 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 99 | clib_unix_warning ("socket_read"); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 100 | return -1; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 101 | } |
| 102 | _vec_len (scm->socket_rx_buffer) += n; |
| 103 | } |
| 104 | |
| 105 | #if CLIB_DEBUG > 1 |
| 106 | if (n > 0) |
| 107 | clib_warning ("read %d bytes", n); |
| 108 | #endif |
| 109 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 110 | mbp = (msgbuf_t *) (scm->socket_rx_buffer); |
| 111 | data_len = ntohl (mbp->data_len); |
| 112 | current_rx_index = vec_len (scm->socket_rx_buffer); |
| 113 | vec_validate (scm->socket_rx_buffer, current_rx_index + data_len); |
| 114 | _vec_len (scm->socket_rx_buffer) = current_rx_index; |
| 115 | mbp = (msgbuf_t *) (scm->socket_rx_buffer); |
| 116 | msg_size = data_len + sizeof (*mbp); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 117 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 118 | while (vec_len (scm->socket_rx_buffer) < msg_size) |
| 119 | { |
| 120 | n = read (scm->socket_fd, |
| 121 | scm->socket_rx_buffer + vec_len (scm->socket_rx_buffer), |
| 122 | msg_size - vec_len (scm->socket_rx_buffer)); |
Florin Coras | 66a1003 | 2018-12-21 16:23:09 -0800 | [diff] [blame] | 123 | if (n < 0) |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 124 | { |
Florin Coras | 66a1003 | 2018-12-21 16:23:09 -0800 | [diff] [blame] | 125 | if (errno == EAGAIN) |
| 126 | continue; |
| 127 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 128 | clib_unix_warning ("socket_read"); |
| 129 | return -1; |
| 130 | } |
| 131 | _vec_len (scm->socket_rx_buffer) += n; |
| 132 | } |
| 133 | |
| 134 | if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp)) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 135 | { |
| 136 | vl_msg_api_socket_handler ((void *) (mbp->data)); |
| 137 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 138 | if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp)) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 139 | _vec_len (scm->socket_rx_buffer) = 0; |
| 140 | else |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 141 | vec_delete (scm->socket_rx_buffer, data_len + sizeof (*mbp), 0); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 142 | mbp = 0; |
| 143 | |
| 144 | /* Quit if we're out of data, and not expecting a ping reply */ |
| 145 | if (vec_len (scm->socket_rx_buffer) == 0 |
| 146 | && scm->control_pings_outstanding == 0) |
| 147 | break; |
| 148 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 149 | if (wait && clib_time_now (&scm->clib_time) >= timeout) |
| 150 | return -1; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 151 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 152 | return 0; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | int |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 156 | vl_socket_client_read (int wait) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 157 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 158 | return vl_socket_client_read_internal (socket_client_ctx, wait); |
| 159 | } |
| 160 | |
| 161 | int |
| 162 | vl_socket_client_read2 (socket_client_main_t * scm, int wait) |
| 163 | { |
| 164 | socket_client_main_t *old_ctx; |
| 165 | int rv; |
| 166 | |
| 167 | old_ctx = vl_socket_client_ctx_push (scm); |
| 168 | rv = vl_socket_client_read_internal (scm, wait); |
| 169 | vl_socket_client_ctx_pop (old_ctx); |
| 170 | return rv; |
| 171 | } |
| 172 | |
| 173 | static int |
| 174 | vl_socket_client_write_internal (socket_client_main_t * scm) |
| 175 | { |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 176 | int n; |
| 177 | |
| 178 | msgbuf_t msgbuf = { |
| 179 | .q = 0, |
| 180 | .gc_mark_timestamp = 0, |
| 181 | .data_len = htonl (scm->socket_tx_nbytes), |
| 182 | }; |
| 183 | |
| 184 | n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf)); |
| 185 | if (n < sizeof (msgbuf)) |
| 186 | { |
| 187 | clib_unix_warning ("socket write (msgbuf)"); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | n = write (scm->socket_fd, scm->socket_tx_buffer, scm->socket_tx_nbytes); |
| 192 | if (n < scm->socket_tx_nbytes) |
| 193 | { |
| 194 | clib_unix_warning ("socket write (msg)"); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | return n; |
| 199 | } |
| 200 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 201 | int |
| 202 | vl_socket_client_write (void) |
| 203 | { |
| 204 | return vl_socket_client_write_internal (socket_client_ctx); |
| 205 | } |
| 206 | |
| 207 | int |
| 208 | vl_socket_client_write2 (socket_client_main_t * scm) |
| 209 | { |
| 210 | socket_client_main_t *old_ctx; |
| 211 | int rv; |
| 212 | |
| 213 | old_ctx = vl_socket_client_ctx_push (scm); |
| 214 | rv = vl_socket_client_write_internal (scm); |
| 215 | vl_socket_client_ctx_pop (old_ctx); |
| 216 | return rv; |
| 217 | } |
| 218 | |
| 219 | void * |
| 220 | vl_socket_client_msg_alloc2 (socket_client_main_t * scm, int nbytes) |
| 221 | { |
| 222 | scm->socket_tx_nbytes = nbytes; |
| 223 | return ((void *) scm->socket_tx_buffer); |
| 224 | } |
| 225 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 226 | void * |
| 227 | vl_socket_client_msg_alloc (int nbytes) |
| 228 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 229 | return vl_socket_client_msg_alloc2 (socket_client_ctx, nbytes); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 233 | vl_socket_client_disconnect2 (socket_client_main_t * scm) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 234 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 235 | if (vl_mem_client_is_connected ()) |
| 236 | { |
| 237 | vl_client_disconnect_from_vlib_no_unmap (); |
| 238 | ssvm_delete_memfd (&scm->memfd_segment); |
| 239 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 240 | if (scm->socket_fd && (close (scm->socket_fd) < 0)) |
| 241 | clib_unix_warning ("close"); |
| 242 | scm->socket_fd = 0; |
| 243 | } |
| 244 | |
| 245 | void |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 246 | vl_socket_client_disconnect (void) |
| 247 | { |
| 248 | vl_socket_client_disconnect2 (socket_client_ctx); |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | vl_socket_client_enable_disable2 (socket_client_main_t * scm, int enable) |
| 253 | { |
| 254 | scm->socket_enable = enable; |
| 255 | } |
| 256 | |
| 257 | void |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 258 | vl_socket_client_enable_disable (int enable) |
| 259 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 260 | vl_socket_client_enable_disable2 (socket_client_ctx, enable); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 263 | clib_error_t * |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 264 | vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 265 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 266 | socket_client_main_t *scm = socket_client_ctx; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 267 | char msgbuf[16]; |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 268 | char ctl[CMSG_SPACE (sizeof (int) * n_fds) |
| 269 | + CMSG_SPACE (sizeof (struct ucred))]; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 270 | struct msghdr mh = { 0 }; |
| 271 | struct iovec iov[1]; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 272 | ssize_t size = 0; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 273 | struct ucred *cr = 0; |
| 274 | struct cmsghdr *cmsg; |
| 275 | pid_t pid __attribute__ ((unused)); |
| 276 | uid_t uid __attribute__ ((unused)); |
| 277 | gid_t gid __attribute__ ((unused)); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 278 | f64 timeout; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 279 | |
| 280 | iov[0].iov_base = msgbuf; |
| 281 | iov[0].iov_len = 5; |
| 282 | mh.msg_iov = iov; |
| 283 | mh.msg_iovlen = 1; |
| 284 | mh.msg_control = ctl; |
| 285 | mh.msg_controllen = sizeof (ctl); |
| 286 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 287 | clib_memset (ctl, 0, sizeof (ctl)); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 288 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 289 | if (wait != ~0) |
| 290 | { |
| 291 | timeout = clib_time_now (&scm->clib_time) + wait; |
| 292 | while (size != 5 && clib_time_now (&scm->clib_time) < timeout) |
| 293 | size = recvmsg (socket_fd, &mh, MSG_DONTWAIT); |
| 294 | } |
| 295 | else |
| 296 | size = recvmsg (socket_fd, &mh, 0); |
| 297 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 298 | if (size != 5) |
| 299 | { |
| 300 | return (size == 0) ? clib_error_return (0, "disconnected") : |
| 301 | clib_error_return_unix (0, "recvmsg: malformed message (fd %d)", |
| 302 | socket_fd); |
| 303 | } |
| 304 | |
| 305 | cmsg = CMSG_FIRSTHDR (&mh); |
| 306 | while (cmsg) |
| 307 | { |
| 308 | if (cmsg->cmsg_level == SOL_SOCKET) |
| 309 | { |
| 310 | if (cmsg->cmsg_type == SCM_CREDENTIALS) |
| 311 | { |
| 312 | cr = (struct ucred *) CMSG_DATA (cmsg); |
| 313 | uid = cr->uid; |
| 314 | gid = cr->gid; |
| 315 | pid = cr->pid; |
| 316 | } |
| 317 | else if (cmsg->cmsg_type == SCM_RIGHTS) |
| 318 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 319 | clib_memcpy_fast (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | cmsg = CMSG_NXTHDR (&mh, cmsg); |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static void vl_api_sock_init_shm_reply_t_handler |
| 328 | (vl_api_sock_init_shm_reply_t * mp) |
| 329 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 330 | socket_client_main_t *scm = socket_client_ctx; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 331 | ssvm_private_t *memfd = &scm->memfd_segment; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 332 | i32 retval = ntohl (mp->retval); |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame^] | 333 | api_main_t *am = vlibapi_get_main (); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 334 | clib_error_t *error; |
| 335 | int my_fd = -1; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 336 | u8 *new_name; |
| 337 | |
| 338 | if (retval) |
| 339 | { |
| 340 | clib_warning ("failed to init shmem"); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Check the socket for the magic fd |
| 346 | */ |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 347 | error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 348 | if (error) |
| 349 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 350 | clib_error_report (error); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 351 | retval = -99; |
| 352 | return; |
| 353 | } |
| 354 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 355 | clib_memset (memfd, 0, sizeof (*memfd)); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 356 | memfd->fd = my_fd; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 357 | |
| 358 | /* Note: this closes memfd.fd */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 359 | retval = ssvm_slave_init_memfd (memfd); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 360 | if (retval) |
| 361 | clib_warning ("WARNING: segment map returned %d", retval); |
| 362 | |
| 363 | /* |
| 364 | * Pivot to the memory client segment that vpp just created |
| 365 | */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 366 | am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 367 | am->shmem_hdr = (void *) am->vlib_rp->user_ctx; |
| 368 | |
| 369 | new_name = format (0, "%v[shm]%c", scm->name, 0); |
| 370 | vl_client_install_client_message_handlers (); |
Tomasz Kulasek | 97dcf5b | 2019-01-31 18:26:32 +0100 | [diff] [blame] | 371 | if (scm->want_shm_pthread) |
| 372 | { |
| 373 | vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name, |
| 374 | 32 /* input_queue_length */ ); |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | vl_client_connect_to_vlib_no_rx_pthread_no_map ("pvt", |
| 379 | (char *) new_name, 32 |
| 380 | /* input_queue_length */ |
| 381 | ); |
| 382 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 383 | vl_socket_client_enable_disable (0); |
| 384 | vec_free (new_name); |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp) |
| 389 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 390 | socket_client_main_t *scm = socket_client_ctx; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 391 | if (!mp->response) |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 392 | { |
| 393 | scm->socket_enable = 1; |
| 394 | scm->client_index = clib_net_to_host_u32 (mp->index); |
| 395 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | #define foreach_sock_client_api_msg \ |
| 399 | _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \ |
| 400 | _(SOCK_INIT_SHM_REPLY, sock_init_shm_reply) \ |
| 401 | |
| 402 | static void |
| 403 | noop_handler (void *notused) |
| 404 | { |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | vl_sock_client_install_message_handlers (void) |
| 409 | { |
| 410 | |
| 411 | #define _(N,n) \ |
| 412 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 413 | vl_api_##n##_t_handler, \ |
| 414 | noop_handler, \ |
| 415 | vl_api_##n##_t_endian, \ |
| 416 | vl_api_##n##_t_print, \ |
| 417 | sizeof(vl_api_##n##_t), 1); |
| 418 | foreach_sock_client_api_msg; |
| 419 | #undef _ |
| 420 | } |
| 421 | |
| 422 | int |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 423 | vl_socket_client_connect_internal (socket_client_main_t * scm, |
| 424 | char *socket_path, char *client_name, |
| 425 | u32 socket_buffer_size) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 426 | { |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 427 | vl_api_sockclnt_create_t *mp; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 428 | clib_socket_t *sock; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 429 | clib_error_t *error; |
| 430 | |
| 431 | /* Already connected? */ |
| 432 | if (scm->socket_fd) |
| 433 | return (-2); |
| 434 | |
| 435 | /* bogus call? */ |
| 436 | if (socket_path == 0 || client_name == 0) |
| 437 | return (-3); |
| 438 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 439 | sock = &scm->client_socket; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 440 | sock->config = socket_path; |
Ole Troan | 4ff09ae | 2019-04-15 11:27:22 +0200 | [diff] [blame] | 441 | sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_NON_BLOCKING_CONNECT; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 442 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 443 | if ((error = clib_socket_init (sock))) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 444 | { |
| 445 | clib_error_report (error); |
| 446 | return (-1); |
| 447 | } |
| 448 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 449 | vl_sock_client_install_message_handlers (); |
| 450 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 451 | scm->socket_fd = sock->fd; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 452 | scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size : |
| 453 | SOCKET_CLIENT_DEFAULT_BUFFER_SIZE; |
| 454 | vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1); |
| 455 | vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1); |
| 456 | _vec_len (scm->socket_rx_buffer) = 0; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 457 | _vec_len (scm->socket_tx_buffer) = 0; |
| 458 | scm->name = format (0, "%s", client_name); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 459 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 460 | mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp)); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 461 | mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE); |
Ole Troan | 7adaa22 | 2019-08-27 15:05:27 +0200 | [diff] [blame] | 462 | strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1); |
| 463 | mp->name[sizeof (mp->name) - 1] = 0; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 464 | mp->context = 0xfeedface; |
| 465 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 466 | clib_time_init (&scm->clib_time); |
| 467 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 468 | if (vl_socket_client_write_internal (scm) <= 0) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 469 | return (-1); |
| 470 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 471 | if (vl_socket_client_read_internal (scm, 5)) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 472 | return (-1); |
| 473 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 474 | return (0); |
| 475 | } |
| 476 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 477 | int |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 478 | vl_socket_client_connect (char *socket_path, char *client_name, |
| 479 | u32 socket_buffer_size) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 480 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 481 | return vl_socket_client_connect_internal (socket_client_ctx, socket_path, |
| 482 | client_name, socket_buffer_size); |
| 483 | } |
| 484 | |
| 485 | int |
| 486 | vl_socket_client_connect2 (socket_client_main_t * scm, char *socket_path, |
| 487 | char *client_name, u32 socket_buffer_size) |
| 488 | { |
| 489 | socket_client_main_t *old_ctx; |
| 490 | int rv; |
| 491 | |
| 492 | old_ctx = vl_socket_client_ctx_push (scm); |
| 493 | rv = vl_socket_client_connect_internal (socket_client_ctx, socket_path, |
| 494 | client_name, socket_buffer_size); |
| 495 | vl_socket_client_ctx_pop (old_ctx); |
| 496 | return rv; |
| 497 | } |
| 498 | |
| 499 | int |
| 500 | vl_socket_client_init_shm_internal (socket_client_main_t * scm, |
| 501 | vl_api_shm_elem_config_t * config, |
| 502 | int want_pthread) |
| 503 | { |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 504 | vl_api_sock_init_shm_t *mp; |
| 505 | int rv, i; |
| 506 | u64 *cfg; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 507 | |
Tomasz Kulasek | 97dcf5b | 2019-01-31 18:26:32 +0100 | [diff] [blame] | 508 | scm->want_shm_pthread = want_pthread; |
| 509 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 510 | mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp) + |
| 511 | vec_len (config) * sizeof (u64)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 512 | clib_memset (mp, 0, sizeof (*mp)); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 513 | mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM); |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 514 | mp->client_index = clib_host_to_net_u32 (scm->client_index); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 515 | mp->requested_size = 64 << 20; |
| 516 | |
| 517 | if (config) |
| 518 | { |
| 519 | for (i = 0; i < vec_len (config); i++) |
| 520 | { |
| 521 | cfg = (u64 *) & config[i]; |
| 522 | mp->configs[i] = *cfg; |
| 523 | } |
| 524 | mp->nitems = vec_len (config); |
| 525 | } |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 526 | rv = vl_socket_client_write_internal (scm); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 527 | if (rv <= 0) |
| 528 | return rv; |
| 529 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 530 | if (vl_socket_client_read_internal (scm, 1)) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 531 | return -1; |
| 532 | |
| 533 | return 0; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 534 | } |
| 535 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 536 | int |
| 537 | vl_socket_client_init_shm (vl_api_shm_elem_config_t * config, |
| 538 | int want_pthread) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 539 | { |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 540 | return vl_socket_client_init_shm_internal (socket_client_ctx, config, |
| 541 | want_pthread); |
| 542 | } |
| 543 | |
| 544 | int |
| 545 | vl_socket_client_init_shm2 (socket_client_main_t * scm, |
| 546 | vl_api_shm_elem_config_t * config, |
| 547 | int want_pthread) |
| 548 | { |
| 549 | socket_client_main_t *old_ctx; |
| 550 | int rv; |
| 551 | |
| 552 | old_ctx = vl_socket_client_ctx_push (scm); |
| 553 | rv = vl_socket_client_init_shm_internal (socket_client_ctx, config, |
| 554 | want_pthread); |
| 555 | vl_socket_client_ctx_pop (old_ctx); |
| 556 | return rv; |
| 557 | } |
| 558 | |
| 559 | clib_error_t * |
| 560 | vl_socket_client_recv_fd_msg2 (socket_client_main_t * scm, int fds[], |
| 561 | int n_fds, u32 wait) |
| 562 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 563 | if (!scm->socket_fd) |
| 564 | return clib_error_return (0, "no socket"); |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 565 | return vl_sock_api_recv_fd_msg (scm->client_socket.fd, fds, n_fds, wait); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Florin Coras | 59cea1a | 2019-11-25 13:40:42 -0800 | [diff] [blame] | 568 | clib_error_t * |
| 569 | vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait) |
| 570 | { |
| 571 | return vl_socket_client_recv_fd_msg2 (socket_client_ctx, fds, n_fds, wait); |
| 572 | } |
| 573 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 574 | /* |
| 575 | * fd.io coding-style-patch-verification: ON |
| 576 | * |
| 577 | * Local Variables: |
| 578 | * eval: (c-set-style "gnu") |
| 579 | * End: |
| 580 | */ |