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