summaryrefslogtreecommitdiffstats
path: root/src/vlib/init.c
blob: 8d4784513ab115ad27b08ecd3871b946bc7be8a2 (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
/*
 * 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.
 */
/*
 * init.c: mechanism for functions to be called at init/exit.
 *
 * Copyright (c) 2008 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.
 */

#include <vlib/vlib.h>

clib_error_t *
vlib_call_init_exit_functions (vlib_main_t * vm,
			       _vlib_init_function_list_elt_t * head,
			       int call_once)
{
  clib_error_t *error = 0;
  _vlib_init_function_list_elt_t *i;

  i = head;
  while (i)
    {
      if (call_once && !hash_get (vm->init_functions_called, i->f))
	{
	  if (call_once)
	    hash_set1 (vm->init_functions_called, i->f);
	  error = i->f (vm);
	  if (error)
	    return error;
	}
      i = i->next_init_function;
    }
  return error;
}

clib_error_t *
vlib_call_all_init_functions (vlib_main_t * vm)
{
  /* Call dummy functions to make sure purely static modules are
     linked in. */
#define _(f) vlib_##f##_reference ();
  foreach_vlib_module_reference;
#undef _

  return vlib_call_init_exit_functions
    (vm, vm->init_function_registrations, 1 /* call_once */ );
}

clib_error_t *
vlib_call_all_main_loop_enter_functions (vlib_main_t * vm)
{
  return vlib_call_init_exit_functions
    (vm, vm->main_loop_enter_function_registrations, 1 /* call_once */ );
}

clib_error_t *
vlib_call_all_main_loop_exit_functions (vlib_main_t * vm)
{
  return vlib_call_init_exit_functions
    (vm, vm->main_loop_exit_function_registrations, 1 /* call_once */ );
}

clib_error_t *
vlib_call_all_config_functions (vlib_main_t * vm,
				unformat_input_t * input, int is_early)
{
  clib_error_t *error = 0;
  vlib_config_function_runtime_t *c, **all;
  uword *hash = 0, *p;
  uword i;

  hash = hash_create_string (0, sizeof (uword));
  all = 0;

  c = vm->config_function_registrations;

  while (c)
    {
      hash_set_mem (hash, c->name, vec_len (all));
      vec_add1 (all, c);
      unformat_init (&c->input, 0, 0);
      c = c->next_registration;
    }

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      u8 *s, *v;

      if (!unformat (input, "%s %v", &s, &v) || !(p = hash_get_mem (hash, s)))
	{
	  error = clib_error_create ("unknown input `%s %v'", s, v);
	  goto done;
	}

      c = all[p[0]];
      if (vec_len (c->input.buffer) > 0)
	vec_add1 (c->input.buffer, ' ');
      vec_add (c->input.buffer, v, vec_len (v));
      vec_free (v);
      vec_free (s);
    }

  for (i = 0; i < vec_len (all); i++)
    {
      c = all[i];

      /* Is this an early config? Are we doing early configs? */
      if (is_early ^ c->is_early)
	continue;

      /* Already called? */
      if (hash_get (vm->init_functions_called, c->function))
	continue;
      hash_set1 (vm->init_functions_called, c->function);

      error = c->function (vm, &c->input);
      if (error)
	goto done;
    }

done:
  for (i = 0; i < vec_len (all); i++)
    {
      c = all[i];
      unformat_free (&c->input);
    }
  vec_free (all);
  hash_free (hash);
  return error;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
pan> * f) { /* return __sync_lock_test_and_set (&f->has_event, 1) == 0; return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */ return !__atomic_exchange_n (&f->has_event, 1, __ATOMIC_RELEASE); } /** * Unsets fifo event flag. * * Also acts as a release barrier. */ always_inline void svm_fifo_unset_event (svm_fifo_t * f) { clib_atomic_release (&f->has_event); } static inline void svm_fifo_set_want_tx_evt (svm_fifo_t * f, u8 want_evt) { f->want_tx_evt = want_evt; } static inline u8 svm_fifo_want_tx_evt (svm_fifo_t * f) { return f->want_tx_evt; } svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes); void svm_fifo_free (svm_fifo_t * f); int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, const u8 * copy_from_here); int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 required_bytes, u8 * copy_from_here); int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here); int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here); int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes); void svm_fifo_dequeue_drop_all (svm_fifo_t * f); int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs); void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs); u32 svm_fifo_number_ooo_segments (svm_fifo_t * f); ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f); void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer); void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len); format_function_t format_svm_fifo; always_inline ooo_segment_t * svm_fifo_newest_ooo_segment (svm_fifo_t * f) { if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX) return 0; return pool_elt_at_index (f->ooo_segments, f->ooos_newest); } always_inline void svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f) { f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; } /** * Max contiguous chunk of data that can be read */ always_inline u32 svm_fifo_max_read_chunk (svm_fifo_t * f) { return ((f->tail > f->head) ? (f->tail - f->head) : (f->nitems - f->head)); } /** * Max contiguous chunk of data that can be written */ always_inline u32 svm_fifo_max_write_chunk (svm_fifo_t * f) { return ((f->tail >= f->head) ? (f->nitems - f->tail) : (f->head - f->tail)); } /** * Advance tail pointer * * Useful for moving tail pointer after external enqueue. */ always_inline void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes) { ASSERT (bytes <= svm_fifo_max_enqueue (f)); f->tail = (f->tail + bytes) % f->nitems; f->cursize += bytes; } always_inline u8 * svm_fifo_head (svm_fifo_t * f) { return (f->data + f->head); } always_inline u8 * svm_fifo_tail (svm_fifo_t * f) { return (f->data + f->tail); } always_inline u32 ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos) { /* Ambiguous. Assumption is that ooo segments don't touch tail */ if (PREDICT_FALSE (pos == f->tail && f->tail == f->head)) return f->nitems; return (((f->nitems + pos) - f->tail) % f->nitems); } always_inline u32 ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos) { return (((f->nitems + f->tail) - pos) % f->nitems); } always_inline u32 ooo_segment_offset (svm_fifo_t * f, ooo_segment_t * s) { return ooo_segment_distance_from_tail (f, s->start); } always_inline u32 ooo_segment_end_offset (svm_fifo_t * f, ooo_segment_t * s) { return ooo_segment_distance_from_tail (f, s->start) + s->length; } always_inline u32 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s) { return s->length; } always_inline ooo_segment_t * ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s) { if (s->prev == OOO_SEGMENT_INVALID_INDEX) return 0; return pool_elt_at_index (f->ooo_segments, s->prev); } always_inline ooo_segment_t * ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s) { if (s->next == OOO_SEGMENT_INVALID_INDEX) return 0; return pool_elt_at_index (f->ooo_segments, s->next); } #endif /* __included_ssvm_fifo_h__ */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */