Skip to content

Commit 2b21ed7

Browse files
Project layout refactoring
The commit changes project layout as - common: base common libraries - filter: packet filtering routines - lib/dataplane: dataplane related utilities - packet: packet processing routines (encap, decap and so on) - module: module related structures and functions - pipeline: pipeline configuration and processing - modules: module directory Also this commit includes following changes: - unittest skeleton - some WIP-structures and items - minor refactoring
1 parent eebea13 commit 2b21ed7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1684
-1023
lines changed

common/hash_table.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
#include <stdint.h>
5+
6+
struct hash_table_chunk {};
7+
8+
struct hash_table {
9+
size_t length;
10+
11+
size_t key_size;
12+
size_t value_size;
13+
14+
struct hash_table_chunk **chunks;
15+
};
16+
17+
static inline struct hash_table_get_chunk(
18+
struct hash_table *table, size_t chunk_index *index
19+
) {
20+
21+
}
22+
23+
static inline int
24+
hash_table_put(
25+
struct hash_table *table,
26+
uint64_t gen,
27+
const void *key,
28+
size_t key_size,
29+
const void *data,
30+
size_t data_size
31+
) {
32+
}
33+
34+
static inline int
35+
hash_table_get(
36+
struct hash_table *table,
37+
uint64_t gen,
38+
const void *key,
39+
size_t key_size,
40+
void *data,
41+
size_t data_size
42+
) {
43+
}

common/lpm.h

-5
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ lpm_insert(
130130
int8_t max_hop = 0;
131131

132132
while (1) {
133-
/*
134-
for (int idx = 0; idx < hop + 1; ++idx)
135-
fprintf(stdout, "%02x ", key[idx]);
136-
fprintf(stdout, " === %d\n", hop);
137-
*/
138133
uint32_t *stored_value = (*pages[hop]) + key[hop];
139134
if (*stored_value == LPM_VALUE_INVALID) {
140135
if (hop < key_size - 1 &&

common/memory.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
#include <stdint.h>
5+
6+
#define BLOCK_COUNT 8
7+
8+
#include "memory_block.h"
9+
10+
// TODO: link parent and child context
11+
struct memory_context {
12+
struct block_allocator *block_allocator;
13+
size_t balloc_count;
14+
size_t bfree_count;
15+
size_t balloc_size;
16+
size_t bfree_size;
17+
18+
char name[64];
19+
};
20+
21+
static inline int
22+
memory_context_init(
23+
struct memory_context *context,
24+
const char *name,
25+
struct block_allocator *block_allocator
26+
) {
27+
context->balloc_count = 0;
28+
context->bfree_count = 0;
29+
context->balloc_size = 0;
30+
context->bfree_size = 0;
31+
32+
context->block_allocator = block_allocator;
33+
snprintf(context->name, sizeof(context->name), "%s", name);
34+
35+
return 0;
36+
}
37+
38+
static inline void *
39+
memory_balloc(struct memory_context *context, size_t size) {
40+
++context->balloc_count;
41+
context->balloc_size += size;
42+
return block_allocator_balloc(context->block_allocator, size);
43+
}
44+
45+
static inline void
46+
memory_bfree(struct memory_context *context, void *block, size_t size) {
47+
++context->bfree_count;
48+
context->bfree_size += size;
49+
return block_allocator_bfree(context->block_allocator, block, size);
50+
}

common/memory_block.h

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
#define MEMORY_BLOCK_ALLOCATOR_MIN_BITS 3
6+
#define MEMORY_BLOCK_ALLOCATOR_EXP 20
7+
#define MEMORY_BLOCK_ALLOCATOR_MIN_SIZE (1 << MEMORY_BLOCK_ALLOCATOR_MIN_BITS)
8+
#define MEMORY_BLOCK_ALLOCATOR_MAX_SIZE \
9+
(1 << (MEMORY_BLOCK_ALLOCATOR_MIN_BITS + MEMORY_BLOCK_ALLOCATOR_EXP))
10+
11+
struct block_allocator_pool {
12+
uint64_t allocate;
13+
uint64_t free;
14+
uint64_t borrow;
15+
16+
void *free_list;
17+
};
18+
19+
typedef void *(*block_allocator_alloc_func)(size_t size, void *data);
20+
21+
struct block_allocator {
22+
block_allocator_alloc_func alloc_func;
23+
void *alloc_func_data;
24+
struct block_allocator_pool pools[MEMORY_BLOCK_ALLOCATOR_EXP];
25+
};
26+
27+
static inline int
28+
block_allocator_init(
29+
struct block_allocator *allocator,
30+
block_allocator_alloc_func alloc_func,
31+
void *alloc_func_data
32+
33+
) {
34+
allocator->alloc_func = alloc_func;
35+
allocator->alloc_func_data = alloc_func_data;
36+
37+
for (size_t pool_idx = 0; pool_idx < MEMORY_BLOCK_ALLOCATOR_EXP;
38+
++pool_idx) {
39+
allocator->pools[pool_idx].allocate = 0;
40+
allocator->pools[pool_idx].free = 0;
41+
allocator->pools[pool_idx].borrow = 0;
42+
allocator->pools[pool_idx].free_list = NULL;
43+
}
44+
45+
return 0;
46+
}
47+
48+
static inline size_t
49+
block_allocator_pool_size(
50+
struct block_allocator *allocator, size_t pool_index
51+
) {
52+
(void)allocator;
53+
54+
return 1 << (MEMORY_BLOCK_ALLOCATOR_MIN_BITS + pool_index);
55+
}
56+
57+
static inline size_t
58+
block_allocator_pool_index(struct block_allocator *allocator, size_t size) {
59+
(void)allocator;
60+
61+
size = (size << 1) - 1;
62+
size >>= MEMORY_BLOCK_ALLOCATOR_MIN_BITS;
63+
64+
return sizeof(long long) * 8 - 1 - __builtin_clzll(size);
65+
}
66+
67+
static inline void *
68+
block_allocator_pool_get(
69+
struct block_allocator *allocator, struct block_allocator_pool *pool
70+
) {
71+
(void)allocator;
72+
73+
void *result = pool->free_list;
74+
pool->free_list = *(void **)pool->free_list;
75+
++pool->allocate;
76+
--pool->free;
77+
return result;
78+
}
79+
80+
static inline void
81+
block_allocator_pool_borrow(
82+
struct block_allocator *allocator, size_t pool_index
83+
) {
84+
// Get a memory chunk from parent pool
85+
struct block_allocator_pool *parent_pool =
86+
allocator->pools + pool_index + 1;
87+
void *data = block_allocator_pool_get(allocator, parent_pool);
88+
89+
struct block_allocator_pool *pool = allocator->pools + pool_index;
90+
91+
// Split the memory chunk into two piece and insert into free list
92+
size_t size = block_allocator_pool_size(allocator, pool_index);
93+
void *next_data = (void *)((uintptr_t)data + size);
94+
*(void **)data = next_data;
95+
*(void **)next_data = pool->free_list;
96+
pool->free_list = data;
97+
98+
++parent_pool->borrow;
99+
pool->free += 2;
100+
}
101+
102+
static inline void *
103+
block_allocator_balloc(struct block_allocator *allocator, size_t size) {
104+
if (size < MEMORY_BLOCK_ALLOCATOR_MIN_SIZE)
105+
size = MEMORY_BLOCK_ALLOCATOR_MIN_SIZE;
106+
107+
if (size > MEMORY_BLOCK_ALLOCATOR_MAX_SIZE)
108+
return NULL;
109+
110+
size_t pool_index = block_allocator_pool_index(allocator, size);
111+
112+
struct block_allocator_pool *pool = allocator->pools + pool_index;
113+
114+
if (pool->free_list == NULL) {
115+
/*
116+
* Look for the first parent pool with free memory block
117+
* available and then recursively borrow memory block.
118+
*/
119+
size_t parent_pool_index = pool_index + 1;
120+
while (parent_pool_index < MEMORY_BLOCK_ALLOCATOR_EXP &&
121+
allocator->pools[parent_pool_index].free_list == NULL) {
122+
++parent_pool_index;
123+
}
124+
125+
if (parent_pool_index == MEMORY_BLOCK_ALLOCATOR_EXP) {
126+
size_t alloc_size = block_allocator_pool_size(
127+
allocator, parent_pool_index - 1
128+
);
129+
130+
void *data = allocator->alloc_func(
131+
alloc_size, allocator->alloc_func_data
132+
);
133+
if (data == NULL)
134+
return NULL;
135+
136+
struct block_allocator_pool *root =
137+
allocator->pools + MEMORY_BLOCK_ALLOCATOR_EXP -
138+
1;
139+
++root->free;
140+
root->free_list = data;
141+
*(void **)data = NULL;
142+
--parent_pool_index;
143+
}
144+
145+
while (parent_pool_index-- > pool_index) {
146+
block_allocator_pool_borrow(
147+
allocator, parent_pool_index
148+
);
149+
}
150+
}
151+
152+
return block_allocator_pool_get(allocator, pool);
153+
}
154+
155+
static inline void
156+
block_allocator_bfree(
157+
struct block_allocator *allocator, void *block, size_t size
158+
) {
159+
size_t pool_index = block_allocator_pool_index(allocator, size);
160+
struct block_allocator_pool *pool = allocator->pools + pool_index;
161+
162+
*(void **)block = pool->free_list;
163+
pool->free_list = block;
164+
++pool->free;
165+
}

common/meson.build

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies = []
2+
3+
lib_common_dep = declare_dependency(
4+
include_directories : include_directories('.'),
5+
)
6+

common/range_collector.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include <stdint.h>
44

5-
#include "common/key.h"
6-
#include "common/lpm.h"
7-
#include "common/radix.h"
5+
#include "key.h"
6+
#include "lpm.h"
7+
#include "radix.h"
88

99
struct range_collector {
1010
struct radix radix;

controlplane/main.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
3+
int
4+
main(int argc, char **argv) {
5+
(void)argc;
6+
(void)argv;
7+
8+
return 0;
9+
}

controlplane/meson.build

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
dependencies = []
2+
3+
dependencies += lib_common_dep
4+
dependencies += lib_filter_dep
5+
6+
lib_sources = files(
7+
'module.c')
8+
9+
lib_controlplane = static_library(
10+
'controlplane',
11+
lib_sources,
12+
dependencies: dependencies,
13+
install: false,
14+
)
15+
16+
lib_controlplane_dep = declare_dependency(
17+
link_with: lib_controlplane,
18+
include_directories: include_directories('.'),
19+
)
20+
21+
dependencies += lib_controlplane_dep
22+
23+
sources = files(
24+
'main.c',
25+
)
26+
27+
executable(
28+
'yanet-controlplane',
29+
sources,
30+
dependencies: dependencies,
31+
install: true,
32+
)
33+

controlplane/module.c

Whitespace-only changes.

0 commit comments

Comments
 (0)