/* * 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) 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. */ /* This is all stolen from Bob Jenkins and reworked for clib. Thanks once again Bob for the great work. */ /* ------------------------------------------------------------------------------ perfect.c: code to generate code for a hash for perfect hashing. (c) Bob Jenkins, September 1996, December 1999 You may use this code in any way you wish, and it is free. No warranty. I hereby place this in the public domain. Source is http://burtleburtle.net/bob/c/perfect.c This generates a minimal perfect hash function. That means, given a set of n keys, this determines a hash function that maps each of those keys into a value in 0..n-1 with no collisions. The perfect hash function first uses a normal hash function on the key to determine (a,b) such that the pair (a,b) is distinct for all keys, then it computes a^scramble[tab[b]] to get the final perfect hash. tab[] is an array of 1-byte values and scramble[] is a 256-term array of 2-byte or 4-byte values. If there are n keys, the length of tab[] is a power of two between n/3 and n. I found the idea of computing distinct (a,b) values in "Practical minimal perfect hash functions for large databases", Fox, Heath, Chen, and Daoud, Communications of the ACM, January 1992. They found the idea in Chichelli (CACM Jan 1980). Beyond that, our methods differ. The key is hashed to a pair (a,b) where a in 0..*alen*-1 and b in 0..*blen*-1. A fast hash function determines both a and b simultaneously. Any decent hash function is likely to produce hashes so that (a,b) is distinct for all pairs. I try the hash using different values of *salt* until all pairs are distinct. The final hash is (a XOR scramble[tab[b]]). *scramble* is a predetermined mapping of 0..255 into 0..smax-1. *tab* is an array that we fill in in such a way as to make the hash perfect. First we fill in all values of *tab* that are used by more than one key. We try all possible values for each position until one works. This leaves m unmapped keys and m values that something could hash to. If you treat unmapped keys as lefthand nodes and unused hash values as righthand nodes, and draw a line connecting each key to each hash value it could map to, you get a bipartite graph. We attempt to find a perfect matching in this graph. If we succeed, we have determined a perfect hash for the whole set of keys. *scramble* is used because (a^tab[i]) clusters keys around *a*. ------------------------------------------------------------------------------ */ #include #include #include #include static void init_keys_direct_u32 (phash_main_t * pm) { int n_keys_left, b_mask, a_shift; u32 seed; phash_key_t *k; seed = pm->hash_seed; b_mask = (1 << pm->b_bits) - 1; a_shift = BITS (seed) - pm->a_bits; k = pm->keys; n_keys_left = vec_len (pm->keys); while (n_keys_left >= 2) { u32 x0, y0, z0; u32 x1, y1, z1; x0 = y0 = z0 = seed; x1 = y1 = z1 = seed; x0 += (u32) k[0].key; x1 += (u32) k[1].key; hash_mix32 (x0, y0, z0); hash_mix32 (x1, y1, z1); k[0].b = z0 & b_mask; k[1].b = z1 & b_mask; k[0].a = z0 >> a_shift; k[1].a = z1 >> a_shift; if (PREDICT_FALSE (a_shift >= BITS (z0))) k[0].a = k[1].a = 0; k += 2; n_keys_left -= 2; } if (n_keys_left >= 1) { u32 x0, y0, z0; x0 = y0 = z0 = seed; x0 += k[0].key; hash_mix32 (x0, y0, z0); k[0].b = z0 & b_mask; k[0].a = z0 >> a_shift; if (PREDICT_FALSE (a_shift >= BITS (z0))) k[0].a = 0; k += 1; n_keys_left -= 1; } } static void init_keys_direct_u64 (phash_main_t * pm) { int n_keys_left, b_mask, a_shift; u64 seed; phash_key_t *k; seed = pm->hash_seed; b_mask = (1 << pm->b_bits) - 1; a_shift = BITS (seed) - pm->a_bits; k = pm->keys; n_keys_left = vec_len (pm->keys); while (n_keys_left >= 2) { u64 x0, y0, z0; u64 x1, y1, z1; x0 = y0 = z0 = seed; x1 = y1 = z1 = seed; x0 += (u64) k[0].key; x1 += (u64) k[1].key; hash_mix64 (x0, y0, z0); hash_mix64 (x1, y1, z1); k[0].b = z0 & b_mask; k[1].b = z1 & b_mask; k[0].a = z0 >> a_shift; k[1].a = z1 >> a_shift; if (PREDICT_FALSE (a_shift >= BITS (z0))) k[0].a = k[1].a = 0; k
from trex_stl_lib.api import *

class STLS1(object):

    def create_stream (self):
        base_pkt = Ether()/IP(src="2.2.0.1")/UDP(sport=12)

        pad = Padding()
        if len(base_pkt) < 64:
            pad_len = 64 - len(base_pkt)
            pad.load = '\x00' * pad_len

        vm = STLVM()

        vm.tuple_var(name="tuple", ip_min="173.16.1.3", ip_max="173.16.1.102", port_min=1025, port_max=1124, limit_flows = 100000)

        vm.write(fv_name="tuple.ip", pkt_offset="IP.dst")
        vm.fix_chksum()

        vm.write(fv_name="tuple.port", pkt_offset="UDP.dport")

        pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm)

        return STLStream(packet=pkt, mode=STLTXCont())

    def get_streams (self, direction = 0, **kwargs):
        return [self.create_stream()]


# dynamic load - used for trex console or simulator
def register():
    return STLS1()
(1 << nbits) - 1; int const2 = 1 + nbits / 2; int const3 = 1 + nbits / 3; int const4 = 1 + nbits / 4; int const5 = 1 + nbits / 5; for (i = 0; i < 20; i++) { x = (x + (x << const2)) & mask; x = (x ^ (x >> const3)); x = (x + (x << const4)) & mask; x = (x ^ (x >> const5)); } return x; } /* initialize scramble[] with distinct random values in 0..smax-1 */ static void scramble_init (phash_main_t * pm) { u32 i; /* fill scramble[] with distinct random integers in 0..smax-1 */ vec_validate (pm->scramble, (1 << (pm->s_bits < 8 ? 8 : pm->s_bits)) - 1); for (i = 0; i < vec_len (pm->scramble); i++) pm->scramble[i] = scramble_permute (i, pm->s_bits); } /* Try to find a perfect hash function. */ clib_error_t * phash_find_perfect_hash (phash_main_t * pm) { clib_error_t *error = 0; u32 max_a_bits, n_tries_this_a_b, want_minimal; /* guess initial values for s_max, a_max and b_max */ guess_initial_parameters (pm); want_minimal = pm->flags & PHASH_FLAG_MINIMAL; new_s: if (pm->b_bits == 0) pm->a_bits = pm->s_bits; max_a_bits = pm->s_bits - want_minimal; if (max_a_bits < 1) max_a_bits = 1; pm->hash_max = want_minimal ? vec_len (pm->keys) : (1 << pm->s_bits); scramble_init (pm); /* Allocate working memory. */ vec_free (pm->tabh); vec_validate_init_empty (pm->tabh, pm->hash_max - 1, ~0); vec_free (pm->tabq); vec_validate (pm->tabq, 1 << pm->b_bits); /* Actually find the perfect hash */ n_tries_this_a_b = 0; while (1) { /* Choose random hash seeds until keys become unique. */ pm->hash_seed = random_u64 (&pm->random_seed); pm->n_seed_trials++; if (init_tabb (pm)) { /* Found unique (A, B). */ /* Hash may already be perfect. */ if (pm->b_bits == 0) goto done; pm->n_perfect_calls++; if (perfect (pm)) goto done; goto increase_b; } /* Keep trying with different seed value. */ n_tries_this_a_b++; if (n_tries_this_a_b < 2048) continue; /* Try to put more bits in (A,B) to make distinct (A,B) more likely */ if (pm->a_bits < max_a_bits) pm->a_bits++; else if (pm->b_bits < pm->s_bits) { increase_b: vec_resize (pm->tabb, vec_len (pm->tabb)); vec_resize (pm->tabq, vec_len (pm->tabq)); pm->b_bits++; } else { /* Can't increase (A, B) any more, so try increasing S. */ goto new_s; } } done: /* Construct mapping table for hash lookups. */ if (!error) { u32 b, v; pm->a_shift = ((pm->flags & PHASH_FLAG_MIX64) ? 64 : 32) - pm->a_bits; pm->b_mask = (1 << pm->b_bits) - 1; vec_resize (pm->tab, vec_len (pm->tabb)); for (b = 0; b < vec_len (pm->tabb); b++) { v = pm->tabb[b].val_b; /* Apply scramble now for small enough value of b_bits. */ if (!(pm->flags & PHASH_FLAG_USE_SCRAMBLE)) v = pm->scramble[v]; pm->tab[b] = v; } } /* Free working memory. */ phash_main_free_working_memory (pm); return error; } /* Slow hash computation for general keys. */ uword phash_hash_slow (phash_main_t * pm, uword key) { u32 a, b, v; if (pm->flags & PHASH_FLAG_MIX64) { u64 x0, y0, z0; x0 = y0 = z0 = pm->hash_seed; if (pm->key_seed1) { u64 xyz[3]; pm->key_seed1 (pm->private, key, &xyz); x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2]; } else x0 += key; hash_mix64 (x0, y0, z0); a = z0 >> pm->a_shift; b = z0 & pm->b_mask; } else { u32 x0, y0, z0; x0 = y0 = z0 = pm->hash_seed; if (pm->key_seed1) { u32 xyz[3]; pm->key_seed1 (pm->private, key, &xyz); x0 += xyz[0]; y0 += xyz[1]; z0 += xyz[2]; } else x0 += key; hash_mix32 (x0, y0, z0); a = z0 >> pm->a_shift; b = z0 & pm->b_mask; } v = pm->tab[b]; if (pm->flags & PHASH_FLAG_USE_SCRAMBLE) v = pm->scramble[v]; return a ^ v; } /* Verify that perfect hash is perfect. */ clib_error_t * phash_validate (phash_main_t * pm) { phash_key_t *k; uword *unique_bitmap = 0; clib_error_t *error = 0; vec_foreach (k, pm->keys) { uword h = phash_hash_slow (pm, k->key); if (h >= pm->hash_max) { error = clib_error_return (0, "hash out of range %wd", h); goto done; } if (clib_bitmap_get (unique_bitmap, h)) { error = clib_error_return (0, "hash non-unique"); goto done; } unique_bitmap = clib_bitmap_ori (unique_bitmap, h); } done: clib_bitmap_free (unique_bitmap); return error; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */