summaryrefslogtreecommitdiff
path: root/common/src/tfe_session_table.cpp
blob: accd9ad439fb886379a6372af99d4e440cddafad (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
#include <assert.h>

#include "tfe_session_table.h"
#include <tfe_utils.h>

extern void *g_packet_io_logger;

struct session_table
{
    struct session_node *root_by_id;
    struct session_node *root_by_addr;
    uint64_t session_node_count;
};

// Note: session_addr must be initialized by memset(0) before use !!!

struct session_table *session_table_create()
{
    struct session_table *table = (struct session_table *)calloc(1, sizeof(struct session_table));
    assert(table);
    table->session_node_count = 0;

    return table;
}

void session_table_destory(struct session_table *table)
{
    if (table)
    {
        struct session_node *temp = NULL;
        struct session_node *node = NULL;
        HASH_ITER(hh1, table->root_by_id, node, temp)
        {
            HASH_DELETE(hh1, table->root_by_id, node);
            if (!node->is_session_id_only_key)
            {
                HASH_DELETE(hh2, table->root_by_addr, node);
            }

            if (node->val_freecb && node->val_data)
            {
                node->val_freecb(node->val_data);
            }

            free(node);
            node = NULL;
        }

        free(table);
        table = NULL;
    }
}

void session_table_reset(struct session_table *table)
{
    if (table)
    {
        struct session_node *temp = NULL;
        struct session_node *node = NULL;
        HASH_ITER(hh1, table->root_by_id, node, temp)
        {
            HASH_DELETE(hh1, table->root_by_id, node);
            if (!node->is_session_id_only_key)
            {
                HASH_DELETE(hh2, table->root_by_addr, node);
            }

            if (node->val_freecb && node->val_data)
            {
                node->val_freecb(node->val_data);
            }

            free(node);
            node = NULL;

            table->session_node_count--;
        }
    }
}

uint64_t session_table_count(struct session_table *table)
{
    if (table)
    {
        return table->session_node_count;
    }
    else
    {
        return 0;
    }
}

// session_addr : deep copy
// val_data     : shallow copy (malloc by user, free by val_freecb)
int session_table_insert(struct session_table *table, uint8_t is_session_id_only_key, uint64_t session_id, const struct tuple4 *session_addr, void *val_data, const fn_free_cb *val_freecb)
{
    struct session_node *temp = NULL;
    HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp);
    if (temp)
    {
        TFE_LOG_DEBUG(g_packet_io_logger, "%s: insert: key %lu exists", LOG_TAG_STABLE, session_id);
        return -1;
    }

    temp = (struct session_node *)calloc(1, sizeof(struct session_node));
    assert(temp);

    temp->is_session_id_only_key = is_session_id_only_key;
    temp->session_id = session_id;
    memcpy(&temp->session_addr, session_addr, sizeof(struct tuple4));
    temp->val_data = val_data;
    temp->val_freecb = val_freecb;

    HASH_ADD(hh1, table->root_by_id, session_id, sizeof(temp->session_id), temp);

    if (!is_session_id_only_key)
    {
        HASH_ADD(hh2, table->root_by_addr, session_addr, sizeof(temp->session_addr), temp);
    }

    TFE_LOG_DEBUG(g_packet_io_logger, "%s: insert: key %lu success", LOG_TAG_STABLE, session_id);
    table->session_node_count++;

    return 0;
}

int session_table_delete_by_id(struct session_table *table, uint64_t session_id)
{
    struct session_node *temp = NULL;
    HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp);
    if (!temp)
    {
        TFE_LOG_DEBUG(g_packet_io_logger, "%s: delete: key %lu not exists", LOG_TAG_STABLE, session_id);
        return -1;
    }

    HASH_DELETE(hh1, table->root_by_id, temp);
    if (!temp->is_session_id_only_key)
    {
        HASH_DELETE(hh2, table->root_by_addr, temp);
    }

    if (temp->val_freecb && temp->val_data)
    {
        temp->val_freecb(temp->val_data);
        temp->val_data = NULL;
    }

    free(temp);
    temp = NULL;

    TFE_LOG_DEBUG(g_packet_io_logger, "%s: delete: key %lu success", LOG_TAG_STABLE, session_id);
    table->session_node_count--;

    return 0;
}

int session_table_delete_by_addr(struct session_table *table, const struct tuple4 *session_addr)
{
    struct session_node *temp = NULL;
    char buffer[128] = {0};
    tuple4_tostring(session_addr, buffer, sizeof(buffer));
    HASH_FIND(hh2, table->root_by_addr, session_addr, sizeof(struct tuple4), temp);
    if (!temp)
    {
        struct tuple4 reverse_addr;
        memset(&reverse_addr, 0, sizeof(struct tuple4));
        tuple4_reverse(session_addr, &reverse_addr);
        HASH_FIND(hh2, table->root_by_addr, &reverse_addr, sizeof(struct tuple4), temp);
        if (!temp)
        {
            TFE_LOG_DEBUG(g_packet_io_logger, "%s: delete: key %s not exists", LOG_TAG_STABLE, buffer);
            return -1;
        }
    }

    HASH_DELETE(hh1, table->root_by_id, temp);
    HASH_DELETE(hh2, table->root_by_addr, temp);

    if (temp->val_freecb && temp->val_data)
    {
        temp->val_freecb(temp->val_data);
        temp->val_data = NULL;
    }

    free(temp);
    temp = NULL;

    TFE_LOG_DEBUG(g_packet_io_logger, "%s: delete: key %s success", LOG_TAG_STABLE, buffer);
    table->session_node_count--;

    return 0;
}

struct session_node *session_table_search_by_id(struct session_table *table, uint64_t session_id)
{
    struct session_node *temp = NULL;
    HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp);
    if (!temp)
    {
        TFE_LOG_DEBUG(g_packet_io_logger, "%s: search: key %lu not exists", LOG_TAG_STABLE, session_id);
        return NULL;
    }

    TFE_LOG_DEBUG(g_packet_io_logger, "%s: search: key %lu success", LOG_TAG_STABLE, session_id);

    return temp;
}

struct session_node *session_table_search_by_addr(struct session_table *table, const struct tuple4 *session_addr)
{
    struct session_node *temp = NULL;
    HASH_FIND(hh2, table->root_by_addr, session_addr, sizeof(struct tuple4), temp);
    if (!temp)
    {
        struct tuple4 reverse_addr;
        memset(&reverse_addr, 0, sizeof(struct tuple4));
        tuple4_reverse(session_addr, &reverse_addr);
        HASH_FIND(hh2, table->root_by_addr, &reverse_addr, sizeof(struct tuple4), temp);
        if (!temp)
        {
            return NULL;
        }
    }

    return temp;
}

void session_foreach(struct session_table *table, struct fieldstat_easy_intercept *metrics, int (*func)(struct fieldstat_easy_intercept *, void *, int), int thread_index)
{
    struct session_node *temp = NULL;
    struct session_node *node = NULL;

    if (!func)
        return;

    HASH_ITER(hh1, table->root_by_id, node, temp)
    {
        func(metrics, node->val_data, thread_index);
    }
    return;
}