-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcmdFlowLb-test.c
More file actions
230 lines (200 loc) · 5.58 KB
/
Copy pathcmdFlowLb-test.c
File metadata and controls
230 lines (200 loc) · 5.58 KB
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
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <flow.h>
#include <maglevdyn.h>
#include <shmem.h>
#include <nfqueue.h>
#include "nfqlb.h"
extern struct FlowSet* fset;
extern struct LoadBalancer* lblist;
extern void loadbalancerLock(void* user_ref);
extern void loadbalancerRelease(struct LoadBalancer* lb);
extern struct LoadBalancer* loadbalancerFindOrCreate(char const* target);
extern void cmd_set(struct FlowCmd* cmd, int cd);
extern void cmd_delete(struct FlowCmd* cmd, int cd);
// COPIED FROM cmdFlowLb.c. KEEP IN SYNC!
struct LoadBalancer {
struct LoadBalancer* next;
int refCounter;
char* target;
int fd;
struct SharedData* st;
struct MagDataDyn magd;
};
static void initShm(char const* name, int ownFw, unsigned m, unsigned n);
static int countLb(void);
static int readResult(int fd);
int main(int argc, char* argv[])
{
struct LoadBalancer* lb;
struct FlowCmd cmd;
int pipe[2];
char* protocols[2];
// Init
shm_unlink("lb100");
shm_unlink("lb200");
assert(pipe2(pipe, O_NONBLOCK) == 0);
fset = flowSetCreate(loadbalancerLock);
// LB handling
assert(lblist == NULL);
assert(loadbalancerFindOrCreate("lb100") == NULL);
initShm("lb100", 0, 17, 2);
initShm("lb200", 0, 17, 2);
lb = loadbalancerFindOrCreate("lb100");
assert(lb != NULL);
assert(countLb() == 1);
loadbalancerRelease(lb);
assert(countLb() == 0);
lb = loadbalancerFindOrCreate("lb100");
assert(lb != NULL);
assert(loadbalancerFindOrCreate("lb100") == lb);
assert(countLb() == 1);
loadbalancerRelease(lb);
assert(countLb() == 1);
loadbalancerRelease(lb);
assert(countLb() == 0);
// Flows
memset(&cmd, 0, sizeof(cmd));
cmd.name = "lb100";
cmd.target = "lb100";
cmd_set(&cmd, pipe[1]);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
assert(readResult(pipe[0]) == 0);
assert(flowSetSize(fset) == 1);
// Re-define must not increment the refCounter (issue #9)
cmd_set(&cmd, pipe[1]);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
assert(readResult(pipe[0]) == 0);
cmd.name = "lb100-2";
cmd_set(&cmd, pipe[1]);
assert(readResult(pipe[0]) == 0);
assert(countLb() == 1);
assert(lblist->refCounter == 2);
cmd_delete(&cmd, pipe[1]);
assert(readResult(pipe[0]) == 0);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
cmd.name = "lb100";
cmd_delete(&cmd, pipe[1]);
assert(readResult(pipe[0]) == 0);
assert(countLb() == 0);
assert(lblist == NULL);
// Flows with udpencap
memset(&cmd, 0, sizeof(cmd));
cmd.name = "lb100";
cmd.target = "lb100";
protocols[0] = "sctp";
protocols[1] = NULL;
cmd.protocols = (const char**)protocols;
cmd.dports = "2000";
cmd.udpencap = 9000;
cmd_set(&cmd, pipe[1]);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
assert(readResult(pipe[0]) == 0);
assert(flowSetSize(fset) == 2);
cmd_delete(&cmd, pipe[1]);
assert(readResult(pipe[0]) == 0);
assert(countLb() == 0);
assert(lblist == NULL);
assert(flowSetSize(fset) == 0);
// Re-define a udpencap and delete
cmd_set(&cmd, pipe[1]);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
assert(readResult(pipe[0]) == 0);
assert(flowSetSize(fset) == 2);
cmd_set(&cmd, pipe[1]);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
assert(readResult(pipe[0]) == 0);
assert(flowSetSize(fset) == 2);
cmd_delete(&cmd, pipe[1]);
assert(readResult(pipe[0]) == 0);
assert(countLb() == 0);
assert(lblist == NULL);
assert(flowSetSize(fset) == 0);
// Re-define the upd-port should fail
cmd_set(&cmd, pipe[1]);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
assert(readResult(pipe[0]) == 0);
assert(flowSetSize(fset) == 2);
cmd.udpencap = 8000;
cmd_set(&cmd, pipe[1]);
assert(readResult(pipe[0]) != 0);
cmd_delete(&cmd, pipe[1]);
assert(readResult(pipe[0]) == 0);
assert(countLb() == 0);
assert(lblist == NULL);
assert(flowSetSize(fset) == 0);
// Target re-define
memset(&cmd, 0, sizeof(cmd));
cmd.name = "lb100";
cmd.target = "lb100";
cmd_set(&cmd, pipe[1]);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
assert(readResult(pipe[0]) == 0);
assert(flowSetSize(fset) == 1);
cmd.target = "WRONG";
cmd_set(&cmd, pipe[1]);
assert(readResult(pipe[0]) != 0);
assert(lblist->refCounter == 1);
assert(flowSetSize(fset) == 1);
assert(countLb() == 1);
cmd.target = "lb200";
cmd_set(&cmd, pipe[1]);
assert(countLb() == 1);
assert(lblist->refCounter == 1);
assert(readResult(pipe[0]) == 0);
assert(flowSetSize(fset) == 1);
cmd_delete(&cmd, pipe[1]);
assert(readResult(pipe[0]) == 0);
assert(countLb() == 0);
assert(lblist == NULL);
assert(flowSetSize(fset) == 0);
// Clean-up
assert(shm_unlink("lb100") == 0);
assert(shm_unlink("lb200") == 0);
printf("=== cmdFlowLb-test; OK\n");
return 0;
}
static int readResult(int fd)
{
char buff[64];
int rc = read(fd, buff, sizeof(buff));
//printf("rc=%d, response [%s]\n", rc, buff);
if (rc < 3) return -1;
if (rc > 3) printf("response [%s], rc=%d\n", buff, rc);
if (strncmp(buff, "OK", 2) != 0)
return -1;
return 0;
}
static int countLb(void)
{
int cnt = 0;
struct LoadBalancer* lb;
for (lb = lblist; lb != NULL; lb = lb->next) cnt++;
return cnt;
}
static void initShm(
char const* name, int ownFw, unsigned m, unsigned n)
{
unsigned len = magDataDyn_len(m, n);
struct SharedData* s = malloc(sizeof(struct SharedData) + len);
s->ownFwmark = ownFw;
createSharedDataOrDie(name, s, sizeof(struct SharedData) + len);
free(s);
s = mapSharedDataOrDie(name, O_RDWR, NULL);
magDataDyn_init(m, n, s->mem, len);
}
void freeFlowCmd(struct FlowCmd* cmd){}
int readFlowCmd(FILE* in, struct FlowCmd* cmd) { return 0; }