FD.io VPP  v17.10-9-gd594711
Vector Packet Processing
builtin_server.c
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2015-2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
20 
21 typedef struct
22 {
23  /*
24  * Server app parameters
25  */
27  unix_shared_memory_queue_t *vl_input_queue; /**< Sever's event queue */
28 
29  u32 app_index; /**< Server app index */
30  u32 my_client_index; /**< API client handle */
31  u32 node_index; /**< process node index for evnt scheduling */
32 
33  /*
34  * Config params
35  */
36  u8 no_echo; /**< Don't echo traffic */
37  u32 fifo_size; /**< Fifo size */
38  u32 rcv_buffer_size; /**< Rcv buffer size */
39  u32 prealloc_fifos; /**< Preallocate fifos */
40  u32 private_segment_count; /**< Number of private segments */
41  u32 private_segment_size; /**< Size of private segments */
42  char *server_uri; /**< Server URI */
43 
44  /*
45  * Test state
46  */
47  u8 **rx_buf; /**< Per-thread RX buffer */
50 
53 
55 
56 int
58 {
60 
61  bsm->vpp_queue[s->thread_index] =
62  session_manager_get_vpp_event_queue (s->thread_index);
63  s->session_state = SESSION_STATE_READY;
64  bsm->byte_index = 0;
65  vec_validate (bsm->rx_retries[s->thread_index], s->session_index);
66  bsm->rx_retries[s->thread_index][s->session_index] = 0;
67  return 0;
68 }
69 
70 void
72 {
74  vnet_disconnect_args_t _a, *a = &_a;
75 
76  a->handle = stream_session_handle (s);
77  a->app_index = bsm->app_index;
79 }
80 
81 void
83 {
84  clib_warning ("Reset session %U", format_stream_session, s, 2);
86 }
87 
88 
89 int
90 builtin_session_connected_callback (u32 app_index, u32 api_context,
91  stream_session_t * s, u8 is_fail)
92 {
93  clib_warning ("called...");
94  return -1;
95 }
96 
97 int
99  const u8 * seg_name, u32 seg_size)
100 {
101  clib_warning ("called...");
102  return -1;
103 }
104 
105 int
106 builtin_redirect_connect_callback (u32 client_index, void *mp)
107 {
108  clib_warning ("called...");
109  return -1;
110 }
111 
112 void
113 test_bytes (builtin_server_main_t * bsm, int actual_transfer)
114 {
115  int i;
116  u32 my_thread_id = vlib_get_thread_index ();
117 
118  for (i = 0; i < actual_transfer; i++)
119  {
120  if (bsm->rx_buf[my_thread_id][i] != ((bsm->byte_index + i) & 0xff))
121  {
122  clib_warning ("at %lld expected %d got %d", bsm->byte_index + i,
123  (bsm->byte_index + i) & 0xff,
124  bsm->rx_buf[my_thread_id][i]);
125  }
126  }
127  bsm->byte_index += actual_transfer;
128 }
129 
130 /*
131  * If no-echo, just read the data and be done with it
132  */
133 int
135 {
137  u32 my_thread_id = vlib_get_thread_index ();
138  int actual_transfer;
139  svm_fifo_t *rx_fifo;
140 
141  rx_fifo = s->server_rx_fifo;
142 
143  do
144  {
145  actual_transfer =
147  bsm->rx_buf[my_thread_id]);
148  }
149  while (actual_transfer > 0);
150  return 0;
151 }
152 
153 int
155 {
156  u32 n_written, max_dequeue, max_enqueue, max_transfer;
157  int actual_transfer;
158  svm_fifo_t *tx_fifo, *rx_fifo;
160  session_fifo_event_t evt;
161  static int serial_number = 0;
162  u32 thread_index = vlib_get_thread_index ();
163 
164  ASSERT (s->thread_index == thread_index);
165 
166  rx_fifo = s->server_rx_fifo;
167  tx_fifo = s->server_tx_fifo;
168 
169  ASSERT (rx_fifo->master_thread_index == thread_index);
170  ASSERT (tx_fifo->master_thread_index == thread_index);
171 
172  max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
173  max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo);
174 
175  if (PREDICT_FALSE (max_dequeue == 0))
176  return 0;
177 
178  /* Number of bytes we're going to copy */
179  max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue;
180 
181  /* No space in tx fifo */
182  if (PREDICT_FALSE (max_transfer == 0))
183  {
184  /* XXX timeout for session that are stuck */
185 
186  rx_event:
187  /* Program self-tap to retry */
188  if (svm_fifo_set_event (rx_fifo))
189  {
191  evt.fifo = rx_fifo;
192  evt.event_type = FIFO_EVENT_BUILTIN_RX;
193  evt.event_id = 0;
194 
195  q = bsm->vpp_queue[thread_index];
196  if (PREDICT_FALSE (q->cursize == q->maxsize))
197  clib_warning ("out of event queue space");
198  else if (unix_shared_memory_queue_add (q, (u8 *) & evt, 0))
199  clib_warning ("failed to enqueue self-tap");
200 
201  if (bsm->rx_retries[thread_index][s->session_index] == 500000)
202  {
203  clib_warning ("session stuck: %U", format_stream_session, s, 2);
204  }
205  if (bsm->rx_retries[thread_index][s->session_index] < 500001)
206  bsm->rx_retries[thread_index][s->session_index]++;
207  }
208 
209  return 0;
210  }
211 
212  _vec_len (bsm->rx_buf[thread_index]) = max_transfer;
213 
214  actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_transfer,
215  bsm->rx_buf[thread_index]);
216  ASSERT (actual_transfer == max_transfer);
217 
218 // test_bytes (bsm, actual_transfer);
219 
220  /*
221  * Echo back
222  */
223 
224  n_written = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer,
225  bsm->rx_buf[thread_index]);
226 
227  if (n_written != max_transfer)
228  clib_warning ("short trout!");
229 
230  if (svm_fifo_set_event (tx_fifo))
231  {
232  /* Fabricate TX event, send to vpp */
233  evt.fifo = tx_fifo;
234  evt.event_type = FIFO_EVENT_APP_TX;
235  evt.event_id = serial_number++;
236 
237  if (unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
238  (u8 *) & evt,
239  0 /* do wait for mutex */ ))
240  clib_warning ("failed to enqueue tx evt");
241  }
242 
243  if (PREDICT_FALSE (n_written < max_dequeue))
244  goto rx_event;
245 
246  return 0;
247 }
248 
250  .session_accept_callback = builtin_session_accept_callback,
251  .session_disconnect_callback = builtin_session_disconnect_callback,
252  .session_connected_callback = builtin_session_connected_callback,
253  .add_segment_callback = builtin_add_segment_callback,
254  .redirect_connect_callback = builtin_redirect_connect_callback,
255  .builtin_server_rx_callback = builtin_server_rx_callback,
256  .session_reset_callback = builtin_session_reset_callback
257 };
258 
259 /* Abuse VPP's input queue */
260 static int
262 {
264  api_main_t *am = &api_main;
266 
267  shmem_hdr = am->shmem_hdr;
268  bsm->vl_input_queue = shmem_hdr->vl_input_queue;
269  bsm->my_client_index =
270  vl_api_memclnt_create_internal ("tcp_test_server", bsm->vl_input_queue);
271  return 0;
272 }
273 
274 static int
276 {
278  u8 segment_name[128];
280  vnet_app_attach_args_t _a, *a = &_a;
281 
282  memset (a, 0, sizeof (*a));
283  memset (options, 0, sizeof (options));
284 
285  if (bsm->no_echo)
286  builtin_session_cb_vft.builtin_server_rx_callback =
288  else
289  builtin_session_cb_vft.builtin_server_rx_callback =
291  a->api_client_index = bsm->my_client_index;
292  a->session_cb_vft = &builtin_session_cb_vft;
293  a->options = options;
294  a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20;
295  a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = bsm->fifo_size;
296  a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = bsm->fifo_size;
299  a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
300  bsm->prealloc_fifos ? bsm->prealloc_fifos : 1;
301 
302  a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
303 
304  a->segment_name = segment_name;
305  a->segment_name_length = ARRAY_LEN (segment_name);
306 
307  if (vnet_application_attach (a))
308  {
309  clib_warning ("failed to attach server");
310  return -1;
311  }
312  bsm->app_index = a->app_index;
313  return 0;
314 }
315 
316 static int
318 {
320  vnet_bind_args_t _a, *a = &_a;
321  memset (a, 0, sizeof (*a));
322  a->app_index = bsm->app_index;
323  a->uri = bsm->server_uri;
324  return vnet_bind_uri (a);
325 }
326 
327 static int
329 {
332  u32 num_threads;
333  int i;
334 
335  if (bsm->my_client_index == (u32) ~ 0)
336  {
337  if (create_api_loopback (vm))
338  {
339  clib_warning ("failed to create api loopback");
340  return -1;
341  }
342  }
343 
344  num_threads = 1 /* main thread */ + vtm->n_threads;
345  vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
346  vec_validate (bsm->rx_buf, num_threads - 1);
347  vec_validate (bsm->rx_retries, num_threads - 1);
348 
349  for (i = 0; i < num_threads; i++)
350  vec_validate (bsm->rx_buf[i], bsm->rcv_buffer_size);
351 
352  if (server_attach ())
353  {
354  clib_warning ("failed to attach server");
355  return -1;
356  }
357  if (server_listen ())
358  {
359  clib_warning ("failed to start listening");
360  return -1;
361  }
362  return 0;
363 }
364 
365 static clib_error_t *
367  vlib_cli_command_t * cmd)
368 {
370  u8 server_uri_set = 0;
371  int rv;
372  u64 tmp;
373 
374  bsm->no_echo = 0;
375  bsm->fifo_size = 64 << 10;
376  bsm->rcv_buffer_size = 128 << 10;
377  bsm->prealloc_fifos = 0;
378  bsm->private_segment_count = 0;
379  bsm->private_segment_size = 0;
380  vec_free (bsm->server_uri);
381 
383  {
384  if (unformat (input, "no-echo"))
385  bsm->no_echo = 1;
386  else if (unformat (input, "fifo-size %d", &bsm->fifo_size))
387  bsm->fifo_size <<= 10;
388  else if (unformat (input, "rcv-buf-size %d", &bsm->rcv_buffer_size))
389  ;
390  else if (unformat (input, "prealloc-fifos %d", &bsm->prealloc_fifos))
391  ;
392  else if (unformat (input, "private-segment-count %d",
393  &bsm->private_segment_count))
394  ;
395  else if (unformat (input, "private-segment-size %U",
396  unformat_memory_size, &tmp))
397  {
398  if (tmp >= 0x100000000ULL)
399  return clib_error_return
400  (0, "private segment size %lld (%llu) too large", tmp, tmp);
401  bsm->private_segment_size = tmp;
402  }
403  else if (unformat (input, "uri %s", &bsm->server_uri))
404  server_uri_set = 1;
405  else
406  return clib_error_return (0, "unknown input `%U'",
407  format_unformat_error, input);
408  }
409 
410  vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
411 
412  if (!server_uri_set)
413  bsm->server_uri = (char *) format (0, "tcp://0.0.0.0/1234%c", 0);
414 
415  rv = server_create (vm);
416  switch (rv)
417  {
418  case 0:
419  break;
420  default:
421  return clib_error_return (0, "server_create returned %d", rv);
422  }
423 
424  return 0;
425 }
426 
427 /* *INDENT-OFF* */
428 VLIB_CLI_COMMAND (server_create_command, static) =
429 {
430  .path = "test tcp server",
431  .short_help = "test tcp server [no echo][fifo-size <mbytes>] "
432  "[rcv-buf-size <bytes>][prealloc-fifos <count>]"
433  "[private-segment-count <count>][private-segment-size <bytes[m|g]>]"
434  "[uri <tcp://ip/port>]",
435  .function = server_create_command_fn,
436 };
437 /* *INDENT-ON* */
438 
439 clib_error_t *
441 {
443  bsm->my_client_index = ~0;
444  return 0;
445 }
446 
448 
449 /*
450 * fd.io coding-style-patch-verification: ON
451 *
452 * Local Variables:
453 * eval: (c-set-style "gnu")
454 * End:
455 */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
void test_bytes(builtin_server_main_t *bsm, int actual_transfer)
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
u32 rcv_buffer_size
Rcv buffer size.
unix_shared_memory_queue_t ** vpp_queue
a
Definition: bitmap.h:516
u32 private_segment_count
Number of private segments.
int vnet_bind_uri(vnet_bind_args_t *a)
static clib_error_t * server_create_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
unix_shared_memory_queue_t * vl_input_queue
Definition: api_common.h:68
int builtin_redirect_connect_callback(u32 client_index, void *mp)
u8 ** rx_buf
Per-thread RX buffer.
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
Definition: svm_fifo.h:106
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
u32 prealloc_fifos
Preallocate fifos.
builtin_server_main_t builtin_server_main
struct _svm_fifo svm_fifo_t
u8 no_echo
Don&#39;t echo traffic.
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
struct _vnet_disconnect_args_t vnet_disconnect_args_t
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
Definition: svm_fifo.h:100
struct _stream_session_cb_vft session_cb_vft_t
unix_shared_memory_queue_t * vl_input_queue
Sever&#39;s event queue.
#define clib_error_return(e, args...)
Definition: error.h:99
static int server_attach()
char * server_uri
Server URI.
unsigned long u64
Definition: types.h:89
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:241
void stream_session_cleanup(stream_session_t *s)
Cleanup transport and session state.
Definition: session.c:776
struct _stream_session_t stream_session_t
static int create_api_loopback(vlib_main_t *vm)
struct _vnet_app_attach_args_t vnet_app_attach_args_t
vl_shmem_hdr_t * shmem_hdr
int unix_shared_memory_queue_add(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
void builtin_session_disconnect_callback(stream_session_t *s)
struct _unformat_input_t unformat_input_t
u32 my_client_index
API client handle.
#define PREDICT_FALSE(x)
Definition: clib.h:97
clib_error_t * vnet_session_enable_disable(vlib_main_t *vm, u8 is_en)
Definition: session.c:928
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:182
static int server_create(vlib_main_t *vm)
u32 fifo_size
Fifo size.
static u8 svm_fifo_set_event(svm_fifo_t *f)
Sets fifo event flag.
Definition: svm_fifo.h:123
static unix_shared_memory_queue_t * session_manager_get_vpp_event_queue(u32 thread_index)
Definition: session.h:345
api_main_t api_main
Definition: api_shared.c:35
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vlib_main
int builtin_add_segment_callback(u32 client_index, const u8 *seg_name, u32 seg_size)
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:283
u8 * format_stream_session(u8 *s, va_list *args)
Format stream session as per the following format.
Definition: session_cli.c:52
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
#define clib_warning(format, args...)
Definition: error.h:59
u32 private_segment_size
Size of private segments.
u32 vl_api_memclnt_create_internal(char *, unix_shared_memory_queue_t *)
Definition: memory_vlib.c:152
int vnet_disconnect_session(vnet_disconnect_args_t *a)
#define ARRAY_LEN(x)
Definition: clib.h:59
int svm_fifo_enqueue_nowait(svm_fifo_t *f, u32 max_bytes, u8 *copy_from_here)
Definition: svm_fifo.c:533
int builtin_session_accept_callback(stream_session_t *s)
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
int vnet_application_attach(vnet_app_attach_args_t *a)
Attaches application.
clib_error_t * builtin_tcp_server_main_init(vlib_main_t *vm)
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
int builtin_server_rx_callback(stream_session_t *s)
int builtin_server_rx_callback_no_echo(stream_session_t *s)
static u64 stream_session_handle(stream_session_t *s)
Definition: session.h:237
u32 node_index
process node index for evnt scheduling
unsigned char u8
Definition: types.h:56
void builtin_session_reset_callback(stream_session_t *s)
static session_cb_vft_t builtin_session_cb_vft
static int server_listen()
unformat_function_t unformat_memory_size
Definition: format.h:294
int builtin_session_connected_callback(u32 app_index, u32 api_context, stream_session_t *s, u8 is_fail)
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
u32 app_index
Server app index.
int svm_fifo_dequeue_nowait(svm_fifo_t *f, u32 max_bytes, u8 *copy_here)
Definition: svm_fifo.c:690
struct _vnet_bind_args_t vnet_bind_args_t
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
struct _unix_shared_memory_queue unix_shared_memory_queue_t