summaryrefslogtreecommitdiffstats
path: root/src/vlibmemory/socket_client.c
blob: b60fd4f6229f7a0a32a612076803fd669198aaf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 *------------------------------------------------------------------
 * socket_client.c - API message handling over sockets, client code.
 *
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <vppinfra/clib.h>
#include <vppinfra/vec.h>
#include <vppinfra/hash.h>
#include <vppinfra/bitmap.h>
#include <vppinfra/fifo.h>
#include <vppinfra/time.h>
#include <vppinfra/mheap.h>
#include <vppinfra/heap.h>
#include <vppinfra/pool.h>
#include <vppinfra/format.h>

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vlibmemory/api.h>

#include <vlibmemory/vl_memory_msg_enum.h>

#define vl_typedefs		/* define message structures */
#include <vlibmemory/vl_memory_api_h.h>
#undef vl_typedefs

#define vl_endianfun		/* define message structures */
#include <vlibmemory/vl_memory_api_h.h>
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...) clib_warning (__VA_ARGS__)
#define vl_printfun
#include <vlibmemory/vl_memory_api_h.h>
#undef vl_printfun

socket_client_main_t socket_client_main;

/* Debug aid */
u32 vl (void *p) __attribute__ ((weak));
u32
vl (void *p)
{
  return vec_len (p);
}

void
vl_socket_client_read_reply (socket_client_main_t * scm)
{
  int n, current_rx_index;
  msgbuf_t *mbp;

  if (scm->socket_fd == 0 || scm->socket_enable == 0)
    return;

  mbp = 0;

  while (1)
    {
      current_rx_index = vec_len (scm->socket_rx_buffer);
      while (vec_len (scm->socket_rx_buffer) <
	     sizeof (*mbp) + 2 /* msg id */ )
	{
	  vec_validate (scm->socket_rx_buffer, current_rx_index
			+ scm->socket_buffer_size - 1);
	  _vec_len (scm->socket_rx_buffer) = current_rx_index;
	  n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
		    scm->socket_buffer_size);
	  if (n < 0)
	    {
	      clib_unix_warning ("socket_read");
	      return;
	    }
	  _vec_len (scm->socket_rx_buffer) += n;
	}

#if CLIB_DEBUG > 1
      if (n > 0)
	clib_warning ("read %d bytes", n);
#endif

      if (mbp == 0)
	mbp = (msgbuf_t *) (scm->socket_rx_buffer);

      if (vec_len (scm->socket_rx_buffer) >= ntohl (mbp->data_len)
	  + sizeof (*mbp))
	{
	  vl_msg_api_socket_handler ((void *) (mbp->data));

	  if (vec_len (scm->socket_rx_buffer) == ntohl (mbp->data_len)
	      + sizeof (*mbp))
	    _vec_len (scm->socket_rx_buffer) = 0;
	  else
	    vec_delete (scm->socket_rx_buffer, ntohl (mbp->data_len)
			+ sizeof (*mbp), 0);
	  mbp = 0;

	  /* Quit if we're out of data, and not expecting a ping reply */
	  if (vec_len (scm->socket_rx_buffer) == 0
	      && scm->control_pings_outstanding == 0)
	    break;
	}
    }
}

int
vl_socket_client_connect (socket_client_main_t * scm, char *socket_path,
			  char *client_name, u32 socket_buffer_size)
{
  char buffer[256];
  char *rdptr;
  int n, total_bytes;
  vl_api_sockclnt_create_reply_t *rp;
  vl_api_sockclnt_create_t *mp;
  clib_socket_t *sock = &scm->client_socket;
  msgbuf_t *mbp;
  clib_error_t *error;

  /* Already connected? */
  if (scm->socket_fd)
    return (-2);

  /* bogus call? */
  if (socket_path == 0 || client_name == 0)
    return (-3);

  sock->config = socket_path;
  sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET;

  error = clib_socket_init (sock);

  if (error)
    {
      clib_error_report (error);
      return (-1);
    }

  scm->socket_fd = sock->fd;

  mbp = (msgbuf_t *) buffer;
  mbp->q = 0;
  mbp->data_len = htonl (sizeof (*mp));
  mbp->gc_mark_timestamp = 0;

  mp = (vl_api_sockclnt_create_t *) mbp->data;
  mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
  strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
  mp->name[sizeof (mp->name) - 1] = 0;
  mp->context = 0xfeedface;

  n = write (scm->socket_fd, mbp, sizeof (*mbp) + sizeof (*mp));
  if (n < 0)
    {
      clib_unix_warning ("socket write (msg)");
      return (-1);
    }

  memset (buffer, 0, sizeof (buffer));

  total_bytes = 0;
  rdptr = buffer;
  do
    {
      n = read (scm->socket_fd, rdptr, sizeof (buffer) - (rdptr - buffer));
      if (n < 0)
	{
	  clib_unix_warning ("socket read");
	}
      total_bytes += n;
      rdptr += n;
    }
  while (total_bytes < sizeof (vl_api_sockclnt_create_reply_t)
	 + sizeof (msgbuf_t));

  rp = (vl_api_sockclnt_create_reply_t *) (buffer + sizeof (msgbuf_t));
  if (ntohs (rp->_vl_msg_id) != VL_API_SOCKCLNT_CREATE_REPLY)
    {
      clib_warning ("connect reply got msg id %d\n", ntohs (rp->_vl_msg_id));
      return (-1);
    }

  /* allocate tx, rx buffers */
  scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
    SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
  vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
  vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
  _vec_len (scm->socket_rx_buffer) = 0;
  scm->socket_enable = 1;

  return (0);
}

void
vl_socket_client_disconnect (socket_client_main_t * scm)
{
  if (scm->socket_fd && (close (scm->socket_fd) < 0))
    clib_unix_warning ("close");
  scm->socket_fd = 0;
}

void
vl_socket_client_enable_disable (socket_client_main_t * scm, int enable)
{
  scm->socket_enable = enable;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
n> break; } { /* Time to add a new ply? */ clib_slist_elt_t *head_elt = pool_elt_at_index (sp->elts, 0); int top_level = vec_len (head_elt->n.nexts) - 1; if (((f64) sp->occupancy[top_level]) * sp->branching_factor > 1.0) { vec_add1 (sp->occupancy, 0); vec_add1 (head_elt->n.nexts, (u32) ~ 0); /* full match case returns n+1 items */ vec_validate (sp->path, vec_len (head_elt->n.nexts)); } } } clib_slist_search_result_t clib_slist_del (clib_slist_t * sp, void *key) { clib_slist_search_result_t search_result; clib_slist_elt_t *del_elt; int level; search_result = slist_search_internal (sp, key, 1 /* need full path */ ); if (PREDICT_FALSE (search_result == CLIB_SLIST_NO_MATCH)) return search_result; del_elt = pool_elt_at_index (sp->elts, sp->path[vec_len (sp->path) - 1]); ASSERT (vec_len (sp->path) > 1); for (level = 0; level < vec_len (sp->path) - 1; level++) { clib_slist_elt_t *path_elt; u32 path_elt_next_index; path_elt = pool_elt_at_index (sp->elts, sp->path[level]); path_elt_next_index = clib_slist_get_next_at_level (path_elt, level); /* Splice the item out of the list if it's adjacent to the victim */ if (path_elt_next_index == del_elt - sp->elts) { sp->occupancy[level]--; path_elt_next_index = clib_slist_get_next_at_level (del_elt, level); clib_slist_set_next_at_level (path_elt, path_elt_next_index, level); } } /* If this element is on more than two lists it has a vector of nexts */ if (!(del_elt->n.next0[0] & 1)) vec_free (del_elt->n.nexts); pool_put (sp->elts, del_elt); return CLIB_SLIST_MATCH; } u8 * format_slist (u8 * s, va_list * args) { clib_slist_t *sl = va_arg (*args, clib_slist_t *); int verbose = va_arg (*args, int); int i; clib_slist_elt_t *head_elt, *elt; s = format (s, "slist 0x%x, %u items, branching_factor %.2f\n", sl, sl->occupancy ? sl->occupancy[0] : 0, sl->branching_factor); if (pool_elts (sl->elts) == 0) return s; head_elt = pool_elt_at_index (sl->elts, 0); for (i = 0; i < vec_len (head_elt->n.nexts); i++) { s = format (s, "level %d: %d elts\n", i, sl->occupancy ? sl->occupancy[i] : 0); if (verbose && head_elt->n.nexts[i] != (u32) ~ 0) { elt = pool_elt_at_index (sl->elts, head_elt->n.nexts[i]); while (elt) { u32 next_index; s = format (s, "%U(%d) ", sl->format_user_element, elt->user_pool_index, elt - sl->elts); next_index = clib_slist_get_next_at_level (elt, i); ASSERT (next_index != 0x7fffffff); if (next_index == (u32) ~ 0) break; else elt = pool_elt_at_index (sl->elts, next_index); } } s = format (s, "\n"); } return s; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */