/* * 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-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. */ #ifndef included_hash_h #define included_hash_h #include #include #include #include struct hash_header; typedef uword (hash_key_sum_function_t) (struct hash_header *, uword key); typedef uword (hash_key_equal_function_t) (struct hash_header *, uword key1, uword key2); /* Vector header for hash tables. */ typedef struct hash_header { /* Number of elements in hash table. */ uword elts; /* Flags as follows. */ u32 flags; /* Set if user does not want table to auto-resize when sufficiently full. */ #define HASH_FLAG_NO_AUTO_GROW (1 << 0) /* Set if user does not want table to auto-resize when sufficiently empty. */ #define HASH_FLAG_NO_AUTO_SHRINK (1 << 1) /* Set when hash_next is in the process of iterating through this hash table. */ #define HASH_FLAG_HASH_NEXT_IN_PROGRESS (1 << 2) u32 log2_pair_size; /* Function to compute the "sum" of a hash key. Hash function is this sum modulo the prime size of the hash table (vec_len (v)). */ hash_key_sum_function_t *key_sum; /* Special values for key_sum "function". */ #define KEY_FUNC_NONE (0) /*< sum = key */ #define KEY_FUNC_POINTER_UWORD (1) /*< sum = *(uword *) key */ #define KEY_FUNC_POINTER_U32 (2) /*< sum = *(u32 *) key */ #define KEY_FUNC_STRING (3) /*< sum = string_key_sum, etc. */ #define KEY_FUNC_MEM (4) /*< sum = mem_key_sum */ /* key comparison function */ hash_key_equal_function_t *key_equal; /* Hook for user's data. Used to parameterize sum/equal functions. */ any user; /* Format a (k,v) pair */ format_function_t *format_pair; /* Format function arg */ void *format_pair_arg; /* Bit i is set if pair i is a user object (as opposed to being either zero or an indirect array of pairs). */ uword *is_user; } hash_t; /* Returns a pointer to the hash header given the vector pointer */ always_inline hash_t * hash_header (void *v) { return vec_header (v); } /* Number of elements in the hash table */ always_inline uword hash_elts (void *v) { hash_t *h = hash_header (v); return v ? h->elts : 0; } /* Number of elements the hash table can hold */ always_inline uword hash_capacity (void *v) { return vec_len (v); } /* Returns 1 if the hash pair contains user data */ always_inline uword hash_is_user (void *v, uword i) { hash_t *h = hash_header (v); uword i0 = i / BITS (h->is_user[0]); uword i1 = i % BITS (h->is_user[0]); return (h->is_user[i0] & ((uword) 1 << i1)) != 0; } /* Set the format function and format argument for a hash table */ always_inline void hash_set_pair_format (void *v, format_function_t * format_pair, void *format_pair_arg) { hash_t *h = hash_header (v); h->format_pair = format_pair; h->format_pair_arg = format_pair_arg; } /* Set hash table flags */ always_inline void hash_set_flags (void *v, uword flags) { hash_header (v)->flags |= flags; } /* Key value pairs. */ typedef struct { /* The Key */ uword key; /* The Value. Length is 2^log2_pair_size - 1. */ uword value[0]; } hash_pair_t; /* The indirect pair structure If log2_pair_size > 0 we overload hash pairs with indirect pairs for buckets with more than one pair. */ typedef struct { /* pair vector */ hash_pair_t *pairs; /* padding */ u8 pad[sizeof (uword) - sizeof (hash_pair_t *)]; /* allocated length */ uword alloc_len; } hash_pair_indirect_t; /* Direct / Indirect pair union */ typedef union { hash_pair_t direct; hash_pair_indirect_t indirect; } hash_pair_union_t; #define LOG2_ALLOC_BITS (5) #define PAIR_BITS (BITS (uword) - LOG2_ALLOC_BITS) /* Log2 number of bytes allocated in pairs array. */ always_inline uword indirect_pair_get_log2_bytes (hash_pair_indirect_t * p) { return p->alloc_len >> PAIR_BITS; } /* Get the length of an indirect pair */ always_inline uword indirect_pair_get_len (hash_pair_indirect_t * p) { if (!p->pairs) return 0; else return p->alloc_len & (((uword) 1 << PAIR_BITS) - 1); } /* Set the length of an indirect pair */ always_inline void indirect_pair_set (hash_pair_indirect_t * p, uword log2_alloc, uword len) { ASSERT (len < ((uword) 1 << PAIR_BITS)); ASSERT (log2_alloc < ((uword) 1 << LOG2_ALLOC_BITS)); p->alloc_len = (log2_alloc << PAIR_BITS) | len; } /* internal routine to fetch value for given key */ uword *_hash_get (void *v, uword key); /* internal routine to fetch value (key, value) pair for given key */ hash_pair_t *_hash_get_pair (void *v, uword key); /* internal routine to unset a (key, value) pair */ void *_hash_unset (void *v, uword key, void *old_value); /* internal routine to set a (key, value) pair, return the old value */ void *_hash_set3 (void *v, uword key, void *value, void *old_value); /* Resize a hash table */ void *hash_resize (void *old, uword new_size); /* duplicate a hash table */ void *hash_dup (void *old); /* Returns the number of bytes used by a hash table */ uword hash_bytes (void *v); /* Public macro to set a (key, value) pair, return the old value */ #define hash_set3(h,key,value,old_value) \ ({ \ uword _v = (uword) (value); \ (h) = _hash_set3 ((h), (uword) (key), (void *) &_v, (old_value)); \ }) /* Public macro to fetch value for given key */ #define hash_get(h,key) _hash_get ((h), (uword) (key)) /* Public macro to fetch value (key, value) pair for given key */ #define hash_get_pair(h,key) _hash_get_pair ((h), (uword) (key)) /* Public macro to set a (key, value) pair */ #define hash_set(h,key,value) hash_set3(h,key,value,0) /* Public macro to set (key, 0) pair */ #define hash_set1(h,key) (h) = _hash_set3(h,(uword) (key),0,0) /* Public macro to unset a (key, value) pair */ #define hash_unset(h,key) ((h) = _hash_unset ((h), (uword) (key),0)) /* Public macro to unset a (key, value) pair, return the old value */ #define hash_unset3(h,key,old_value) ((h) = _hash_unset ((h), (uword) (key), (void *) (old_value))) /* get/set/unset for pointer keys. */ /* Public macro to fetch value for given pointer key */ #define hash_get_mem(h,key) _hash_get ((h), pointer_to_uword (key)) /* Public macro to fetch (key, value) for given pointer key */ #define hash_get_pair_mem(h,key) _hash_get_pair ((h), pointer_to_uword (key)) /* Public macro to set (key, value) for pointer key */ #define hash_set_mem(h,key,value) hash_set3 (h, pointer_to_uword (key), (value), 0) /* Public inline function allocate and copy key to use in hash for pointer key */ always_inline void hash_set_mem_alloc (uword ** h, const void *key, uword v) { int objsize = __builtin_object_size (key, 0); size_t ksz = hash_header (*h)->user; void *copy; if (objsize > 0) { ASSERT (objsize == ksz); copy = clib_mem_alloc (objsize); clib_memcpy_fast (copy, key, objsize); } else { copy = clib_mem_alloc (ksz); clib_memcpy_fast (copy, key, ksz); } hash_set_mem (*h, copy, v); } /* Public macro to set (key, 0) for pointer key */ #define hash_set1_mem(h,key) hash_set3 ((h), pointer_to_uword (key), 0, 0) /* Public macro to unset (key, value) for pointer key */ #define hash_unset_mem(h,key) ((h) = _hash_unset ((h), pointer_to_uword (key),0)) /* P
/*
 * echo_client.h - built-in application layer echo client
 *
 * 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.
 */
#ifndef __included_echo_client_h__
#define __included_echo_client_h__

#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>

#include <vppinfra/hash.h>
#include <vppinfra/error.h>
#include <svm/svm_fifo_segment.h>
#include <vnet/session/session.h>
#include <vnet/session/application_interface.h>

typedef struct
{
  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
  app_session_t data;
  u64 bytes_to_send;
  u64 bytes_sent;
  u64 bytes_to_receive;
  u64 bytes_received;
  u64 vpp_session_handle;
  u8 thread_index;
} eclient_session_t;

typedef struct
{
  /*
   * Application setup parameters
   */
  svm_queue_t *vl_input_queue;		/**< vpe input queue */
  svm_msg_q_t **vpp_event_queue;

  u32 cli_node_index;			/**< cli process node index */
  u32 my_client_index;			/**< loopback API client handle */
  u32 app_index;			/**< app index after attach */

  /*
   * Configuration params
   */
  u8 *connect_uri;			/**< URI for slave's connect */
  u64 bytes_to_send;			/**< Bytes to send */
  u32 configured_segment_size;
  u32 fifo_size;
  u32 expected_connections;		/**< Number of clients/connections */
  u32 connections_per_batch;		/**< Connections to rx/tx at once */
  u32 private_segment_count;		/**< Number of private fifo segs */
  u32 private_segment_size;		/**< size of private fifo segs */
  u32 tls_engine;			/**< TLS engine mbedtls/openssl */
  u8 is_dgram;
  u32 no_copy;				/**< Don't memcpy data to tx fifo */

  /*
   * Test state variables
   */
  eclient_session_t *sessions;		/**< Session pool, shared */
  clib_spinlock_t sessions_lock;
  u8 **rx_buf;				/**< intermediate rx buffers */
  u8 *connect_test_data;		/**< Pre-computed test data */
  u32 **connection_index_by_thread;
  u32 **connections_this_batch_by_thread; /**< active connection batch */
  pthread_t client_thread_handle;

  volatile u32 ready_connections;
  volatile u32 finished_connections;
  volatile u64 rx_total;
  volatile u64 tx_total;
  volatile int run_test;		/**< Signal start of test */

  f64 test_start_time;
  f64 test_end_time;
  u32 prev_conns;
  u32 repeats;
  /*
   * Flags
   */
  u8 is_init;
  u8 test_client_attached;
  u8 no_return;
  u8 test_return_packets;
  int i_am_master;
  int drop_packets