summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/socket.c
blob: 99b353fcd52deebfd729e0fb2cb498d3b384f371 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Copyright (c) 2015 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.
 */
/*
  Copyright (c) 2001, 2002, 2003, 2005 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>		/* strchr */

#include <vppinfra/mem.h>
#include <vppinfra/vec.h>
#include <vppinfra/socket.h>
#include <vppinfra/format.h>
#include <vppinfra/error.h>

void
clib_socket_tx_add_formatted (clib_socket_t * s, char *fmt, ...)
{
  va_list va;
  va_start (va, fmt);
  clib_socket_tx_add_va_formatted (s, fmt, &va);
  va_end (va);
}

/* Return and bind to an unused port. */
static word
find_free_port (word sock)
{
  word port;

  for (port = IPPORT_USERRESERVED; port < 1 << 16; port++)
    {
      struct sockaddr_in a;

      memset (&a, 0, sizeof (a));	/* Warnings be gone */

      a.sin_family = PF_INET;
      a.sin_addr.s_addr = INADDR_ANY;
      a.sin_port = htons (port);

      if (bind (sock, (struct sockaddr *) &a, sizeof (a)) >= 0)
	break;
    }

  return port < 1 << 16 ? port : -1;
}

/* Convert a config string to a struct sockaddr and length for use
   with bind or connect. */
static clib_error_t *
socket_config (char *config,
	       void *addr, socklen_t * addr_len, u32 ip4_default_address)
{
  clib_error_t *error = 0;

  if (!config)
    config = "";

  /* Anything that begins with a / is a local PF_LOCAL socket. */
  if (config[0] == '/')
    {
      struct sockaddr_un *su = addr;
      su->sun_family = PF_LOCAL;
      clib_memcpy (&su->sun_path, config,
		   clib_min (sizeof (su->sun_path), 1 + strlen (config)));
      *addr_len = sizeof (su[0]);
    }

  /* Hostname or hostname:port or port. */
  else
    {
      char *host_name;
      int port = -1;
      struct sockaddr_in *sa = addr;

      host_name = 0;
      port = -1;
      if (config[0] != 0)
	{
	  unformat_input_t i;

	  unformat_init_string (&i, config, strlen (config));
	  if (unformat (&i, "%s:%d", &host_name, &port)
	      || unformat (&i, "%s:0x%x", &host_name, &port))
	    ;
	  else if (unformat (&i, "%s", &host_name))
	    ;
	  else
	    error = clib_error_return (0, "unknown input `%U'",
				       format_unformat_error, &i);
	  unformat_free (&i);

	  if (error)
	    goto done;
	}

      sa->sin_family = PF_INET;
      *addr_len = sizeof (sa[0]);
      if (port != -1)
	sa->sin_port = htons (port);
      else
	sa->sin_port = 0;

      if (host_name)
	{
	  struct in_addr host_addr;

	  /* Recognize localhost to avoid host lookup in most common cast. */
	  if (!strcmp (host_name, "localhost"))
	    sa->sin_addr.s_addr = htonl (INADDR_LOOPBACK);

	  else if (inet_aton (host_name, &host_addr))
	    sa->sin_addr = host_addr;

	  else if (host_name && strlen (host_name) > 0)
	    {
	      struct hostent *host = gethostbyname (host_name);
	      if (!host)
		error = clib_error_return (0, "unknown host `%s'", config);
	      else
		clib_memcpy (&sa->sin_addr.s_addr, host->h_addr_list[0],
			     host->h_length);
	    }

	  else
	    sa->sin_addr.s_addr = htonl (ip4_default_address);

	  vec_free (host_name);
	  if (error)
	    goto done;
	}
    }

done:
  return error;
}

static clib_error_t *
default_socket_write (clib_socket_t * s)
{
  clib_error_t *err = 0;
  word written = 0;
  word fd = 0;
  word tx_len;

  fd = s->fd;

  /* Map standard input to standard output.
     Typically, fd is a socket for which read/write both work. */
  if (fd == 0)
    fd = 1;

  tx_len = vec_len (s->tx_buffer);
  written = write (fd, s->tx_buffer, tx_len);

  /* Ignore certain errors. */
  if (written < 0 && !unix_error_is_fatal (errno))
    written = 0;

  /* A "real" error occurred. */
  if (written < 0)
    {
      err = clib_error_return_unix (0, "write %wd bytes", tx_len);
      vec_free (s->tx_buffer);
      goto done;
    }

  /* Reclaim the transmitted part of the tx buffer on successful writes. */
  else if (written > 0)
    {
      if (written == tx_len)
	_vec_len (s->tx_buffer) = 0;
      else
	vec_delete (s->tx_buffer, written, 0);
    }

  /* If a non-fatal error occurred AND
     the buffer is full, then we must free it. */
  else if (written == 0 && tx_len > 64 * 1024)
    {
      vec_free (s->tx_buffer);
    }

done:
  return err;
}

static clib_error_t *
default_socket_read (clib_socket_t * sock, int n_bytes)
{
  word fd, n_read;
  u8 *buf;

  /* RX side of socket is down once end of file is reached. */
  if (sock->flags & SOCKET_RX_END_OF_FILE)
    return 0;

  fd = sock->fd;

  n_bytes = clib_max (n_bytes, 4096);
  vec_add2 (sock->rx_buffer, buf, n_bytes);

  if ((n_read = read (fd, buf, n_bytes)) < 0)
    {
      n_read = 0;

      /* Ignore certain errors. */
      if (!unix_error_is_fatal (errno))
	goto non_fatal;

      return clib_error_return_unix (0, "read %d bytes", n_bytes);
    }

  /* Other side closed the socket. */
  if (n_read == 0)
    sock->flags |= SOCKET_RX_END_OF_FILE;

non_fatal:
  _vec_len (sock->rx_buffer) += n_read - n_bytes;

  return 0;
}

static clib_error_t *
default_socket_close (clib_socket_t * s)
{
  if (close (s->fd) < 0)
    return clib_error_return_unix (0, "close");
  return 0;
}

static void
socket_init_funcs (clib_socket_t * s)
{
  if (!s->write_func)
    s->write_func = default_socket_write;
  if (!s->read_func)
    s->read_func = default_socket_read;
  if (!s->close_func)
    s->close_func = default_socket_close;
}

clib_error_t *
clib_socket_init (clib_socket_t * s)
{
  union
  {
    struct sockaddr sa;
    struct sockaddr_un su;
  } addr;
  socklen_t addr_len = 0;
  clib_error_t *error = 0;
  word port;

  error = socket_config (s->config, &addr.sa, &addr_len,
			 (s->flags & SOCKET_IS_SERVER
			  ? INADDR_LOOPBACK : INADDR_ANY));
  if (error)
    goto done;

  socket_init_funcs (s);

  s->fd = socket (addr.sa.sa_family, SOCK_STREAM, 0);
  if (s->fd < 0)
    {
      error = clib_error_return_unix (0, "socket");
      goto done;
    }

  port = 0;
  if (addr.sa.sa_family == PF_INET)
    port = ((struct sockaddr_in *) &addr)->sin_port;

  if (s->flags & SOCKET_IS_SERVER)
    {
      uword need_bind = 1;

      if (addr.sa.sa_family == PF_INET)
	{
	  if (port == 0)
	    {
	      port = find_free_port (s->fd);
	      if (port < 0)
		{
		  error = clib_error_return (0, "no free port");
		  goto done;
		}
	      need_bind = 0;
	    }
	}
      if (addr.sa.sa_family == PF_LOCAL)
	unlink (((struct sockaddr_un *) &addr)->sun_path);

      /* Make address available for multiple users. */
      {
	int v = 1;
	if (setsockopt (s->fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof (v)) < 0)
	  clib_unix_warning ("setsockopt SO_REUSEADDR fails");
      }

      if (need_bind && bind (s->fd, &addr.sa, addr_len) < 0)
	{
	  error = clib_error_return_unix (0, "bind");
	  goto done;
	}

      if (listen (s->fd, 5) < 0)
	{
	  error = clib_error_return_unix (0, "listen");
	  goto done;
	}
    }
  else
    {
      if ((s->flags & SOCKET_NON_BLOCKING_CONNECT)
	  && fcntl (s->fd, F_SETFL, O_NONBLOCK) < 0)
	{
	  error = clib_error_return_unix (0, "fcntl NONBLOCK");
	  goto done;
	}

      if (connect (s->fd, &addr.sa, addr_len) < 0
	  && !((s->flags & SOCKET_NON_BLOCKING_CONNECT) &&
	       errno == EINPROGRESS))
	{
	  error = clib_error_return_unix (0, "connect");
	  goto done;
	}
    }

  return error;

done:
  if (s->fd > 0)
    close (s->fd);
  return error;
}

clib_error_t *
clib_socket_accept (clib_socket_t * server, clib_socket_t * client)
{
  clib_error_t *err = 0;
  socklen_t len = 0;

  memset (client, 0, sizeof (client[0]));

  /* Accept the new socket connection. */
  client->fd = accept (server->fd, 0, 0);
  if (client->fd < 0)
    return clib_error_return_unix (0, "accept");

  /* Set the new socket to be non-blocking. */
  if (fcntl (client->fd, F_SETFL, O_NONBLOCK) < 0)
    {
      err = clib_error_return_unix (0, "fcntl O_NONBLOCK");
      goto close_client;
    }

  /* Get peer info. */
  len = sizeof (client->peer);
  if (getpeername (client->fd, (struct sockaddr *) &client->peer, &len) < 0)
    {
      err = clib_error_return_unix (0, "getpeername");
      goto close_client;
    }

  client->flags = SOCKET_IS_CLIENT;

  socket_init_funcs (client);
  return 0;

close_client:
  close (client->fd);
  return err;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
class="o">->requests, size); if (!tmp) { return VAPI_ENOMEM; } ctx->requests = tmp; memset (ctx->requests, 0, size); /* coverity[MISSING_LOCK] - 177211 requests_mutex is not needed here */ ctx->requests_start = ctx->requests_count = 0; if (chroot_prefix) { VAPI_DBG ("set memory root path `%s'", chroot_prefix); vl_set_memory_root_path ((char *) chroot_prefix); } static char api_map[] = "/vpe-api"; VAPI_DBG ("client api map `%s'", api_map); if ((vl_client_api_map (api_map)) < 0) { return VAPI_EMAP_FAIL; } VAPI_DBG ("connect client `%s'", name); if (vl_client_connect ((char *) name, 0, response_queue_size) < 0) { vl_client_api_unmap (); return VAPI_ECON_FAIL; } #if VAPI_DEBUG_CONNECT VAPI_DBG ("start probing messages"); #endif int rv; int i; for (i = 0; i < __vapi_metadata.count; ++i) { vapi_message_desc_t *m = __vapi_metadata.msgs[i]; u8 scratch[m->name_with_crc_len + 1]; memcpy (scratch, m->name_with_crc, m->name_with_crc_len + 1); u32 id = vl_api_get_msg_index (scratch); if (~0 != id) { if (id > UINT16_MAX) { VAPI_ERR ("Returned vl_msg_id `%u' > UINT16MAX `%u'!", id, UINT16_MAX); rv = VAPI_EINVAL; goto fail; } if (id > ctx->vl_msg_id_max) { vapi_msg_id_t *tmp = realloc (ctx->vl_msg_id_to_vapi_msg_t, sizeof (*ctx->vl_msg_id_to_vapi_msg_t) * (id + 1)); if (!tmp) { rv = VAPI_ENOMEM; goto fail; } ctx->vl_msg_id_to_vapi_msg_t = tmp; ctx->vl_msg_id_max = id; } ctx->vl_msg_id_to_vapi_msg_t[id] = m->id; ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = id; #if VAPI_DEBUG_CONNECT VAPI_DBG ("Message `%s' has vl_msg_id `%u'", m->name_with_crc, (unsigned) id); #endif } else { ctx->vapi_msg_id_t_to_vl_msg_id[m->id] = UINT16_MAX; VAPI_DBG ("Message `%s' not available", m->name_with_crc); } } #if VAPI_DEBUG_CONNECT VAPI_DBG ("finished probing messages"); #endif if (!vapi_is_msg_available (ctx, vapi_msg_id_control_ping) || !vapi_is_msg_available (ctx, vapi_msg_id_control_ping_reply)) { VAPI_ERR ("control ping or control ping reply not available, cannot connect"); rv = VAPI_EINCOMPATIBLE; goto fail; } ctx->mode = mode; ctx->connected = true; return VAPI_OK; fail: vl_client_disconnect (); vl_client_api_unmap (); return rv; } vapi_error_e vapi_disconnect (vapi_ctx_t ctx) { if (!ctx->connected) { return VAPI_EINVAL; } vl_client_disconnect (); vl_client_api_unmap (); #if VAPI_DEBUG_ALLOC vapi_to_be_freed_validate (); #endif ctx->connected = false; return VAPI_OK; } vapi_error_e vapi_get_fd (vapi_ctx_t ctx, int *fd) { return VAPI_ENOTSUP; } vapi_error_e vapi_send (vapi_ctx_t ctx, void *msg) { vapi_error_e rv = VAPI_OK; if (!ctx || !msg || !ctx->connected) { rv = VAPI_EINVAL; goto out; } int tmp; unix_shared_memory_queue_t *q = api_main.shmem_hdr->vl_input_queue; #if VAPI_DEBUG unsigned msgid = be16toh (*(u16 *) msg); if (msgid <= ctx->vl_msg_id_max) { vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid]; if (id < __vapi_metadata.count) { VAPI_DBG ("send msg@%p:%u[%s]", msg, msgid, __vapi_metadata.msgs[id]->name); } else { VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid); } } else { VAPI_DBG ("send msg@%p:%u[UNKNOWN]", msg, msgid); } #endif tmp = unix_shared_memory_queue_add (q, (u8 *) & msg, VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1); if (tmp < 0) { rv = VAPI_EAGAIN; } out: VAPI_DBG ("vapi_send() rv = %d", rv); return rv; } vapi_error_e vapi_send2 (vapi_ctx_t ctx, void *msg1, void *msg2) { vapi_error_e rv = VAPI_OK; if (!ctx || !msg1 || !msg2 || !ctx->connected) { rv = VAPI_EINVAL; goto out; } unix_shared_memory_queue_t *q = api_main.shmem_hdr->vl_input_queue; #if VAPI_DEBUG unsigned msgid1 = be16toh (*(u16 *) msg1); unsigned msgid2 = be16toh (*(u16 *) msg2); const char *name1 = "UNKNOWN"; const char *name2 = "UNKNOWN"; if (msgid1 <= ctx->vl_msg_id_max) { vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid1]; if (id < __vapi_metadata.count) { name1 = __vapi_metadata.msgs[id]->name; } } if (msgid2 <= ctx->vl_msg_id_max) { vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid2]; if (id < __vapi_metadata.count) { name2 = __vapi_metadata.msgs[id]->name; } } VAPI_DBG ("send two: %u[%s], %u[%s]", msgid1, name1, msgid2, name2); #endif int tmp = unix_shared_memory_queue_add2 (q, (u8 *) & msg1, (u8 *) & msg2, VAPI_MODE_BLOCKING == ctx->mode ? 0 : 1); if (tmp < 0) { rv = VAPI_EAGAIN; } out: VAPI_DBG ("vapi_send() rv = %d", rv); return rv; } vapi_error_e vapi_recv (vapi_ctx_t ctx, void **msg, size_t * msg_size) { if (!ctx || !ctx->connected || !msg || !msg_size) { return VAPI_EINVAL; } vapi_error_e rv = VAPI_OK; api_main_t *am = &api_main; uword data; if (am->our_pid == 0) { return VAPI_EINVAL; } unix_shared_memory_queue_t *q = am->vl_input_queue; VAPI_DBG ("doing shm queue sub"); int tmp = unix_shared_memory_queue_sub (q, (u8 *) & data, 0); if (tmp == 0) { #if VAPI_DEBUG_ALLOC vapi_add_to_be_freed ((void *) data); #endif msgbuf_t *msgbuf = (msgbuf_t *) ((u8 *) data - offsetof (msgbuf_t, data)); if (!msgbuf->data_len) { vapi_msg_free (ctx, (u8 *) data); return VAPI_EAGAIN; } *msg = (u8 *) data; *msg_size = ntohl (msgbuf->data_len); #if VAPI_DEBUG unsigned msgid = be16toh (*(u16 *) * msg); if (msgid <= ctx->vl_msg_id_max) { vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[msgid]; if (id < __vapi_metadata.count) { VAPI_DBG ("recv msg@%p:%u[%s]", *msg, msgid, __vapi_metadata.msgs[id]->name); } else { VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid); } } else { VAPI_DBG ("recv msg@%p:%u[UNKNOWN]", *msg, msgid); } #endif } else { rv = VAPI_EAGAIN; } return rv; } vapi_error_e vapi_wait (vapi_ctx_t ctx, vapi_wait_mode_e mode) { return VAPI_ENOTSUP; } static vapi_error_e vapi_dispatch_response (vapi_ctx_t ctx, vapi_msg_id_t id, u32 context, void *msg) { int mrv; if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex))) { VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv)); return VAPI_MUTEX_FAILURE; } int tmp = ctx->requests_start; const int requests_end = vapi_requests_end (ctx); while (ctx->requests[tmp].context != context && tmp != requests_end) { ++tmp; if (tmp == ctx->requests_size) { tmp = 0; } } VAPI_DBG ("dispatch, search from %d, %s at %d", ctx->requests_start, ctx->requests[tmp].context == context ? "matched" : "stopped", tmp); vapi_error_e rv = VAPI_OK; if (ctx->requests[tmp].context == context) { while (ctx->requests_start != tmp) { VAPI_ERR ("No response to req with context=%u", (unsigned) ctx->requests[tmp].context); ctx->requests[ctx->requests_start].callback (ctx, ctx->requests [ctx-> requests_start].callback_ctx, VAPI_ENORESP, true, NULL); memset (&ctx->requests[ctx->requests_start], 0, sizeof (ctx->requests[ctx->requests_start])); ++ctx->requests_start; --ctx->requests_count; if (ctx->requests_start == ctx->requests_size) { ctx->requests_start = 0; } } // now ctx->requests_start == tmp int payload_offset = vapi_get_payload_offset (id); void *payload = ((u8 *) msg) + payload_offset; bool is_last = true; if (ctx->requests[tmp].is_dump) { if (vapi_msg_id_control_ping_reply == id) { payload = NULL; } else { is_last = false; } } if (payload_offset != -1) { rv = ctx->requests[tmp].callback (ctx, ctx->requests[tmp].callback_ctx, VAPI_OK, is_last, payload); } else { /* this is a message without payload, so bend the callback a little */ rv = ((vapi_error_e (*)(vapi_ctx_t, void *, vapi_error_e, bool)) ctx->requests[tmp].callback) (ctx, ctx->requests[tmp].callback_ctx, VAPI_OK, is_last); } if (is_last) { memset (&ctx->requests[ctx->requests_start], 0, sizeof (ctx->requests[ctx->requests_start])); ++ctx->requests_start; --ctx->requests_count; if (ctx->requests_start == ctx->requests_size) { ctx->requests_start = 0; } } VAPI_DBG ("after dispatch, req start = %d, end = %d, count = %d", ctx->requests_start, requests_end, ctx->requests_count); } if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex))) { VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv, strerror (mrv)); abort (); /* this really shouldn't happen */ } return rv; } static vapi_error_e vapi_dispatch_event (vapi_ctx_t ctx, vapi_msg_id_t id, void *msg) { if (ctx->event_cbs[id].cb) { return ctx->event_cbs[id].cb (ctx, ctx->event_cbs[id].ctx, msg); } else if (ctx->generic_cb.cb) { return ctx->generic_cb.cb (ctx, ctx->generic_cb.ctx, id, msg); } else { VAPI_DBG ("No handler/generic handler for msg id %u[%s], message ignored", (unsigned) id, __vapi_metadata.msgs[id]->name); } return VAPI_OK; } bool vapi_msg_is_with_context (vapi_msg_id_t id) { assert (id <= __vapi_metadata.count); return __vapi_metadata.msgs[id]->has_context; } vapi_error_e vapi_dispatch_one (vapi_ctx_t ctx) { VAPI_DBG ("vapi_dispatch_one()"); void *msg; size_t size; vapi_error_e rv = vapi_recv (ctx, &msg, &size); if (VAPI_OK != rv) { VAPI_DBG ("vapi_recv failed with rv=%d", rv); return rv; } u16 vpp_id = be16toh (*(u16 *) msg); if (vpp_id > ctx->vl_msg_id_max) { VAPI_ERR ("Unknown msg ID received, id `%u', out of range <0,%u>", (unsigned) vpp_id, (unsigned) ctx->vl_msg_id_max); vapi_msg_free (ctx, msg); return VAPI_EINVAL; } if (~0 == (unsigned) ctx->vl_msg_id_to_vapi_msg_t[vpp_id]) { VAPI_ERR ("Unknown msg ID received, id `%u' marked as not supported", (unsigned) vpp_id); vapi_msg_free (ctx, msg); return VAPI_EINVAL; } const vapi_msg_id_t id = ctx->vl_msg_id_to_vapi_msg_t[vpp_id]; const size_t expect_size = vapi_get_message_size (id); if (size < expect_size) { VAPI_ERR ("Invalid msg received, unexpected size `%zu' < expected min `%zu'", size, expect_size); vapi_msg_free (ctx, msg); return VAPI_EINVAL; } u32 context; vapi_get_swap_to_host_func (id) (msg); if (vapi_msg_is_with_context (id)) { context = *(u32 *) (((u8 *) msg) + vapi_get_context_offset (id)); /* is this a message originating from VAPI? */ VAPI_DBG ("dispatch, context is %x", context); if (context & context_counter_mask) { rv = vapi_dispatch_response (ctx, id, context, msg); goto done; } } rv = vapi_dispatch_event (ctx, id, msg); done: vapi_msg_free (ctx, msg); return rv; } vapi_error_e vapi_dispatch (vapi_ctx_t ctx) { vapi_error_e rv = VAPI_OK; while (!vapi_requests_empty (ctx)) { rv = vapi_dispatch_one (ctx); if (VAPI_OK != rv) { return rv; } } return rv; } void vapi_set_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id, vapi_event_cb callback, void *callback_ctx) { vapi_event_cb_with_ctx *c = &ctx->event_cbs[id]; c->cb = callback; c->ctx = callback_ctx; } void vapi_clear_event_cb (vapi_ctx_t ctx, vapi_msg_id_t id) { vapi_set_event_cb (ctx, id, NULL, NULL); } void vapi_set_generic_event_cb (vapi_ctx_t ctx, vapi_generic_event_cb callback, void *callback_ctx) { ctx->generic_cb.cb = callback; ctx->generic_cb.ctx = callback_ctx; } void vapi_clear_generic_event_cb (vapi_ctx_t ctx) { ctx->generic_cb.cb = NULL; ctx->generic_cb.ctx = NULL; } u16 vapi_lookup_vl_msg_id (vapi_ctx_t ctx, vapi_msg_id_t id) { assert (id < __vapi_metadata.count); return ctx->vapi_msg_id_t_to_vl_msg_id[id]; } int vapi_get_client_index (vapi_ctx_t ctx) { return api_main.my_client_index; } bool vapi_is_nonblocking (vapi_ctx_t ctx) { return (VAPI_MODE_NONBLOCKING == ctx->mode); } size_t vapi_get_max_request_count (vapi_ctx_t ctx) { return ctx->requests_size - 1; } int vapi_get_payload_offset (vapi_msg_id_t id) { assert (id < __vapi_metadata.count); return __vapi_metadata.msgs[id]->payload_offset; } void (*vapi_get_swap_to_host_func (vapi_msg_id_t id)) (void *msg) { assert (id < __vapi_metadata.count); return __vapi_metadata.msgs[id]->swap_to_host; } void (*vapi_get_swap_to_be_func (vapi_msg_id_t id)) (void *msg) { assert (id < __vapi_metadata.count); return __vapi_metadata.msgs[id]->swap_to_be; } size_t vapi_get_message_size (vapi_msg_id_t id) { assert (id < __vapi_metadata.count); return __vapi_metadata.msgs[id]->size; } size_t vapi_get_context_offset (vapi_msg_id_t id) { assert (id < __vapi_metadata.count); return __vapi_metadata.msgs[id]->context_offset; } vapi_msg_id_t vapi_register_msg (vapi_message_desc_t * msg) { int i = 0; for (i = 0; i < __vapi_metadata.count; ++i) { if (!strcmp (msg->name_with_crc, __vapi_metadata.msgs[i]->name_with_crc)) { /* this happens if somebody is linking together several objects while * using the static inline headers, just fill in the already * assigned id here so that all the objects are in sync */ msg->id = __vapi_metadata.msgs[i]->id; return msg->id; } } vapi_msg_id_t id = __vapi_metadata.count; ++__vapi_metadata.count; __vapi_metadata.msgs = realloc (__vapi_metadata.msgs, sizeof (*__vapi_metadata.msgs) * __vapi_metadata.count); __vapi_metadata.msgs[id] = msg; size_t s = strlen (msg->name_with_crc); if (s > __vapi_metadata.max_len_name_with_crc) { __vapi_metadata.max_len_name_with_crc = s; } msg->id = id; return id; } vapi_error_e vapi_producer_lock (vapi_ctx_t ctx) { int mrv; if (0 != (mrv = pthread_mutex_lock (&ctx->requests_mutex))) { VAPI_DBG ("pthread_mutex_lock() failed, rv=%d:%s", mrv, strerror (mrv)); (void) mrv; /* avoid warning if the above debug is not enabled */ return VAPI_MUTEX_FAILURE; } return VAPI_OK; } vapi_error_e vapi_producer_unlock (vapi_ctx_t ctx) { int mrv; if (0 != (mrv = pthread_mutex_unlock (&ctx->requests_mutex))) { VAPI_DBG ("pthread_mutex_unlock() failed, rv=%d:%s", mrv, strerror (mrv)); (void) mrv; /* avoid warning if the above debug is not enabled */ return VAPI_MUTEX_FAILURE; } return VAPI_OK; } size_t vapi_get_message_count () { return __vapi_metadata.count; } const char * vapi_get_msg_name (vapi_msg_id_t id) { return __vapi_metadata.msgs[id]->name; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */