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