summaryrefslogtreecommitdiffstats
path: root/src/vnet/bfd/bfd_main.h
blob: a7540004dcc92f7296e1f18b89032e017f39880b (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Copyright (c) 2011-2016 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.
 */
/**
 * @file
 * @brief BFD global declarations
 */
#ifndef __included_bfd_main_h__
#define __included_bfd_main_h__

#include <vnet/vnet.h>
#include <vnet/bfd/bfd_protocol.h>
#include <vnet/bfd/bfd_udp.h>
#include <vlib/log.h>
#include <vppinfra/os.h>
#include <vppinfra/tw_timer_1t_3w_1024sl_ov.h>

#define foreach_bfd_mode(F) \
  F (asynchronous)          \
  F (demand)

typedef enum
{
#define F(x) BFD_MODE_##x,
  foreach_bfd_mode (F)
#undef F
} bfd_mode_e;

typedef struct
{
  /** global configuration key ID */
  u32 conf_key_id;

  /** keeps track of how many sessions reference this key */
  u32 use_count;

  /**
   * key data directly usable for bfd purposes - already padded with zeroes
   * (so we don't need the actual length)
   */
  u8 key[20];

  /** authentication type for this key */
  bfd_auth_type_e auth_type;
} bfd_auth_key_t;

#define foreach_bfd_poll_state(F) \
  F (NOT_NEEDED)                  \
  F (NEEDED)                      \
  F (IN_PROGRESS)                 \
  F (IN_PROGRESS_AND_QUEUED)

typedef enum
{
#define F(x) BFD_POLL_##x,
  foreach_bfd_poll_state (F)
#undef F
} bfd_poll_state_e;

/**
 * hop types
 */
#define foreach_bfd_hop(F)                     \
  F (SINGLE, "single")                         \
  F (MULTI,  "multi")                          \

typedef enum
{
#define F(sym, str) BFD_HOP_TYPE_##sym,
  foreach_bfd_hop (F)
#undef F
} bfd_hop_type_e;

typedef struct bfd_session_s
{
  /** index in bfd_main.sessions pool */
  u32 bs_idx;

  /** session state */
  bfd_state_e local_state;

  /** remote session state */
  bfd_state_e remote_state;

  /** BFD hop type */
  bfd_hop_type_e hop_type;

  /** local diagnostics */
  bfd_diag_code_e local_diag;

  /** remote diagnostics */
  bfd_diag_code_e remote_diag;

  /** local discriminator */
  u32 local_discr;

  /** remote discriminator */
  u32 remote_discr;

  /** configured desired min tx interval (microseconds) */
  u32 config_desired_min_tx_usec;

  /** configured desired min tx interval (nsec) */
  u64 config_desired_min_tx_nsec;

  /** effective desired min tx interval (nsec) */
  u64 effective_desired_min_tx_nsec;

  /** configured required min rx interval (microseconds) */
  u32 config_required_min_rx_usec;

  /** configured required min rx interval (nsec) */
  u64 config_required_min_rx_nsec;

  /** effective required min rx interval (nsec) */
  u64 effective_required_min_rx_nsec;

  /** remote min rx interval (microseconds) */
  u64 remote_min_rx_usec;

  /** remote min rx interval (nsec) */
  u64 remote_min_rx_nsec;

  /** remote min echo rx interval (microseconds) */
  u64 remote_min_echo_rx_usec;

  /** remote min echo rx interval (nsec) */
  u64 remote_min_echo_rx_nsec;

  /** remote desired min tx interval (nsec) */
  u64 remote_desired_min_tx_nsec;

  /** configured detect multiplier */
  u8 local_detect_mult;

  /** 1 if remote system sets demand mode, 0 otherwise */
  u8 remote_demand;

  /** remote detect multiplier */
  u8 remote_detect_mult;

  /** 1 is echo function is active, 0 otherwise */
  u8 echo;

  /** next event time in nsec for this session (0 if no event) */
  u64 event_time_nsec;

  /** timing wheel internal id used to manipulate timer (if set) */
  u32 tw_id;

  /** transmit interval */
  u64 transmit_interval_nsec;

  /** next time at which to transmit a packet */
  u64 tx_timeout_nsec;

  /** timestamp of last packet transmitted */
  u64 last_tx_nsec;

  /** timestamp of last packet received */
  u64 last_rx_nsec;

  /** transmit interval for echo packets */
  u64 echo_transmit_interval_nsec;

  /** next time at which to transmit echo packet */
  u64 echo_tx_timeout_nsec;

  /** timestamp of last echo packet transmitted */
  u64 echo_last_tx_nsec;

  /** timestamp of last echo packet received */
  u64 echo_last_rx_nsec;

  /** secret used for calculating/checking checksum of echo packets */
  u32 echo_secret;

  /** detection time */
  u64 detection_time_nsec;

  /** state info regarding poll sequence */
  bfd_poll_state_e poll_state;

  /**
   * helper for delayed poll sequence - marks either start of running poll
   * sequence or timeout, after which we can start the next poll sequnce
   */
  u64 poll_state_start_or_timeout_nsec;

  /** authentication information */
  struct
  {
    /** current key in use */
    bfd_auth_key_t *curr_key;

    /**
     * set to next key to use if delayed switch is enabled - in that case
     * the key is switched when first incoming packet is signed with next_key
     */
    bfd_auth_key_t *next_key;

    /** sequence number incremented occasionally or always (if meticulous) */
    u32 local_seq_number;

    /** remote sequence number */
    u32 remote_seq_number;

    /** set to 1 if remote sequence number is known */
    u8 remote_seq_number_known;

    /** current key ID sent out in bfd packet */
    u8 curr_bfd_key_id;

    /** key ID to use when switched to next_key */
    u8 next_bfd_key_id;

    /**
     * set to 1 if delayed action is pending, which might be activation
     * of authentication, change of key or deactivation
     */
    u8 is_delayed;
  } auth;

  /** transport type for this session */
  bfd_transport_e transport;

  /** union of transport-specific data */
  union
  {
    bfd_udp_session_t udp;
  };
} bfd_session_t;

/**
 * listener events
 */
#define foreach_bfd_listen_event(F)            \
  F (CREATE, "sesion-created")                 \
  F (UPDATE, "session-updated")                \
  F (DELETE, "session-deleted")

typedef enum
{
#define F(sym, str) BFD_LISTEN_EVENT_##sym,
  foreach_bfd_listen_event (F)
#undef F
} bfd_listen_event_e;

/**
 * session nitification call back function type
 */
typedef void (*bfd_notify_fn_t) (bfd_listen_event_e, const bfd_session_t *);

typedef struct
{
  /** lock to protect data structures */
  clib_spinlock_t lock;
  int lock_recursion_count;
  uword owner_thread_index;

  /** Number of event wakeup RPCs in flight. Should be 0 or 1 */
  int bfd_process_wakeup_events_in_flight;

  /** The timestamp of last wakeup event being sent */
  u64 bfd_process_wakeup_event_start_nsec;

  /** The time it took the last wakeup event to make it to handling */
  u64 bfd_process_wakeup_event_delay_nsec;

  /** When the bfd process is supposed to wake up next */
  u64 bfd_process_next_wakeup_nsec;

  /** pool of bfd sessions context data */
  bfd_session_t *sessions;

  /** timing wheel for scheduling timeouts */
    TWT (tw_timer_wheel) wheel;

  /** hashmap - bfd session by discriminator */
  u32 *session_by_disc;

  /** background process node index */
  u32 bfd_process_node_index;

  /** convenience variables */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;

  /** how many nanoseconds is one timing wheel tick */
  u64 nsec_per_tw_tick;

  /** default desired min tx in nsec */
  u64 default_desired_min_tx_nsec;

  /** minimum required min rx while echo function is active - nsec */
  u64 min_required_min_rx_while_echo_nsec;

  /** for generating random numbers */
  u32 random_seed;

  /** pool of authentication keys */
  bfd_auth_key_t *auth_keys;

  /** hashmap - index in pool auth_keys by conf_key_id */
  u32 *auth_key_by_conf_key_id;

  /** vector of callback notification functions */
  bfd_notify_fn_t *listeners;

  /** log class */
  vlib_log_class_t log_class;
} bfd_main_t;

extern bfd_main_t bfd_main;

/** Packet counters */
#define foreach_bfd_error(F)               \
  F (NONE, "good bfd packets (processed)") \
  F (BAD, "invalid bfd packets")           \
  F (DISABLED, "bfd packets received on disabled interfaces")

typedef enum
{
#define F(sym, str) BFD_ERROR_##sym,
  foreach_bfd_error (F)
#undef F
    BFD_N_ERROR,
} bfd_error_t;

/** bfd packet trace capture */
typedef struct
{
  u32 len;
  u8 data[400];
} bfd_input_trace_t;

typedef enum
{
  BFD_EVENT_RESCHEDULE = 1,
  BFD_EVENT_NEW_SESSION,
  BFD_EVENT_CONFIG_CHANGED,
} bfd_process_event_e;

/* *INDENT-OFF* */
/** echo packet structure */
typedef CLIB_PACKED (struct {
  /** local discriminator */
  u32 discriminator;
  /** expire time of this packet - nsec */
  u64 expire_time_nsec;
  /** checksum - based on discriminator, local secret and expire time */
  u64 checksum;
}) bfd_echo_pkt_t;
/* *INDENT-ON* */

static inlin
}
/*
 * Copyright (c) 2017-2019 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 <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <time.h>
#include <arpa/inet.h>
#include <vcl/vcl_test.h>
#include <pthread.h>

typedef struct
{
  vcl_test_session_t *sessions;
  uint32_t n_sessions;
  uint32_t wrk_index;
  fd_set wr_fdset;
  fd_set rd_fdset;
  int max_fd_index;
  pthread_t thread_handle;
  vcl_test_cfg_t cfg;
} vcl_test_client_worker_t;

typedef struct
{
  vcl_test_client_worker_t *workers;
  vppcom_endpt_t server_endpt;
  uint32_t cfg_seq_num;
  vcl_test_session_t ctrl_session;
  vcl_test_session_t *sessions;
  uint8_t dump_cfg;
  vcl_test_t post_test;
  uint32_t proto;
  uint32_t n_workers;
  volatile int active_workers;
  struct sockaddr_storage server_addr;
} vcl_test_client_main_t;

static __thread int __wrk_index = 0;

vcl_test_client_main_t vcl_client_main;

#define vtc_min(a, b) (a < b ? a : b)
#define vtc_max(a, b) (a > b ? a : b)

static int
vtc_cfg_sync (vcl_test_session_t * ts)
{
  vcl_test_client_main_t *vcm = &vcl_client_main;
  vcl_test_cfg_t *rx_cfg = (vcl_test_cfg_t *) ts->rxbuf;
  int rx_bytes, tx_bytes;

  vt_atomic_add (&ts->cfg.seq_num, 1);
  if (ts->cfg.verbose)
    {
      vtinf ("(fd %d): Sending config to server.", ts->fd);
      vcl_test_cfg_dump (&ts->cfg, 1 /* is_client */ );
    }
  tx_bytes = vcl_test_write (ts->fd, (uint8_t *) & ts->cfg,
			     sizeof (ts->cfg), NULL, ts->cfg.verbose);
  if (tx_bytes < 0)
    {
      vtwrn ("(fd %d): write test cfg failed (%d)!", ts->fd, tx_bytes);
      return tx_bytes;
    }

  rx_bytes = vcl_test_read (ts->fd, (uint8_t *) ts->rxbuf,
			    sizeof (vcl_test_cfg_t), NULL);
  if (rx_bytes < 0)
    return rx_bytes;

  if (rx_cfg->magic != VCL_TEST_CFG_CTRL_MAGIC)
    {
      vtwrn ("(fd %d): Bad server reply cfg -- aborting!", ts->fd);
      return -1;
    }
  if ((rx_bytes != sizeof (vcl_test_cfg_t))
      || !vcl_test_cfg_verify (rx_cfg, &ts->cfg))
    {
      vtwrn ("(fd %d): Invalid config received from server!", ts->fd);
      if (rx_bytes != sizeof (vcl_test_cfg_t))
	{
	  vtinf ("\tRx bytes %d != cfg size %lu", rx_bytes,
		 sizeof (vcl_test_cfg_t));
	}
      else
	{
	  vcl_test_cfg_dump (rx_cfg, 1 /* is_client */ );
	  vtinf ("(fd %d): Valid config sent to server.", ts->fd);
	  vcl_test_cfg_dump (&ts->cfg, 1 /* is_client */ );
	}
      return -1;
    }
  if (ts->cfg.verbose)
    {
      vtinf ("(fd %d): Got config back from server.", ts->fd);
      vcl_test_cfg_dump (rx_cfg, 1 /* is_client */ );
    }

  return 0;
}

static int
vtc_connect_test_sessions (vcl_test_client_worker_t * wrk)
{
  vcl_test_client_main_t *vcm = &vcl_client_main;
  vcl_test_session_t *ts;
  uint32_t n_test_sessions;
  int i
essions >= n_test_sessions) goto done; if (wrk->n_sessions) wrk->sessions = realloc (wrk->sessions, n_test_sessions * sizeof (vcl_test_session_t)); else wrk->sessions = calloc (n_test_sessions, sizeof (vcl_test_session_t)); if (!wrk->sessions) { vterr ("failed to alloc sessions", -errno); return errno; } for (i = 0; i < n_test_sessions; i++) { ts = &wrk->sessions[i]; ts->fd = vppcom_session_create (vcm->proto, 1 /* is_nonblocking */ ); if (ts->fd < 0) { vterr ("vppcom_session_create()", ts->fd); return ts->fd; } rv = vppcom_session_connect (ts->fd, &vcm->server_endpt); if (rv < 0) { vterr ("vppcom_session_connect()", rv); return rv; } vtinf ("Test session %d (fd %d) connected.", i, ts->fd); } wrk->n_sessions = n_test_sessions; done: vtinf ("All test sessions (%d) connected!", n_test_sessions); return 0; } static int vtc_worker_test_setup (vcl_test_client_worker_t * wrk) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; vcl_test_cfg_t *cfg = &wrk->cfg; vcl_test_session_t *ts; uint32_t sidx; int i, j; FD_ZERO (&wrk->wr_fdset); FD_ZERO (&wrk->rd_fdset); for (i = 0; i < cfg->num_test_sessions; i++) { ts = &wrk->sessions[i]; ts->cfg = wrk->cfg; vcl_test_session_buf_alloc (ts); switch (cfg->test) { case VCL_TEST_TYPE_ECHO: memcpy (ts->txbuf, ctrl->txbuf, cfg->total_bytes); break; case VCL_TEST_TYPE_UNI: case VCL_TEST_TYPE_BI: for (j = 0; j < ts->txbuf_size; j++) ts->txbuf[j] = j & 0xff; break; } FD_SET (vppcom_session_index (ts->fd), &wrk->wr_fdset); FD_SET (vppcom_session_index (ts->fd), &wrk->rd_fdset); sidx = vppcom_session_index (ts->fd); wrk->max_fd_index = vtc_max (sidx, wrk->max_fd_index); } wrk->max_fd_index += 1; return 0; } static int vtc_worker_init (vcl_test_client_worker_t * wrk) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; vcl_test_cfg_t *cfg = &wrk->cfg; vcl_test_session_t *ts; uint32_t i, n; int rv, nbytes; __wrk_index = wrk->wrk_index; vtinf ("Initializing worker %u ...", wrk->wrk_index); if (wrk->wrk_index) { if (vppcom_worker_register ()) { vtwrn ("failed to register worker"); return -1; } vt_atomic_add (&vcm->active_workers, 1); } rv = vtc_connect_test_sessions (wrk); if (rv) { vterr ("vtc_connect_test_sessions ()", rv); return rv; } if (vtc_worker_test_setup (wrk)) return -1; vtinf ("Sending config to server on all sessions ..."); for (n = 0; n < cfg->num_test_sessions; n++) { ts = &wrk->sessions[n]; if (vtc_cfg_sync (ts)) return -1; memset (&ts->stats, 0, sizeof (ts->stats)); } return 0; } static int stats_lock = 0; static void vtc_accumulate_stats (vcl_test_client_worker_t * wrk, vcl_test_session_t * ctrl) { vcl_test_session_t *ts; static char buf[64]; int i, show_rx = 0; while (__sync_lock_test_and_set (&stats_lock, 1)) ; if (ctrl->cfg.test == VCL_TEST_TYPE_BI || ctrl->cfg.test == VCL_TEST_TYPE_ECHO) show_rx = 1; for (i = 0; i < wrk->cfg.num_test_sessions; i++) { ts = &wrk->sessions[i]; ts->stats.start = ctrl->stats.start; if (ctrl->cfg.verbose > 1) { sprintf (buf, "CLIENT (fd %d) RESULTS", ts->fd); vcl_test_stats_dump (buf, &ts->stats, show_rx, 1 /* show tx */ , ctrl->cfg.verbose); } vcl_test_stats_accumulate (&ctrl->stats, &ts->stats); if (vcl_comp_tspec (&ctrl->stats.stop, &ts->stats.stop) < 0) ctrl->stats.stop = ts->stats.stop; } __sync_lock_release (&stats_lock); } static void vtc_worker_sessions_exit (vcl_test_client_worker_t * wrk) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; vcl_test_session_t *ts; int i, verbose = ctrl->cfg.verbose; for (i = 0; i < wrk->cfg.num_test_sessions; i++) { ts = &wrk->sessions[i]; ts->cfg.test = VCL_TEST_TYPE_EXIT; if (verbose) { vtinf ("(fd %d): Sending exit cfg to server...", ts->fd); vcl_test_cfg_dump (&ts->cfg, 1 /* is_client */ ); } (void) vcl_test_write (ts->fd, (uint8_t *) & ts->cfg, sizeof (ts->cfg), &ts->stats, verbose); } wrk->n_sessions = 0; } static void * vtc_worker_loop (void *arg) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; vcl_test_client_worker_t *wrk = arg; uint32_t n_active_sessions, n_bytes; fd_set _wfdset, *wfdset = &_wfdset; fd_set _rfdset, *rfdset = &_rfdset; vcl_test_session_t *ts; int i, rv, check_rx = 0; rv = vtc_worker_init (wrk); if (rv) { vterr ("vtc_worker_init()", rv); return 0; } vtinf ("Starting test ..."); if (wrk->wrk_index == 0) clock_gettime (CLOCK_REALTIME, &ctrl->stats.start); check_rx = wrk->cfg.test != VCL_TEST_TYPE_UNI; n_active_sessions = wrk->cfg.num_test_sessions; while (n_active_sessions) { _wfdset = wrk->wr_fdset; _rfdset = wrk->rd_fdset; rv = vppcom_select (wrk->max_fd_index, (unsigned long *) rfdset, (unsigned long *) wfdset, NULL, 0); if (rv < 0) { vterr ("vppcom_select()", rv); goto exit; } else if (rv == 0) continue; for (i = 0; i < wrk->cfg.num_test_sessions; i++) { ts = &wrk->sessions[i]; if (!((ts->stats.stop.tv_sec == 0) && (ts->stats.stop.tv_nsec == 0))) continue; if (FD_ISSET (vppcom_session_index (ts->fd), rfdset) && ts->stats.rx_bytes < ts->cfg.total_bytes) { (void) vcl_test_read (ts->fd, (uint8_t *) ts->rxbuf, ts->rxbuf_size, &ts->stats); } if (FD_ISSET (vppcom_session_index (ts->fd), wfdset) && ts->stats.tx_bytes < ts->cfg.total_bytes) { n_bytes = ts->cfg.txbuf_size; if (ts->cfg.test == VCL_TEST_TYPE_ECHO) n_bytes = strlen (ctrl->txbuf) + 1; rv = vcl_test_write (ts->fd, (uint8_t *) ts->txbuf, n_bytes, &ts->stats, ts->cfg.verbose); if (rv < 0) { vtwrn ("vppcom_test_write (%d) failed -- aborting test", ts->fd); goto exit; } } if ((!check_rx && ts->stats.tx_bytes >= ts->cfg.total_bytes) || (check_rx && ts->stats.rx_bytes >= ts->cfg.total_bytes)) { clock_gettime (CLOCK_REALTIME, &ts->stats.stop); n_active_sessions--; } } } exit: vtinf ("Worker %d done ...", wrk->wrk_index); if (wrk->cfg.test != VCL_TEST_TYPE_ECHO) vtc_accumulate_stats (wrk, ctrl); sleep (VCL_TEST_DELAY_DISCONNECT); vtc_worker_sessions_exit (wrk); if (wrk->wrk_index) vt_atomic_add (&vcm->active_workers, -1); return 0; } static void vtc_print_stats (vcl_test_session_t * ctrl) { int is_echo = ctrl->cfg.test == VCL_TEST_TYPE_ECHO; int show_rx = 0; char buf[64]; if (ctrl->cfg.test == VCL_TEST_TYPE_BI || ctrl->cfg.test == VCL_TEST_TYPE_ECHO) show_rx = 1; vcl_test_stats_dump ("CLIENT RESULTS", &ctrl->stats, show_rx, 1 /* show tx */ , ctrl->cfg.verbose); vcl_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); if (ctrl->cfg.verbose) { vtinf (" ctrl session info\n" VCL_TEST_SEPARATOR_STRING " fd: %d (0x%08x)\n" " rxbuf: %p\n" " rxbuf size: %u (0x%08x)\n" " txbuf: %p\n" " txbuf size: %u (0x%08x)\n" VCL_TEST_SEPARATOR_STRING, ctrl->fd, (uint32_t) ctrl->fd, ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size, ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size); } if (is_echo) sprintf (buf, "Echo"); else sprintf (buf, "%s-directional Stream", ctrl->cfg.test == VCL_TEST_TYPE_BI ? "Bi" : "Uni"); } static void vtc_echo_client (vcl_test_client_main_t * vcm) { vcl_test_client_worker_t *wrk; vcl_test_session_t *ctrl = &vcm->ctrl_session; vcl_test_cfg_t *cfg = &ctrl->cfg; cfg->total_bytes = strlen (ctrl->txbuf) + 1; memset (&ctrl->stats, 0, sizeof (ctrl->stats)); /* Echo works with only one worker */ wrk = vcm->workers; wrk->wrk_index = 0; wrk->cfg = *cfg; vtc_worker_loop (wrk); /* Not relevant for echo test clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop); vtc_accumulate_stats (wrk, ctrl); vtc_print_stats (ctrl); */ } static void vtc_stream_client (vcl_test_client_main_t * vcm) { vcl_test_session_t *ctrl = &vcm->ctrl_session; vcl_test_cfg_t *cfg = &ctrl->cfg; vcl_test_client_worker_t *wrk; vcl_test_session_t *ts; int tx_bytes, rv; uint32_t i, n, sidx, n_conn, n_conn_per_wrk; vtinf ("%s-directional Stream Test Starting!", ctrl->cfg.test == VCL_TEST_TYPE_BI ? "Bi" : "Uni"); cfg->total_bytes = cfg->num_writes * cfg->txbuf_size; cfg->ctrl_handle = ~0; if (vtc_cfg_sync (ctrl)) { vtwrn ("test cfg sync failed -- aborting!"); return; } cfg->ctrl_handle = ((vcl_test_cfg_t *) ctrl->rxbuf)->ctrl_handle; memset (&ctrl->stats, 0, sizeof (ctrl->stats)); n_conn = cfg->num_test_sessions; n_conn_per_wrk = n_conn / vcm->n_workers; for (i = 0; i < vcm->n_workers; i++) { wrk = &vcm->workers[i]; wrk->wrk_index = i; wrk->cfg = ctrl->cfg; wrk->cfg.num_test_sessions = vtc_min (n_conn_per_wrk, n_conn); n_conn -= wrk->cfg.num_test_sessions; } for (i = 1; i < vcm->n_workers; i++) { wrk = &vcm->workers[i]; pthread_create (&wrk->thread_handle, NULL, vtc_worker_loop, (void *) wrk); } vtc_worker_loop (&vcm->workers[0]); while (vcm->active_workers > 0) ; vtinf ("Sending config on ctrl session (fd %d) for stats...", ctrl->fd); if (vtc_cfg_sync (ctrl)) { vtwrn ("test cfg sync failed -- aborting!"); return; } vtc_print_stats (ctrl); ctrl->cfg.test = VCL_TEST_TYPE_ECHO; ctrl->cfg.total_bytes = 0; if (vtc_cfg_sync (ctrl)) vtwrn ("post-test cfg sync failed!"); } static void dump_help (void) { #define INDENT "\n " printf ("CLIENT: Test configuration commands:" INDENT VCL_TEST_TOKEN_HELP "\t\t\tDisplay help." INDENT VCL_TEST_TOKEN_EXIT "\t\t\tExit test client & server." INDENT VCL_TEST_TOKEN_SHOW_CFG "\t\t\tShow the current test cfg." INDENT VCL_TEST_TOKEN_RUN_UNI "\t\t\tRun the Uni-directional test." INDENT VCL_TEST_TOKEN_RUN_BI "\t\t\tRun the Bi-directional test." INDENT VCL_TEST_TOKEN_VERBOSE "\t\t\tToggle verbose setting." INDENT VCL_TEST_TOKEN_RXBUF_SIZE "<rxbuf size>\tRx buffer size (bytes)." INDENT VCL_TEST_TOKEN_TXBUF_SIZE "<txbuf size>\tTx buffer size (bytes)." INDENT VCL_TEST_TOKEN_NUM_WRITES "<# of writes>\tNumber of txbuf writes to server." "\n"); } static void cfg_txbuf_size_set (void) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; char *p = ctrl->txbuf + strlen (VCL_TEST_TOKEN_TXBUF_SIZE); uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10); if (txbuf_size >= VCL_TEST_CFG_BUF_SIZE_MIN) { ctrl->cfg.txbuf_size = txbuf_size; ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; vcl_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size); vcl_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); } else vtwrn ("Invalid txbuf size (%lu) < minimum buf size (%u)!", txbuf_size, VCL_TEST_CFG_BUF_SIZE_MIN); } static void cfg_num_writes_set (void) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; char *p = ctrl->txbuf + strlen (VCL_TEST_TOKEN_NUM_WRITES); uint32_t num_writes = strtoul ((const char *) p, NULL, 10); if (num_writes > 0) { ctrl->cfg.num_writes = num_writes; ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; vcl_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); } else { vtwrn ("invalid num writes: %u", num_writes); } } static void cfg_num_test_sessions_set (void) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; char *p = ctrl->txbuf + strlen (VCL_TEST_TOKEN_NUM_TEST_SESS); uint32_t num_test_sessions = strtoul ((const char *) p, NULL, 10); if ((num_test_sessions > 0) && (num_test_sessions <= VCL_TEST_CFG_MAX_TEST_SESS)) { ctrl->cfg.num_test_sessions = num_test_sessions; vcl_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); } else { vtwrn ("invalid num test sessions: %u, (%d max)", num_test_sessions, VCL_TEST_CFG_MAX_TEST_SESS); } } static void cfg_rxbuf_size_set (void) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; char *p = ctrl->txbuf + strlen (VCL_TEST_TOKEN_RXBUF_SIZE); uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10); if (rxbuf_size >= VCL_TEST_CFG_BUF_SIZE_MIN) { ctrl->cfg.rxbuf_size = rxbuf_size; vcl_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size); vcl_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); } else vtwrn ("Invalid rxbuf size (%lu) < minimum buf size (%u)!", rxbuf_size, VCL_TEST_CFG_BUF_SIZE_MIN); } static void cfg_verbose_toggle (void) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1; vcl_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); } static vcl_test_t parse_input () { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; vcl_test_t rv = VCL_TEST_TYPE_NONE; if (!strncmp (VCL_TEST_TOKEN_EXIT, ctrl->txbuf, strlen (VCL_TEST_TOKEN_EXIT))) rv = VCL_TEST_TYPE_EXIT; else if (!strncmp (VCL_TEST_TOKEN_HELP, ctrl->txbuf, strlen (VCL_TEST_TOKEN_HELP))) dump_help (); else if (!strncmp (VCL_TEST_TOKEN_SHOW_CFG, ctrl->txbuf, strlen (VCL_TEST_TOKEN_SHOW_CFG))) vcm->dump_cfg = 1; else if (!strncmp (VCL_TEST_TOKEN_VERBOSE, ctrl->txbuf, strlen (VCL_TEST_TOKEN_VERBOSE))) cfg_verbose_toggle (); else if (!strncmp (VCL_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf, strlen (VCL_TEST_TOKEN_TXBUF_SIZE))) cfg_txbuf_size_set (); else if (!strncmp (VCL_TEST_TOKEN_NUM_TEST_SESS, ctrl->txbuf, strlen (VCL_TEST_TOKEN_NUM_TEST_SESS))) cfg_num_test_sessions_set (); else if (!strncmp (VCL_TEST_TOKEN_NUM_WRITES, ctrl->txbuf, strlen (VCL_TEST_TOKEN_NUM_WRITES))) cfg_num_writes_set (); else if (!strncmp (VCL_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf, strlen (VCL_TEST_TOKEN_RXBUF_SIZE))) cfg_rxbuf_size_set (); else if (!strncmp (VCL_TEST_TOKEN_RUN_UNI, ctrl->txbuf, strlen (VCL_TEST_TOKEN_RUN_UNI))) rv = ctrl->cfg.test = VCL_TEST_TYPE_UNI; else if (!strncmp (VCL_TEST_TOKEN_RUN_BI, ctrl->txbuf, strlen (VCL_TEST_TOKEN_RUN_BI))) rv = ctrl->cfg.test = VCL_TEST_TYPE_BI; else rv = VCL_TEST_TYPE_ECHO; return rv; } void print_usage_and_exit (void) { fprintf (stderr, "vcl_test_client [OPTIONS] <ipaddr> <port>\n" " OPTIONS\n" " -h Print this message and exit.\n" " -6 Use IPv6\n" " -c Print test config before test.\n" " -w <dir> Write test results to <dir>.\n" " -X Exit after running test.\n" " -D Use UDP transport layer\n" " -L Use TLS transport layer\n" " -E Run Echo test.\n" " -N <num-writes> Test Cfg: number of writes.\n" " -R <rxbuf-size> Test Cfg: rx buffer size.\n" " -T <txbuf-size> Test Cfg: tx buffer size.\n" " -U Run Uni-directional test.\n" " -B Run Bi-directional test.\n" " -V Verbose mode.\n"); exit (1); } static void vtc_process_opts (vcl_test_client_main_t * vcm, int argc, char **argv) { vcl_test_session_t *ctrl = &vcm->ctrl_session; int c, v; opterr = 0; while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6DL")) != -1) switch (c) { case 'c': vcm->dump_cfg = 1; break; case 's': if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sessions) != 1) if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sessions) != 1) { vtwrn ("Invalid value for option -%c!", c); print_usage_and_exit (); } if (!ctrl->cfg.num_test_sessions || (ctrl->cfg.num_test_sessions > FD_SETSIZE)) { vtwrn ("Invalid number of sessions (%d) specified for option -%c!" "\n Valid range is 1 - %d", ctrl->cfg.num_test_sessions, c, FD_SETSIZE); print_usage_and_exit (); } break; case 'w': if (sscanf (optarg, "%d", &v) != 1) { vtwrn ("Invalid value for option -%c!", c); print_usage_and_exit (); } if (v > 1) vcm->n_workers = v; break; case 'X': vcm->post_test = VCL_TEST_TYPE_EXIT; break; case 'E': if (strlen (optarg) > ctrl->txbuf_size) { vtwrn ("Option -%c value larger than txbuf size (%d)!", optopt, ctrl->txbuf_size); print_usage_and_exit (); } strcpy (ctrl->txbuf, optarg); ctrl->cfg.test = VCL_TEST_TYPE_ECHO; break; case 'I': if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sessions) != 1) if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sessions) != 1) { vtwrn ("Invalid value for option -%c!", c); print_usage_and_exit (); } if (ctrl->cfg.num_test_sessions > VCL_TEST_CFG_MAX_TEST_SESS) { vtwrn ("value greater than max number test sessions (%d)!", VCL_TEST_CFG_MAX_TEST_SESS); print_usage_and_exit (); } break; case 'N': if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1) if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1) { vtwrn ("Invalid value for option -%c!", c); print_usage_and_exit (); } ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; break; case 'R': if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1) if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1) { vtwrn ("Invalid value for option -%c!", c); print_usage_and_exit (); } if (ctrl->cfg.rxbuf_size >= VCL_TEST_CFG_BUF_SIZE_MIN) { ctrl->rxbuf_size = ctrl->cfg.rxbuf_size; vcl_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size); } else { vtwrn ("rxbuf size (%lu) less than minumum (%u)", ctrl->cfg.rxbuf_size, VCL_TEST_CFG_BUF_SIZE_MIN); print_usage_and_exit (); } break; case 'T': if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1) if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1) { vtwrn ("Invalid value for option -%c!", c); print_usage_and_exit (); } if (ctrl->cfg.txbuf_size >= VCL_TEST_CFG_BUF_SIZE_MIN) { ctrl->txbuf_size = ctrl->cfg.txbuf_size; vcl_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size); ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; } else { vtwrn ("txbuf size (%lu) less than minumum (%u)!", ctrl->cfg.txbuf_size, VCL_TEST_CFG_BUF_SIZE_MIN); print_usage_and_exit (); } break; case 'U': ctrl->cfg.test = VCL_TEST_TYPE_UNI; break; case 'B': ctrl->cfg.test = VCL_TEST_TYPE_BI; break; case 'V': ctrl->cfg.verbose = 1; break; case '6': ctrl->cfg.address_ip6 = 1; break; case 'D': ctrl->cfg.transport_udp = 1; break; case 'L': ctrl->cfg.transport_tls = 1; break; case '?': switch (optopt) { case 'E': case 'I': case 'N': case 'R': case 'T': case 'w': vtwrn ("Option -%c requires an argument.", optopt); break; default: if (isprint (optopt)) vtwrn ("Unknown option `-%c'.", optopt); else vtwrn ("Unknown option character `\\x%x'.", optopt); } /* fall thru */ case 'h': default: print_usage_and_exit (); } if (argc < (optind + 2)) { vtwrn ("Insufficient number of arguments!"); print_usage_and_exit (); } if (ctrl->cfg.transport_udp) { vcm->proto = VPPCOM_PROTO_UDP; } else if (ctrl->cfg.transport_tls) { vcm->proto = VPPCOM_PROTO_TLS; } else { vcm->proto = VPPCOM_PROTO_TCP; } memset (&vcm->server_addr, 0, sizeof (vcm->server_addr)); if (ctrl->cfg.address_ip6) { struct sockaddr_in6 *sddr6 = (struct sockaddr_in6 *) &vcm->server_addr; sddr6->sin6_family = AF_INET6; inet_pton (AF_INET6, argv[optind++], &(sddr6->sin6_addr)); sddr6->sin6_port = htons (atoi (argv[optind])); vcm->server_endpt.is_ip4 = 0; vcm->server_endpt.ip = (uint8_t *) & sddr6->sin6_addr; vcm->server_endpt.port = (uint16_t) sddr6->sin6_port; } else { struct sockaddr_in *saddr4 = (struct sockaddr_in *) &vcm->server_addr; saddr4->sin_family = AF_INET; inet_pton (AF_INET, argv[optind++], &(saddr4->sin_addr)); saddr4->sin_port = htons (atoi (argv[optind])); vcm->server_endpt.is_ip4 = 1; vcm->server_endpt.ip = (uint8_t *) & saddr4->sin_addr; vcm->server_endpt.port = (uint16_t) saddr4->sin_port; } } static void vtc_read_user_input (vcl_test_session_t * ctrl) { printf ("\nType some characters and hit <return>\n" "('" VCL_TEST_TOKEN_HELP "' for help): "); if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL) { if (strlen (ctrl->txbuf) == 1) { printf ("\nNothing to send! Please try again...\n"); return; } ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline. /* Parse input for keywords */ ctrl->cfg.test = parse_input (); } } static void vtc_ctrl_session_exit (void) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; int verbose = ctrl->cfg.verbose; ctrl->cfg.test = VCL_TEST_TYPE_EXIT; if (verbose) { vtinf ("(fd %d): Sending exit cfg to server...", ctrl->fd); vcl_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); } (void) vcl_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg, sizeof (ctrl->cfg), &ctrl->stats, verbose); sleep (1); } int main (int argc, char **argv) { vcl_test_client_main_t *vcm = &vcl_client_main; vcl_test_session_t *ctrl = &vcm->ctrl_session; int rv, errno_val; vcm->n_workers = 1; vcl_test_cfg_init (&ctrl->cfg); vcl_test_session_buf_alloc (ctrl); vtc_process_opts (vcm, argc, argv); vcm->workers = calloc (vcm->n_workers, sizeof (vcl_test_client_worker_t)); rv = vppcom_app_create ("vcl_test_client"); if (rv < 0) vtfail ("vppcom_app_create()", rv); ctrl->fd = vppcom_session_create (vcm->proto, 0 /* is_nonblocking */ ); if (ctrl->fd < 0) vtfail ("vppcom_session_create()", ctrl->fd); if (vcm->proto == VPPCOM_PROTO_TLS) { vtinf ("Adding tls certs ..."); vppcom_session_tls_add_cert (ctrl->fd, vcl_test_crt_rsa, vcl_test_crt_rsa_len); vppcom_session_tls_add_key (ctrl->fd, vcl_test_key_rsa, vcl_test_key_rsa_len); } vtinf ("Connecting to server..."); rv = vppcom_session_connect (ctrl->fd, &vcm->server_endpt); if (rv) vtfail ("vppcom_session_connect()", rv); vtinf ("Control session (fd %d) connected.", ctrl->fd); rv = vtc_cfg_sync (ctrl); if (rv) vtfail ("vtc_cfg_sync()", rv); ctrl->cfg.ctrl_handle = ((vcl_test_cfg_t *) ctrl->rxbuf)->ctrl_handle; memset (&ctrl->stats, 0, sizeof (ctrl->stats)); while (ctrl->cfg.test != VCL_TEST_TYPE_EXIT) { if (vcm->dump_cfg) { vcl_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); vcm->dump_cfg = 0; } switch (ctrl->cfg.test) { case VCL_TEST_TYPE_ECHO: vtc_echo_client (vcm); break; case VCL_TEST_TYPE_UNI: case VCL_TEST_TYPE_BI: vtc_stream_client (vcm); break; case VCL_TEST_TYPE_EXIT: continue; case VCL_TEST_TYPE_NONE: default: break; } switch (vcm->post_test) { case VCL_TEST_TYPE_EXIT: switch (ctrl->cfg.test) { case VCL_TEST_TYPE_EXIT: case VCL_TEST_TYPE_UNI: case VCL_TEST_TYPE_BI: case VCL_TEST_TYPE_ECHO: ctrl->cfg.test = VCL_TEST_TYPE_EXIT; continue; case VCL_TEST_TYPE_NONE: default: break; } break; case VCL_TEST_TYPE_NONE: case VCL_TEST_TYPE_ECHO: case VCL_TEST_TYPE_UNI: case VCL_TEST_TYPE_BI: default: break; } memset (ctrl->txbuf, 0, ctrl->txbuf_size); memset (ctrl->rxbuf, 0, ctrl->rxbuf_size); vtc_read_user_input (ctrl); } vtc_ctrl_session_exit (); vppcom_session_close (ctrl->fd); vppcom_app_destroy (); free (vcm->workers); return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */