summaryrefslogtreecommitdiffstats
path: root/src/vppinfra/random.h
blob: bceab41956712d6cef225efe7862adadb5e7428b (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
/*
 * 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 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_random_h
#define included_random_h

#include <vppinfra/clib.h>
#include <vppinfra/vec.h>	/* for vec_resize */
#include <vppinfra/format.h>	/* for unformat_input_t */

/** \file
    Linear Congruential Random Number Generator

    This specific random number generator is described in
    "Numerical Recipes in C", 2nd edition, page 284. If you need
    random numbers with really excellent statistics, take a look
    at Chapter 7...

    By definition, a linear congruential random number generator
    is of the form: rand[i+1] = a*rand[i] + c (mod m) for specific
    values of (a,c,m).

    In this case, choose m = 2**32 and use the low-order 32-bits of
    the 64-bit product a*N[i]. Knuth suggests the use of a=1664525,
    H.W. Lewis has tested C=1013904223 extensively. This routine is
    reputedly as good as any 32-bit LCRN, and costs only a single
    multiply-add.

    Several variants: 32/64-bit, machine word width,
    f64 on the closed interval [0,1].
*/

/** \brief 32-bit random number generator */
always_inline u32
random_u32 (u32 * seed)
{
  *seed = (1664525 * *seed) + 1013904223;
  return *seed;
}

/* External test routine. */
int test_random_main (unformat_input_t * input);

/** \brief Maximum value returned by random_u32() */
always_inline u32
random_u32_max (void)
{
  return 0xffffffff;
}

#ifdef CLIB_UNIX

#include <unistd.h>		/* for getpid */

/** \brief Default random seed (unix/linux user-mode) */
always_inline uword
random_default_seed (void)
{
  return getpid ();
}

#endif

#ifdef CLIB_LINUX_KERNEL

#include <linux/sched.h>	/* for jiffies */

/** \brief Default random seed (Linux kernel) */
always_inline uword
random_default_seed (void)
{
  return jiffies;
}

#endif

#ifdef CLIB_STANDALONE
extern u32 standalone_random_default_seed;

always_inline u32
random_default_seed (void)
{
  return standalone_random_default_seed;
}
#endif

/** \brief 64-bit random number generator
 * Again, constants courtesy of Donald Knuth.
 *
 */
always_inline u64
random_u64 (u64 * seed)
{
  *seed = 6364136223846793005ULL * *seed + 1442695040888963407ULL;
  return *seed;
}

/** \brief machine word size random number generator */

always_inline uword
random_uword (u32 * seed)
{
  if (sizeof (uword) == sizeof (u64))
    return random_u64 ((u64 *) seed);
  else
    return random_u32 (seed);
}

/** \brief Generate f64 random number in the interval [0,1] */
always_inline f64
random_f64 (u32 * seed)
{
  return (f64) random_u32 (seed) / (f64) random_u32_max ();
}

/** \brief Generate random character vector

    From the alphabet a-z, lower case.
    Returns a vector of the supplied length which is NOT guaranteed to be
    NULL-terminated. FIXME?
*/
always_inline u8 *
random_string (u32 * seed, uword len)
{
  u8 *alphabet = (u8 *) "abcdefghijklmnopqrstuvwxyz";
  u8 *s = 0;
  word i;

  vec_resize (s, len);
  for (i = 0; i < len; i++)
    s[i] = alphabet[random_u32 (seed) % 26];

  return s;
}

f64 clib_chisquare (u64 * values);

#endif /* included_random_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
span>1] != p2->laddr.stop.ip6.as_u64[1]) return (0); } else { if (p1->laddr.start.ip4.as_u32 != p2->laddr.start.ip4.as_u32) return (0); if (p1->laddr.stop.ip4.as_u32 != p2->laddr.stop.ip4.as_u32) return (0); if (p1->raddr.start.ip4.as_u32 != p2->raddr.start.ip4.as_u32) return (0); if (p1->raddr.stop.ip4.as_u32 != p2->raddr.stop.ip4.as_u32) return (0); } return (1); } static int ipsec_spd_entry_sort (void *a1, void *a2) { ipsec_main_t *im = &ipsec_main; u32 *id1 = a1; u32 *id2 = a2; ipsec_policy_t *p1, *p2; p1 = pool_elt_at_index (im->policies, *id1); p2 = pool_elt_at_index (im->policies, *id2); if (p1 && p2) return p2->priority - p1->priority; return 0; } int ipsec_policy_mk_type (bool is_outbound, bool is_ipv6, ipsec_policy_action_t action, ipsec_spd_policy_type_t * type) { if (is_outbound) { *type = (is_ipv6 ? IPSEC_SPD_POLICY_IP6_OUTBOUND : IPSEC_SPD_POLICY_IP4_OUTBOUND); return (0); } else { switch (action) { case IPSEC_POLICY_ACTION_PROTECT: *type = (is_ipv6 ? IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT : IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT); return (0); case IPSEC_POLICY_ACTION_BYPASS: *type = (is_ipv6 ? IPSEC_SPD_POLICY_IP6_INBOUND_BYPASS : IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS); return (0); case IPSEC_POLICY_ACTION_DISCARD: *type = (is_ipv6 ? IPSEC_SPD_POLICY_IP6_INBOUND_DISCARD : IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD); return (0); case IPSEC_POLICY_ACTION_RESOLVE: break; } } /* Unsupported type */ return (-1); } int ipsec_add_del_policy (vlib_main_t * vm, ipsec_policy_t * policy, int is_add, u32 * stat_index) { ipsec_main_t *im = &ipsec_main; ipsec_spd_t *spd = 0; ipsec_policy_t *vp; u32 spd_index; uword *p; p = hash_get (im->spd_index_by_spd_id, policy->id); if (!p) return VNET_API_ERROR_SYSCALL_ERROR_1; spd_index = p[0]; spd = pool_elt_at_index (im->spds, spd_index); if (!spd) return VNET_API_ERROR_SYSCALL_ERROR_1; if (is_add) { u32 policy_index; if (policy->policy == IPSEC_POLICY_ACTION_PROTECT) { index_t sa_index = ipsec_sa_find_and_lock (policy->sa_id); if (INDEX_INVALID == sa_index) return VNET_API_ERROR_SYSCALL_ERROR_1; policy->sa_index = sa_index; } else policy->sa_index = INDEX_INVALID; pool_get (im->policies, vp); clib_memcpy (vp, policy, sizeof (*vp)); policy_index = vp - im->policies; vlib_validate_combined_counter (&ipsec_spd_policy_counters, policy_index); vlib_zero_combined_counter (&ipsec_spd_policy_counters, policy_index); vec_add1 (spd->policies[policy->type], policy_index); vec_sort_with_function (spd->policies[policy->type], ipsec_spd_entry_sort); *stat_index = policy_index; } else { u32 ii; vec_foreach_index (ii, (spd->policies[policy->type])) { vp = pool_elt_at_index (im->policies, spd->policies[policy->type][ii]); if (ipsec_policy_is_equal (vp, policy)) { vec_del1 (spd->policies[policy->type], ii); ipsec_sa_unlock (vp->sa_index); pool_put (im->policies, vp); break; } } } return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */