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