FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
svm_fifo.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 #ifndef __included_ssvm_fifo_h__
16 #define __included_ssvm_fifo_h__
17 
18 #include <vppinfra/clib.h>
19 #include <vppinfra/vec.h>
20 #include <vppinfra/mheap.h>
21 #include <vppinfra/heap.h>
22 #include <vppinfra/pool.h>
23 #include <vppinfra/format.h>
24 #include <pthread.h>
25 
26 typedef enum
27 {
32 
33 /** Out-of-order segment */
34 typedef struct
35 {
36  u32 next; /**< Next linked-list element pool index */
37  u32 prev; /**< Previous linked-list element pool index */
38 
39  u32 fifo_position; /**< Start of segment, normalized*/
40  u32 length; /**< Length of segment */
42 
43 #define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
44 
45 typedef struct
46 {
47  pthread_mutex_t mutex; /* 8 bytes */
48  pthread_cond_t condvar; /* 8 bytes */
51  volatile u32 cursize;
53 
54  /* Backpointers */
59  CLIB_CACHE_LINE_ALIGN_MARK (end_shared);
61  CLIB_CACHE_LINE_ALIGN_MARK (end_consumer);
62 
63  /* producer */
65 
66  ooo_segment_t *ooo_segments; /**< Pool of ooo segments */
67  u32 ooos_list_head; /**< Head of out-of-order linked-list */
68  u32 ooos_newest; /**< Last segment to have been updated */
69 
71 } svm_fifo_t;
72 
73 static inline int
74 svm_fifo_lock (svm_fifo_t * f, u32 pid, u32 tag, int nowait)
75 {
76  if (PREDICT_TRUE (nowait == 0))
77  pthread_mutex_lock (&f->mutex);
78  else
79  {
80  if (pthread_mutex_trylock (&f->mutex))
81  return -1;
82  }
83  f->owner_pid = pid;
84  f->tag = tag;
85  return 0;
86 }
87 
88 static inline void
90 {
91  f->owner_pid = 0;
92  f->tag = 0;
94  pthread_mutex_unlock (&f->mutex);
95 }
96 
97 static inline u32
99 {
100  return f->cursize;
101 }
102 
103 static inline u32
105 {
106  return f->nitems - f->cursize;
107 }
108 
109 static inline u8
111 {
113 }
114 
115 svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
116 
117 int svm_fifo_enqueue_nowait (svm_fifo_t * f, int pid, u32 max_bytes,
118  u8 * copy_from_here);
119 
120 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, int pid,
121  u32 offset, u32 required_bytes,
122  u8 * copy_from_here);
123 
124 int svm_fifo_dequeue_nowait (svm_fifo_t * f, int pid, u32 max_bytes,
125  u8 * copy_here);
126 
127 int svm_fifo_peek (svm_fifo_t * f, int pid, u32 offset, u32 max_bytes,
128  u8 * copy_here);
129 int svm_fifo_dequeue_drop (svm_fifo_t * f, int pid, u32 max_bytes);
130 
133 {
134  return f->ooo_segments + f->ooos_newest;
135 }
136 
139 {
140  return ((f->nitems + s->fifo_position - f->tail) % f->nitems);
141 }
142 
145 {
146  return ((f->nitems + s->fifo_position + s->length - f->tail) % f->nitems);
147 }
148 
149 #endif /* __included_ssvm_fifo_h__ */
150 
151 /*
152  * fd.io coding-style-patch-verification: ON
153  *
154  * Local Variables:
155  * eval: (c-set-style "gnu")
156  * End:
157  */
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:68
int svm_fifo_dequeue_drop(svm_fifo_t *f, int pid, u32 max_bytes)
Definition: svm_fifo.c:527
static u8 svm_fifo_has_ooo_data(svm_fifo_t *f)
Definition: svm_fifo.h:110
#define PREDICT_TRUE(x)
Definition: clib.h:98
Fixed length block allocator.
u32 prev
Previous linked-list element pool index.
Definition: svm_fifo.h:37
static void svm_fifo_unlock(svm_fifo_t *f)
Definition: svm_fifo.h:89
static u32 svm_fifo_max_enqueue(svm_fifo_t *f)
Definition: svm_fifo.h:104
svm_fifo_t * svm_fifo_create(u32 data_size_in_bytes)
create an svm fifo, in the current heap.
Definition: svm_fifo.c:20
u32 tail
Definition: svm_fifo.h:64
pthread_cond_t condvar
Definition: svm_fifo.h:48
static u32 ooo_segment_end_offset(svm_fifo_t *f, ooo_segment_t *s)
Definition: svm_fifo.h:144
#define always_inline
Definition: clib.h:84
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
Definition: svm_fifo.h:98
u32 ooos_newest
Last segment to have been updated.
Definition: svm_fifo.h:68
volatile u32 cursize
Definition: svm_fifo.h:51
ooo_segment_t * ooo_segments
Pool of ooo segments.
Definition: svm_fifo.h:66
int svm_fifo_peek(svm_fifo_t *f, int pid, u32 offset, u32 max_bytes, u8 *copy_here)
Definition: svm_fifo.c:491
u32 client_session_index
Definition: svm_fifo.h:56
int svm_fifo_enqueue_nowait(svm_fifo_t *f, int pid, u32 max_bytes, u8 *copy_from_here)
Definition: svm_fifo.c:359
static u32 ooo_segment_offset(svm_fifo_t *f, ooo_segment_t *s)
Definition: svm_fifo.h:138
pthread_mutex_t mutex
Definition: svm_fifo.h:47
static ooo_segment_t * svm_fifo_newest_ooo_segment(svm_fifo_t *f)
Definition: svm_fifo.h:132
#define OOO_SEGMENT_INVALID_INDEX
Definition: svm_fifo.h:43
int svm_fifo_dequeue_nowait(svm_fifo_t *f, int pid, u32 max_bytes, u8 *copy_here)
Definition: svm_fifo.c:484
unsigned int u32
Definition: types.h:88
u32 server_session_index
Definition: svm_fifo.h:55
u32 fifo_position
Start of segment, normalized.
Definition: svm_fifo.h:39
u8 server_thread_index
Definition: svm_fifo.h:57
Out-of-order segment.
Definition: svm_fifo.h:34
u32 ooos_list_head
Head of out-of-order linked-list.
Definition: svm_fifo.h:67
u32 length
Length of segment.
Definition: svm_fifo.h:40
u32 next
Next linked-list element pool index.
Definition: svm_fifo.h:36
template key/value backing page structure
Definition: bihash_doc.h:44
svm_lock_tag_t
Definition: svm_fifo.h:26
u32 nitems
Definition: svm_fifo.h:52
unsigned char u8
Definition: types.h:56
u32 head
Definition: svm_fifo.h:60
svm_lock_tag_t tag
Definition: svm_fifo.h:50
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:101
static int svm_fifo_lock(svm_fifo_t *f, u32 pid, u32 tag, int nowait)
Definition: svm_fifo.h:74
u32 owner_pid
Definition: svm_fifo.h:49
u8 client_thread_index
Definition: svm_fifo.h:58
int svm_fifo_enqueue_with_offset(svm_fifo_t *f, int pid, u32 offset, u32 required_bytes, u8 *copy_from_here)
Definition: svm_fifo.c:422
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".