FD.io VPP  v16.06
Vector Packet Processing
smp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  Copyright (c) 2001-2005 Eliot Dresselhaus
17 
18  Permission is hereby granted, free of charge, to any person obtaining
19  a copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be
27  included in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 #ifndef included_clib_smp_h
39 #define included_clib_smp_h
40 
41 #include <vppinfra/cache.h>
42 #include <vppinfra/os.h> /* for os_panic */
43 
44 /* Per-CPU state. */
45 typedef struct {
46  /* Per-cpu local heap. */
47  void * heap;
48 
51 
52 typedef struct {
53  /* Number of CPUs used to model current computer. */
55 
56  /* Number of cpus that are done and have exited. */
58 
59  /* Log2 stack and vm (heap) size. */
60  u8 log2_n_per_cpu_stack_bytes, log2_n_per_cpu_vm_bytes;
61 
62  /* Thread local store (TLS) is stored at stack top.
63  Number of 4k pages to allocate for TLS. */
65 
66  /* Per cpus stacks/heaps start at these addresses. */
67  void * vm_base;
68 
69  /* Thread-safe global heap. Objects here can be allocated/freed by any cpu. */
70  void * global_heap;
71 
74 
76 
77 always_inline void *
79 {
80  return m->vm_base + (cpu << m->log2_n_per_cpu_vm_bytes);
81 }
82 
83 always_inline void *
85 {
86  /* Stack is at top of per cpu VM area. */
87  return clib_smp_vm_base_for_cpu (m, cpu + 1) - ((uword) 1 << m->log2_n_per_cpu_stack_bytes);
88 }
89 
92 {
94  void * sp;
95  uword n;
96 
97  /* Get any old stack address. */
98  sp = &sp;
99 
100  n = ((uword)sp - (uword)m->vm_base) >> m->log2_n_per_cpu_vm_bytes;
101 
102  if (CLIB_DEBUG && m->n_cpus > 0 && n >= m->n_cpus)
103  os_panic ();
104 
105  return n < m->n_cpus ? n : 0;
106 }
107 
108 #define clib_smp_compare_and_swap(addr,new,old) __sync_val_compare_and_swap(addr,old,new)
109 #define clib_smp_swap(addr,new) __sync_lock_test_and_set(addr,new)
110 #define clib_smp_atomic_add(addr,increment) __sync_fetch_and_add(addr,increment)
111 
112 #if defined (i386) || defined (__x86_64__)
113 #define clib_smp_pause() do { asm volatile ("pause"); } while (0)
114 #endif
115 
116 #ifndef clib_smp_pause
117 #define clib_smp_pause() do { } while (0)
118 #endif
119 
120 #ifdef CLIB_UNIX
121 #include <sched.h>
122 
123 always_inline void
125 { sched_yield (); }
126 #else
127 always_inline void
128 os_sched_yield (void)
129 { clib_smp_pause (); }
130 #endif
131 
132 typedef enum {
137 
138 typedef enum {
144 
145 #if uword_bits == 64
147 typedef u32 clib_smp_half_word_t;
148 #else
151 #endif
152 
153 typedef union {
154  struct {
155  /* FIFO of CPUs (threads) waiting for lock. */
156  struct {
158  } waiting_fifo;
159 
160  /* Requesting CPU for atomic compare_and_swap instructions.
161  This makes CPUs requesting same header change unique. */
163 
164  /* Count of readers who have been given read lock.
165  Not applicable for spin locks. */
167 
168  /* Set when writer has been given write lock. Only one of
169  these can happen at a time. */
170  clib_smp_quarter_word_t writer_has_lock : 1;
171  };
172 
175 
178 { return h0.as_uword == h1.as_uword; }
179 
180 typedef struct {
184 
185 /* Cache aligned. */
186 typedef struct {
188 
189  /* Size of waiting FIFO; equal to max number of threads less one. */
191 
193 
196 
199 {
202  return cmp;
203 }
204 
209 
210 always_inline void
212 {
213  clib_smp_lock_header_t h0, h1, h2;
214  uword is_reader = type == CLIB_SMP_LOCK_TYPE_READER;
215  uword my_cpu;
216 
217  /* Null lock means n_cpus <= 1: nothing to lock. */
218  if (! l)
219  return;
220 
221  my_cpu = os_get_cpu_number_inline ();
222  h0 = l->header;
223  while (! h0.writer_has_lock)
224  {
225  /* Want to write but there are still readers with lock? */
226  if (type == CLIB_SMP_LOCK_TYPE_WRITER && h0.n_readers_with_lock != 0)
227  break;
228 
229  if (type == CLIB_SMP_LOCK_TYPE_SPIN)
230  ASSERT_AND_PANIC (h0.waiting_fifo.n_elts == 0);
231 
232  /* Read/write can't proceed when waiting fifo is non-empty. */
233  else if (h0.waiting_fifo.n_elts != 0)
234  break;
235 
236  h1 = h0;
237  h1.request_cpu = my_cpu;
238  h1.writer_has_lock = ! is_reader;
239  h1.n_readers_with_lock += is_reader;
240 
241  /* Try to set head and tail to zero and thereby get the lock. */
242  h2 = clib_smp_lock_set_header (l, h1, h0);
243 
244  /* Compare and swap succeeded? If so, we got the lock. */
245  if (clib_smp_lock_header_is_equal (h2, h0))
246  return;
247 
248  /* Header for slow path. */
249  h0 = h2;
250  }
251 
252  clib_smp_lock_slow_path (l, my_cpu, h0, type);
253 }
254 
255 always_inline void
257 {
258  clib_smp_lock_header_t h0, h1;
259  uword is_reader = type == CLIB_SMP_LOCK_TYPE_READER;
260  uword my_cpu;
261 
262  /* Null means no locking is necessary. */
263  if (! l)
264  return;
265 
266  my_cpu = os_get_cpu_number_inline ();
267  h0 = l->header;
268 
269  /* Should be locked. */
270  if (is_reader)
271  {
274  }
275  else
276  {
279  }
280 
281  /* Locked but empty waiting fifo? */
282  while (h0.waiting_fifo.n_elts == 0)
283  {
284  /* Try to mark it unlocked. */
285  h1 = h0;
286  if (is_reader)
287  h1.n_readers_with_lock -= 1;
288  else
289  h1.writer_has_lock = 0;
290  h1.request_cpu = my_cpu;
291  h1 = clib_smp_lock_set_header (l, h1, h0);
292  if (clib_smp_lock_header_is_equal (h1, h0))
293  return;
294  h0 = h1;
295  }
296 
297  /* Other cpus are waiting. */
298  clib_smp_unlock_slow_path (l, my_cpu, h0, type);
299 }
300 
301 always_inline void
304 
305 always_inline void
308 
309 always_inline void
312 
313 always_inline void
316 
317 always_inline void
320 
321 always_inline void
324 
325 #define clib_exec_on_global_heap(body) \
326 do { \
327  void * __clib_exec_on_global_heap_saved_heap; \
328  \
329  /* Switch to global (thread-safe) heap. */ \
330  __clib_exec_on_global_heap_saved_heap = clib_mem_set_heap (clib_smp_main.global_heap); \
331  \
332  /* Execute body. */ \
333  body; \
334  \
335  /* Switch back to previous heap. */ \
336  clib_mem_set_heap (__clib_exec_on_global_heap_saved_heap); \
337 } while (0)
338 
340  void * bootstrap_function,
341  uword bootstrap_function_arg);
342 
343 void clib_smp_init (void);
344 
345 #endif /* included_clib_smp_h */
#define clib_smp_compare_and_swap(addr, new, old)
Definition: smp.h:108
always_inline void * clib_smp_stack_top_for_cpu(clib_smp_main_t *m, uword cpu)
Definition: smp.h:84
volatile clib_smp_lock_wait_type_t wait_type
Definition: smp.h:181
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
clib_smp_lock_header_t header
Definition: smp.h:187
always_inline void * clib_smp_vm_base_for_cpu(clib_smp_main_t *m, uword cpu)
Definition: smp.h:78
clib_smp_quarter_word_t writer_has_lock
Definition: smp.h:170
void os_panic(void)
Definition: unix-misc.c:165
u16 clib_smp_half_word_t
Definition: smp.h:150
clib_smp_quarter_word_t request_cpu
Definition: smp.h:162
u8 clib_smp_quarter_word_t
Definition: smp.h:149
void clib_smp_lock_init(clib_smp_lock_t **l)
Definition: smp.c:96
always_inline void clib_smp_lock_for_reader(clib_smp_lock_t *l)
Definition: smp.h:310
always_inline void clib_smp_lock_for_writer(clib_smp_lock_t *l)
Definition: smp.h:306
#define always_inline
Definition: clib.h:84
u32 n_waiting_fifo_elts
Definition: smp.h:190
clib_smp_per_cpu_main_t * per_cpu_mains
Definition: smp.h:72
always_inline void clib_smp_unlock(clib_smp_lock_t *l)
Definition: smp.h:314
clib_smp_lock_type_t
Definition: smp.h:132
u32 n_cpus
Definition: smp.h:54
void * vm_base
Definition: smp.h:67
always_inline clib_smp_lock_header_t clib_smp_lock_set_header(clib_smp_lock_t *l, clib_smp_lock_header_t new_hdr, clib_smp_lock_header_t old)
Definition: smp.h:198
clib_smp_quarter_word_t n_readers_with_lock
Definition: smp.h:166
void * global_heap
Definition: smp.h:70
clib_smp_lock_wait_type_t
Definition: smp.h:138
#define ASSERT_AND_PANIC(truth)
always_inline uword clib_smp_lock_header_is_equal(clib_smp_lock_header_t h0, clib_smp_lock_header_t h1)
Definition: smp.h:177
always_inline void clib_smp_unlock_for_writer(clib_smp_lock_t *l)
Definition: smp.h:318
always_inline uword os_get_cpu_number_inline(void)
Definition: smp.h:91
clib_smp_quarter_word_t n_elts
Definition: smp.h:157
clib_smp_main_t clib_smp_main
Definition: mem_mheap.c:46
#define clib_smp_pause()
Definition: smp.h:113
always_inline void os_sched_yield(void)
Definition: smp.h:124
struct clib_smp_lock_header_t::@24::@26 waiting_fifo
always_inline void clib_smp_unlock_inline(clib_smp_lock_t *l, clib_smp_lock_type_t type)
Definition: smp.h:256
u16 n_tls_4k_pages
Definition: smp.h:64
always_inline void clib_smp_unlock_for_reader(clib_smp_lock_t *l)
Definition: smp.h:322
unsigned int u32
Definition: types.h:88
always_inline void clib_smp_lock_inline(clib_smp_lock_t *l, clib_smp_lock_type_t type)
Definition: smp.h:211
u8 log2_n_per_cpu_vm_bytes
Definition: smp.h:60
u64 uword
Definition: types.h:112
u8 log2_n_per_cpu_stack_bytes
Definition: smp.h:60
void clib_smp_unlock_slow_path(clib_smp_lock_t *l, uword my_cpu, clib_smp_lock_header_t h, clib_smp_lock_type_t type)
Definition: smp.c:207
unsigned short u16
Definition: types.h:57
void clib_smp_lock_free(clib_smp_lock_t **l)
Definition: smp.c:127
unsigned char u8
Definition: types.h:56
u32 n_cpus_exited
Definition: smp.h:57
uword os_smp_bootstrap(uword n_cpus, void *bootstrap_function, uword bootstrap_function_arg)
void clib_smp_lock_slow_path(clib_smp_lock_t *l, uword my_cpu, clib_smp_lock_header_t h, clib_smp_lock_type_t type)
Definition: smp.c:134
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
always_inline void clib_smp_lock(clib_smp_lock_t *l)
Definition: smp.h:302
#define BITS(x)
Definition: clib.h:58
void clib_smp_init(void)
Definition: smp.c:82