-
Notifications
You must be signed in to change notification settings - Fork 4
/
ccp_priv.c
59 lines (46 loc) · 1.56 KB
/
ccp_priv.c
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
#include "ccp_priv.h"
#ifdef __KERNEL__
#include <linux/slab.h> // kmalloc
#include <linux/string.h> // memcpy,memset
#else
#include <stdlib.h>
#include <string.h>
#endif
int init_ccp_priv_state(struct ccp_datapath *datapath, struct ccp_connection *conn) {
struct ccp_priv_state *state;
conn->state = __CALLOC__(1, sizeof(struct ccp_priv_state));
state = (struct ccp_priv_state*) conn->state;
state->sent_create = false;
state->implicit_time_zero = datapath->time_zero;
state->program_index = 0;
state->staged_program_index = -1;
conn->datapath = datapath;
return 0;
}
void free_ccp_priv_state(struct ccp_connection *conn) {
struct ccp_priv_state *state = get_ccp_priv_state(conn);
__FREE__(state);
}
__INLINE__ struct ccp_priv_state* get_ccp_priv_state(struct ccp_connection *conn) {
return (struct ccp_priv_state*) conn->state;
}
// lookup datapath program using program ID
// returns NULL on error
struct DatapathProgram* datapath_program_lookup(struct ccp_datapath *datapath, u16 pid) {
struct DatapathProgram *prog;
struct DatapathProgram *programs = (struct DatapathProgram*) datapath->programs;
// bounds check
if (pid == 0) {
libccp_warn("no datapath program set\n");
return NULL;
} else if (pid > datapath->max_programs) {
libccp_warn("program index out of bounds: %d\n", pid);
return NULL;
}
prog = &programs[pid-1];
if (prog->index != pid) {
libccp_warn("index mismatch: pid %d, index %d", pid, prog->index);
return NULL;
}
return prog;
}