2 *------------------------------------------------------------------
3 * socket_client.c - API message handling over sockets, client code.
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
23 #include <sys/types.h>
26 #include <netinet/in.h>
33 #include <vppinfra/clib.h>
34 #include <vppinfra/vec.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/bitmap.h>
37 #include <vppinfra/fifo.h>
38 #include <vppinfra/time.h>
39 #include <vppinfra/mheap.h>
40 #include <vppinfra/heap.h>
41 #include <vppinfra/pool.h>
42 #include <vppinfra/format.h>
44 #include <vlib/vlib.h>
45 #include <vlib/unix/unix.h>
46 #include <vlibmemory/api.h>
48 #include <vlibmemory/vl_memory_msg_enum.h>
50 #define vl_typedefs /* define message structures */
51 #include <vlibmemory/vl_memory_api_h.h>
54 #define vl_endianfun /* define message structures */
55 #include <vlibmemory/vl_memory_api_h.h>
58 /* instantiate all the print functions we know about */
59 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
61 #include <vlibmemory/vl_memory_api_h.h>
64 socket_client_main_t socket_client_main;
67 u32 vl (void *p) __attribute__ ((weak));
75 vl_socket_client_read_reply (socket_client_main_t * scm)
77 int n, current_rx_index;
80 if (scm->socket_fd == 0 || scm->socket_enable == 0)
87 current_rx_index = vec_len (scm->socket_rx_buffer);
88 while (vec_len (scm->socket_rx_buffer) <
89 sizeof (*mbp) + 2 /* msg id */ )
91 vec_validate (scm->socket_rx_buffer, current_rx_index
92 + scm->socket_buffer_size - 1);
93 _vec_len (scm->socket_rx_buffer) = current_rx_index;
94 n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
95 scm->socket_buffer_size);
98 clib_unix_warning ("socket_read");
101 _vec_len (scm->socket_rx_buffer) += n;
106 clib_warning ("read %d bytes", n);
110 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
112 if (vec_len (scm->socket_rx_buffer) >= ntohl (mbp->data_len)
115 vl_msg_api_socket_handler ((void *) (mbp->data));
117 if (vec_len (scm->socket_rx_buffer) == ntohl (mbp->data_len)
119 _vec_len (scm->socket_rx_buffer) = 0;
121 vec_delete (scm->socket_rx_buffer, ntohl (mbp->data_len)
125 /* Quit if we're out of data, and not expecting a ping reply */
126 if (vec_len (scm->socket_rx_buffer) == 0
127 && scm->control_pings_outstanding == 0)
134 vl_socket_client_connect (socket_client_main_t * scm, char *socket_path,
135 char *client_name, u32 socket_buffer_size)
140 vl_api_sockclnt_create_reply_t *rp;
141 vl_api_sockclnt_create_t *mp;
142 clib_socket_t *sock = &scm->client_socket;
146 /* Already connected? */
151 if (socket_path == 0 || client_name == 0)
154 sock->config = socket_path;
155 sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET;
157 error = clib_socket_init (sock);
161 clib_error_report (error);
165 scm->socket_fd = sock->fd;
167 mbp = (msgbuf_t *) buffer;
169 mbp->data_len = ntohl (sizeof (*mp));
170 mbp->gc_mark_timestamp = 0;
172 mp = (vl_api_sockclnt_create_t *) mbp->data;
173 mp->_vl_msg_id = ntohs (VL_API_SOCKCLNT_CREATE);
174 strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
175 mp->name[sizeof (mp->name) - 1] = 0;
176 mp->context = 0xfeedface;
178 n = write (scm->socket_fd, mbp, sizeof (*mbp) + ntohl (mbp->data_len));
181 clib_unix_warning ("socket write (msg)");
185 memset (buffer, 0, sizeof (buffer));
191 n = read (scm->socket_fd, rdptr, sizeof (buffer) - (rdptr - buffer));
194 clib_unix_warning ("socket read");
199 while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t)
200 + sizeof (msgbuf_t));
202 rp = (vl_api_sockclnt_create_reply_t *) (buffer + sizeof (msgbuf_t));
203 if (ntohs (rp->_vl_msg_id) != VL_API_SOCKCLNT_CREATE_REPLY)
205 clib_warning ("connect reply got msg id %d\n", ntohs (rp->_vl_msg_id));
209 /* allocate tx, rx buffers */
210 scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
211 SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
212 vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
213 vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
214 _vec_len (scm->socket_rx_buffer) = 0;
215 scm->socket_enable = 1;
221 vl_socket_client_disconnect (socket_client_main_t * scm)
223 if (scm->socket_fd && (close (scm->socket_fd) < 0))
224 clib_unix_warning ("close");
229 vl_socket_client_enable_disable (socket_client_main_t * scm, int enable)
231 scm->socket_enable = enable;
235 * fd.io coding-style-patch-verification: ON
238 * eval: (c-set-style "gnu")