summaryrefslogtreecommitdiff
path: root/infra/src/vnode_common.h
blob: ce1998dfbc00a1e810ac2e120a16854925b917ef (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#pragma once

#include <vnode.h>
#include <common.h>
#include <sys/queue.h>

#ifndef VNODE_CHECK_THREAD_SAFE
#define VNODE_CHECK_THREAD_SAFE             0
#endif

#ifndef VNODE_STAT_ENABLE
#define VNODE_STAT_ENABLE					1
#endif

struct shared_credict_counter
{
    rte_spinlock_t lock;
    unsigned int counter;
};

/* Tunnel Description Structure */
struct tunnel_desc
{
    /* first cacheline, read only */
    RTE_MARKER cacheline0;

    /* Tunnel Name */
    char symbol[MR_SYMBOL_MAX];
    /* Tunel Object, real object to hold pointers */
    struct rte_ring * tunnel_object;
    /* Tunnel Size */
    unsigned int tunnel_size;
    /* tunnel exclusive size */
    unsigned int tunnel_exclusive_size;

    /* Tunnel Enqueue Buffer */
    struct rte_mbuf ** en_buffer;

#if 0
    /* Tunnel Enqueue Buffer Size */
    struct rte_mbuf ** en_returned_buffer;
#endif

    /* second cacheline, read/write */
    RTE_MARKER cacheline1 __rte_cache_min_aligned;

    unsigned int sz_en_buffer;
    /* Tunnel Enqueue Buffer Used */
    unsigned int sz_en_buffer_used;
    /* shared tunnel use */
    unsigned int shared_credict_used;
    /* counter */
    rte_atomic32_t * shared_credict_counter;

#if 0
    /* Tunnel Enqueue Buffer Returned */
    unsigned int sz_en_buffer_returned;
    /* Tunnel Enqueue Buffer Returned */
    unsigned int sz_en_buffer_returned_used;
    /* Last TSC of flush */
    uint64_t tsc_prev;
#endif

#if VNODE_CHECK_THREAD_SAFE
    /* For debug, to check concurrent access of en_buffer */
    rte_spinlock_t lock_thread_safe_check;
#endif
};

struct tunnel_block
{
    /* Prod Queue Count */
    unsigned int nr_prodq;
    /* Cons Queue Count */
    unsigned int nr_consq;
    /* Tunnel's Producer */
    struct vnode_prod * prod;
    /* Tunnel's Consumer */
    struct vnode_cons * cons;
    /* Tunnel Descs */
    struct tunnel_desc * descs[0];
};

/* Virtual Data-Node Consumer Structure */
struct vnode_cons
{
    TAILQ_ENTRY(vnode_cons) next;
    char symbol[MR_SYMBOL_MAX];
    struct vnode * vnode;
    unsigned int nr_consq;
    struct tunnel_block * block;
    unsigned int cur_attach;

    struct vnode_cons_stat stat[MR_SID_MAX];
    struct vnode_cons_notify notify[MR_SID_MAX];
};

/* Virtual Data-Node Producer Structure */
struct vnode_prod
{
    TAILQ_ENTRY(vnode_prod) next;
    char symbol[MR_SYMBOL_MAX];
    struct vnode * vnode;
    unsigned int nr_prodq;
    unsigned int cur_attach;
    struct tunnel_block * block;
    struct vnode_prod_stat stat[MR_SID_MAX];
};

TAILQ_HEAD(vnode_cons_list, vnode_cons);
TAILQ_HEAD(vnode_prod_list, vnode_prod);

/* Vitrual Data-Node Structure */
struct vnode
{
    /* VNode Symbol */
    char symbol[MR_SYMBOL_MAX];
    /* Consumer */
    struct vnode_cons * cons;
    /* Producer */
    struct vnode_prod * prod;
    /* Tunnel Size */
    unsigned int sz_tunnel;
    /* Tunnel Enqueue Buffer Size */
    unsigned int sz_tunnel_buffer;
    /* shared credict */
    unsigned int sz_shared;
    /* allow to notify cons when packet arrived. */
    unsigned int notify_cons_when_rx;
    /* batch interval */
    unsigned int batch_interval_tsc;

    /* Guarantees one operator(consumer or producer, create or destroy) a time */
    rte_spinlock_t lock __rte_cache_aligned;
    rte_atomic32_t shared_credict_counter __rte_cache_aligned;
};


#ifndef MR_LIBVNODE_MAX_SZ_BURST
#define MR_LIBVNODE_MAX_SZ_BURST		MR_BURST_MAX
#endif

#if VNODE_STAT_ENABLE
#if VNODE_STAT_BY_ATOMIC
#define VNODE_STAT_UPDATE(desc, queue, item, value)					\
do {																\
    rte_atomic64_add(&desc->stat[queue].item,value);				\
} while(0)
#else
#define VNODE_STAT_UPDATE(desc, queue, item, value)					\
do {																\
    desc->stat[queue].item += value;				                \
} while(0)
#endif
#else
#define VNODE_STAT_UPDATE(desc, queue, item, value)					\
do {} while(0)
#endif

#define VNODE_STAT_UPDATE_AVG(desc, queue, item, value)             \
do {                                                                \
    desc->stat[queue].item += (typeof(desc->stat[queue].item))(0.2 * (value - desc->stat[queue].item)); \
} while(0)


static inline struct tunnel_desc ** tunnel_block_locate(struct tunnel_block * block,
    unsigned int prodq_id, unsigned int consq_id)
{
    assert(prodq_id < block->nr_prodq && consq_id < block->nr_consq);
    return &(block->descs[prodq_id * block->nr_consq + consq_id]);
}

/* VNode Structure Operation Functions */
struct vnode * __vnode_common_create(const char * sym, unsigned int sz_tunnel, unsigned int sz_tunnel_buffer,
                                     unsigned int notify_cons_when_rx);
struct vnode_prod * __vnode_common_create_prod(struct vnode * vnode, const char * symbol, int nr_prodq);
struct vnode_cons * __vnode_common_create_cons(struct vnode * vnode, const char * symbol, int nr_consq);
struct vnode_prod * __vnode_common_prod_lookup(struct vnode * vnode, const char * sym);
struct vnode_cons * __vnode_common_cons_lookup(struct vnode * vnode, const char * sym);
int __vnode_common_cons_attach(struct vnode * node, struct vnode_cons * cons);
int __vnode_common_prod_attach(struct vnode * node, struct vnode_prod* prod);

struct vnode_prod_stat * __vnode_common_prod_stat_get(struct vnode_prod * prod);
struct vnode_cons_stat * __vnode_common_cons_stat_get(struct vnode_cons * cons);

int __vnode_common_delete_prod(struct vnode_prod * prod);
int __vnode_common_delete_cons(struct vnode_cons * cons);
int __vnode_common_delete(struct vnode * vnode);

void __vnode_common_unpoison(struct vnode * vnode);
void __vnode_common_unpoison_prod(struct vnode_prod * prod);
void __vnode_common_unpoison_cons(struct vnode_cons * cons);
struct vnode_cons_notify * __vnode_common_cons_notify_ctx(struct vnode_cons * cons);

#define __USE_COMMON_VNODE_CREATE_PROD(_type)																\
struct vnode_prod * vnode_##_type##_create_prod(struct vnode * vnode, const char * symbol, int nr_prodq)	\
{	return __vnode_common_create_prod(vnode,symbol,nr_prodq); }

#define __USE_COMMON_VNODE_CREATE_CONS(_type)																\
struct vnode_cons * vnode_##_type##_create_cons(struct vnode * vnode, const char * symbol, int nr_prodq)	\
{	return __vnode_common_create_cons(vnode,symbol,nr_prodq); }

#define __USE_COMMON_VNODE_PROD_LOOKUP(_type)																\
struct vnode_prod * vnode_##_type##_prod_lookup(struct vnode * vnode, const char * sym)						\
{	return __vnode_common_prod_lookup(vnode,sym); }

#define __USE_COMMON_VNODE_CONS_LOOKUP(_type)																\
struct vnode_cons * vnode_##_type##_cons_lookup(struct vnode * vnode, const char * sym)						\
{	return __vnode_common_cons_lookup(vnode,sym); }

#define __USE_COMMON_VNODE_PROD_ATTACH(_type)																\
int vnode_##_type##_prod_attach(struct vnode * node, struct vnode_prod* prod)								\
{	return __vnode_common_prod_attach(node, prod); }

#define __USE_COMMON_VNODE_CONS_ATTACH(_type)																\
int vnode_##_type##_cons_attach(struct vnode * node, struct vnode_cons * cons)								\
{	return __vnode_common_cons_attach(node, cons); }

#define __USE_COMMON_VNODE_PROD_STAT_GET(_type)																\
struct vnode_prod_stat * vnode_##_type##_prod_stat_get(struct vnode_prod * prod)							\
{	return __vnode_common_prod_stat_get(prod); }

#define __USE_COMMON_VNODE_CONS_STAT_GET(_type)																\
struct vnode_cons_stat * vnode_##_type##_cons_stat_get(struct vnode_cons * cons)							\
{	return __vnode_common_cons_stat_get(cons); }

#define __USE_COMMON_VNODE_DELETE_PROD(_type)																\
int vnode_##_type##_delete_prod(struct vnode_prod * prod)													\
{	return __vnode_common_delete_prod(prod); }

#define __USE_COMMON_VNODE_DELETE_CONS(_type)																\
int vnode_##_type##_delete_cons(struct vnode_cons * cons)												    \
{	return __vnode_common_delete_cons(cons); }																

#define __USE_COMMON_VNODE_UNPOISON_PROD(_type)																\
void vnode_##_type##_unpoison_prod(struct vnode_prod * prod)												\
{	return __vnode_common_unpoison_prod(prod); }

#define __USE_COMMON_VNODE_UNPOISON_CONS(_type)																\
void vnode_##_type##_unpoison_cons(struct vnode_cons * cons)											    \
{	return __vnode_common_unpoison_cons(cons); }

#define __USE_COMMON_VNODE_NOTIFY_CTX_CONS(_type)                                                           \
struct vnode_cons_notify * vnode_##_type##_notify_ctx_cons(struct vnode_cons * cons)                        \
{   return __vnode_common_cons_notify_ctx(cons); }                                                          \