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; |
| 45 | |
| 46 | /* Debug aid */ |
| 47 | u32 vl (void *p) __attribute__ ((weak)); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 48 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 49 | u32 |
| 50 | vl (void *p) |
| 51 | { |
| 52 | return vec_len (p); |
| 53 | } |
| 54 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 55 | int |
| 56 | vl_socket_client_read (int wait) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 57 | { |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 58 | socket_client_main_t *scm = &socket_client_main; |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 59 | u32 data_len = 0, msg_size; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 60 | int n, current_rx_index; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 61 | msgbuf_t *mbp = 0; |
| 62 | f64 timeout; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 63 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 64 | if (scm->socket_fd == 0) |
| 65 | return -1; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 66 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 67 | if (wait) |
| 68 | timeout = clib_time_now (&scm->clib_time) + wait; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 69 | |
| 70 | while (1) |
| 71 | { |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 72 | while (vec_len (scm->socket_rx_buffer) < sizeof (*mbp)) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 73 | { |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 74 | current_rx_index = vec_len (scm->socket_rx_buffer); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 75 | vec_validate (scm->socket_rx_buffer, current_rx_index |
| 76 | + scm->socket_buffer_size - 1); |
| 77 | _vec_len (scm->socket_rx_buffer) = current_rx_index; |
| 78 | n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index, |
| 79 | scm->socket_buffer_size); |
| 80 | if (n < 0) |
| 81 | { |
| 82 | clib_unix_warning ("socket_read"); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 83 | return -1; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 84 | } |
| 85 | _vec_len (scm->socket_rx_buffer) += n; |
| 86 | } |
| 87 | |
| 88 | #if CLIB_DEBUG > 1 |
| 89 | if (n > 0) |
| 90 | clib_warning ("read %d bytes", n); |
| 91 | #endif |
| 92 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 93 | mbp = (msgbuf_t *) (scm->socket_rx_buffer); |
| 94 | data_len = ntohl (mbp->data_len); |
| 95 | current_rx_index = vec_len (scm->socket_rx_buffer); |
| 96 | vec_validate (scm->socket_rx_buffer, current_rx_index + data_len); |
| 97 | _vec_len (scm->socket_rx_buffer) = current_rx_index; |
| 98 | mbp = (msgbuf_t *) (scm->socket_rx_buffer); |
| 99 | msg_size = data_len + sizeof (*mbp); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 100 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 101 | while (vec_len (scm->socket_rx_buffer) < msg_size) |
| 102 | { |
| 103 | n = read (scm->socket_fd, |
| 104 | scm->socket_rx_buffer + vec_len (scm->socket_rx_buffer), |
| 105 | msg_size - vec_len (scm->socket_rx_buffer)); |
| 106 | if (n < 0 && errno != EAGAIN) |
| 107 | { |
| 108 | clib_unix_warning ("socket_read"); |
| 109 | return -1; |
| 110 | } |
| 111 | _vec_len (scm->socket_rx_buffer) += n; |
| 112 | } |
| 113 | |
| 114 | if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp)) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 115 | { |
| 116 | vl_msg_api_socket_handler ((void *) (mbp->data)); |
| 117 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 118 | if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp)) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 119 | _vec_len (scm->socket_rx_buffer) = 0; |
| 120 | else |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 121 | vec_delete (scm->socket_rx_buffer, data_len + sizeof (*mbp), 0); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 122 | mbp = 0; |
| 123 | |
| 124 | /* Quit if we're out of data, and not expecting a ping reply */ |
| 125 | if (vec_len (scm->socket_rx_buffer) == 0 |
| 126 | && scm->control_pings_outstanding == 0) |
| 127 | break; |
| 128 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 129 | if (wait && clib_time_now (&scm->clib_time) >= timeout) |
| 130 | return -1; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 131 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 132 | return 0; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 136 | vl_socket_client_write (void) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 137 | { |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 138 | socket_client_main_t *scm = &socket_client_main; |
| 139 | int n; |
| 140 | |
| 141 | msgbuf_t msgbuf = { |
| 142 | .q = 0, |
| 143 | .gc_mark_timestamp = 0, |
| 144 | .data_len = htonl (scm->socket_tx_nbytes), |
| 145 | }; |
| 146 | |
| 147 | n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf)); |
| 148 | if (n < sizeof (msgbuf)) |
| 149 | { |
| 150 | clib_unix_warning ("socket write (msgbuf)"); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | n = write (scm->socket_fd, scm->socket_tx_buffer, scm->socket_tx_nbytes); |
| 155 | if (n < scm->socket_tx_nbytes) |
| 156 | { |
| 157 | clib_unix_warning ("socket write (msg)"); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | return n; |
| 162 | } |
| 163 | |
| 164 | void * |
| 165 | vl_socket_client_msg_alloc (int nbytes) |
| 166 | { |
| 167 | socket_client_main.socket_tx_nbytes = nbytes; |
| 168 | return ((void *) socket_client_main.socket_tx_buffer); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | vl_socket_client_disconnect (void) |
| 173 | { |
| 174 | socket_client_main_t *scm = &socket_client_main; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 175 | |
| 176 | if (vl_mem_client_is_connected ()) |
| 177 | { |
| 178 | vl_client_disconnect_from_vlib_no_unmap (); |
| 179 | ssvm_delete_memfd (&scm->memfd_segment); |
| 180 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 181 | if (scm->socket_fd && (close (scm->socket_fd) < 0)) |
| 182 | clib_unix_warning ("close"); |
| 183 | scm->socket_fd = 0; |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | vl_socket_client_enable_disable (int enable) |
| 188 | { |
| 189 | socket_client_main_t *scm = &socket_client_main; |
| 190 | scm->socket_enable = enable; |
| 191 | } |
| 192 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 193 | clib_error_t * |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 194 | 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] | 195 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 196 | socket_client_main_t *scm = &socket_client_main; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 197 | char msgbuf[16]; |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 198 | char ctl[CMSG_SPACE (sizeof (int) * n_fds) |
| 199 | + CMSG_SPACE (sizeof (struct ucred))]; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 200 | struct msghdr mh = { 0 }; |
| 201 | struct iovec iov[1]; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 202 | ssize_t size = 0; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 203 | struct ucred *cr = 0; |
| 204 | struct cmsghdr *cmsg; |
| 205 | pid_t pid __attribute__ ((unused)); |
| 206 | uid_t uid __attribute__ ((unused)); |
| 207 | gid_t gid __attribute__ ((unused)); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 208 | f64 timeout; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 209 | |
| 210 | iov[0].iov_base = msgbuf; |
| 211 | iov[0].iov_len = 5; |
| 212 | mh.msg_iov = iov; |
| 213 | mh.msg_iovlen = 1; |
| 214 | mh.msg_control = ctl; |
| 215 | mh.msg_controllen = sizeof (ctl); |
| 216 | |
| 217 | memset (ctl, 0, sizeof (ctl)); |
| 218 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 219 | if (wait != ~0) |
| 220 | { |
| 221 | timeout = clib_time_now (&scm->clib_time) + wait; |
| 222 | while (size != 5 && clib_time_now (&scm->clib_time) < timeout) |
| 223 | size = recvmsg (socket_fd, &mh, MSG_DONTWAIT); |
| 224 | } |
| 225 | else |
| 226 | size = recvmsg (socket_fd, &mh, 0); |
| 227 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 228 | if (size != 5) |
| 229 | { |
| 230 | return (size == 0) ? clib_error_return (0, "disconnected") : |
| 231 | clib_error_return_unix (0, "recvmsg: malformed message (fd %d)", |
| 232 | socket_fd); |
| 233 | } |
| 234 | |
| 235 | cmsg = CMSG_FIRSTHDR (&mh); |
| 236 | while (cmsg) |
| 237 | { |
| 238 | if (cmsg->cmsg_level == SOL_SOCKET) |
| 239 | { |
| 240 | if (cmsg->cmsg_type == SCM_CREDENTIALS) |
| 241 | { |
| 242 | cr = (struct ucred *) CMSG_DATA (cmsg); |
| 243 | uid = cr->uid; |
| 244 | gid = cr->gid; |
| 245 | pid = cr->pid; |
| 246 | } |
| 247 | else if (cmsg->cmsg_type == SCM_RIGHTS) |
| 248 | { |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 249 | clib_memcpy (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | cmsg = CMSG_NXTHDR (&mh, cmsg); |
| 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static void vl_api_sock_init_shm_reply_t_handler |
| 258 | (vl_api_sock_init_shm_reply_t * mp) |
| 259 | { |
| 260 | socket_client_main_t *scm = &socket_client_main; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 261 | ssvm_private_t *memfd = &scm->memfd_segment; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 262 | i32 retval = ntohl (mp->retval); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 263 | api_main_t *am = &api_main; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 264 | clib_error_t *error; |
| 265 | int my_fd = -1; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 266 | u8 *new_name; |
| 267 | |
| 268 | if (retval) |
| 269 | { |
| 270 | clib_warning ("failed to init shmem"); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Check the socket for the magic fd |
| 276 | */ |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 277 | 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] | 278 | if (error) |
| 279 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 280 | clib_error_report (error); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 281 | retval = -99; |
| 282 | return; |
| 283 | } |
| 284 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 285 | memset (memfd, 0, sizeof (*memfd)); |
| 286 | memfd->fd = my_fd; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 287 | |
| 288 | /* Note: this closes memfd.fd */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 289 | retval = ssvm_slave_init_memfd (memfd); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 290 | if (retval) |
| 291 | clib_warning ("WARNING: segment map returned %d", retval); |
| 292 | |
| 293 | /* |
| 294 | * Pivot to the memory client segment that vpp just created |
| 295 | */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 296 | am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 297 | am->shmem_hdr = (void *) am->vlib_rp->user_ctx; |
| 298 | |
| 299 | new_name = format (0, "%v[shm]%c", scm->name, 0); |
| 300 | vl_client_install_client_message_handlers (); |
| 301 | vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name, |
| 302 | 32 /* input_queue_length */ ); |
| 303 | vl_socket_client_enable_disable (0); |
| 304 | vec_free (new_name); |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp) |
| 309 | { |
| 310 | socket_client_main_t *scm = &socket_client_main; |
| 311 | if (!mp->response) |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 312 | { |
| 313 | scm->socket_enable = 1; |
| 314 | scm->client_index = clib_net_to_host_u32 (mp->index); |
| 315 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | #define foreach_sock_client_api_msg \ |
| 319 | _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \ |
| 320 | _(SOCK_INIT_SHM_REPLY, sock_init_shm_reply) \ |
| 321 | |
| 322 | static void |
| 323 | noop_handler (void *notused) |
| 324 | { |
| 325 | } |
| 326 | |
| 327 | void |
| 328 | vl_sock_client_install_message_handlers (void) |
| 329 | { |
| 330 | |
| 331 | #define _(N,n) \ |
| 332 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 333 | vl_api_##n##_t_handler, \ |
| 334 | noop_handler, \ |
| 335 | vl_api_##n##_t_endian, \ |
| 336 | vl_api_##n##_t_print, \ |
| 337 | sizeof(vl_api_##n##_t), 1); |
| 338 | foreach_sock_client_api_msg; |
| 339 | #undef _ |
| 340 | } |
| 341 | |
| 342 | int |
| 343 | vl_socket_client_connect (char *socket_path, char *client_name, |
| 344 | u32 socket_buffer_size) |
| 345 | { |
| 346 | socket_client_main_t *scm = &socket_client_main; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 347 | vl_api_sockclnt_create_t *mp; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 348 | clib_socket_t *sock; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 349 | clib_error_t *error; |
| 350 | |
| 351 | /* Already connected? */ |
| 352 | if (scm->socket_fd) |
| 353 | return (-2); |
| 354 | |
| 355 | /* bogus call? */ |
| 356 | if (socket_path == 0 || client_name == 0) |
| 357 | return (-3); |
| 358 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 359 | sock = &scm->client_socket; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 360 | sock->config = socket_path; |
| 361 | sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET; |
| 362 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 363 | if ((error = clib_socket_init (sock))) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 364 | { |
| 365 | clib_error_report (error); |
| 366 | return (-1); |
| 367 | } |
| 368 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 369 | vl_sock_client_install_message_handlers (); |
| 370 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 371 | scm->socket_fd = sock->fd; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 372 | scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size : |
| 373 | SOCKET_CLIENT_DEFAULT_BUFFER_SIZE; |
| 374 | vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1); |
| 375 | vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1); |
| 376 | _vec_len (scm->socket_rx_buffer) = 0; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 377 | _vec_len (scm->socket_tx_buffer) = 0; |
| 378 | scm->name = format (0, "%s", client_name); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 379 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 380 | mp = vl_socket_client_msg_alloc (sizeof (*mp)); |
| 381 | mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE); |
| 382 | strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1); |
| 383 | mp->name[sizeof (mp->name) - 1] = 0; |
| 384 | mp->context = 0xfeedface; |
| 385 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 386 | clib_time_init (&scm->clib_time); |
| 387 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 388 | if (vl_socket_client_write () <= 0) |
| 389 | return (-1); |
| 390 | |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 391 | if (vl_socket_client_read (5)) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 392 | return (-1); |
| 393 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 394 | return (0); |
| 395 | } |
| 396 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 397 | int |
| 398 | vl_socket_client_init_shm (vl_api_shm_elem_config_t * config) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 399 | { |
Florin Coras | 2881dec | 2018-10-02 18:29:25 -0700 | [diff] [blame] | 400 | socket_client_main_t *scm = &socket_client_main; |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 401 | vl_api_sock_init_shm_t *mp; |
| 402 | int rv, i; |
| 403 | u64 *cfg; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 404 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 405 | mp = vl_socket_client_msg_alloc (sizeof (*mp) + |
| 406 | vec_len (config) * sizeof (u64)); |
| 407 | memset (mp, 0, sizeof (*mp)); |
| 408 | 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] | 409 | mp->client_index = clib_host_to_net_u32 (scm->client_index); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 410 | mp->requested_size = 64 << 20; |
| 411 | |
| 412 | if (config) |
| 413 | { |
| 414 | for (i = 0; i < vec_len (config); i++) |
| 415 | { |
| 416 | cfg = (u64 *) & config[i]; |
| 417 | mp->configs[i] = *cfg; |
| 418 | } |
| 419 | mp->nitems = vec_len (config); |
| 420 | } |
| 421 | rv = vl_socket_client_write (); |
| 422 | if (rv <= 0) |
| 423 | return rv; |
| 424 | |
| 425 | if (vl_socket_client_read (1)) |
| 426 | return -1; |
| 427 | |
| 428 | return 0; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 429 | } |
| 430 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 431 | clib_error_t * |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 432 | vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 433 | { |
| 434 | socket_client_main_t *scm = &socket_client_main; |
| 435 | if (!scm->socket_fd) |
| 436 | return clib_error_return (0, "no socket"); |
Florin Coras | 466f289 | 2018-08-03 02:50:43 -0700 | [diff] [blame] | 437 | 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] | 438 | } |
| 439 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 440 | /* |
| 441 | * fd.io coding-style-patch-verification: ON |
| 442 | * |
| 443 | * Local Variables: |
| 444 | * eval: (c-set-style "gnu") |
| 445 | * End: |
| 446 | */ |