aboutsummaryrefslogtreecommitdiffstats
path: root/vpp/plugins/vcgn-plugin/vcgn/cnat_va_db.c
blob: 7423bdf2de2f7294257100ccb5b1aed2d5a196ce (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 *------------------------------------------------------------------
 * cnat_va_db.c - virtual assembly database
 *
 * Copyright (c) 2009, 2013 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cnat_va_db.h>
#include <format.h>
#include <spp_node.h>
#include <spp_alloc.h>
#include <spp_byteorder.h>
#include <spp_main.h>
#include <spp_cache.h>
#include <spp_interface.h>
#include <spp_api.h>
#include <spp_client_api.h>
#include <spp_timers.h>
#include <cnat_db.h>
#include <spp_plugin.h>
#include <cnat_v4_functions.h>


va_bucket_t va_bucket[VA_BUCKETS];

void va_bucket_init () {

    u32 i;

    /* 
     * set the pointer in each bucket
     * points to nowhere
     */
    for (i=0; i<VA_BUCKETS; i++) {
        va_bucket[i].next_available_entry = ~0;
    }

}

inline void va_db_add_new_entry (u32 bucket_index, 
                                 va_lookup_key * key ) 
{

    va_entry_t * entry_p;
    u32 head, next;

    entry_p = va_db_lookup(bucket_index, key);

    if (PREDICT_FALSE(entry_p)) { 
        FRAG_DEBUG_PRINTF6(
	  "\nVA_ADD_NEW: Bucket %d fnd Existng entry [%d, %d] -> [%d, %d]\n", 
	  bucket_index, entry_p->src_port, 
	  entry_p->dst_port, key->e.src_port, key->e.dst_port)

        /* found match entry, update it */
        entry_p->src_port = key->e.src_port; 
        entry_p->dst_port = key->e.dst_port; 

        FRAG_DEBUG_PRINTF3("VA_ADD_NEW: Existing bucket %d, counter %d\n", 
	                    bucket_index, 
			    va_bucket[bucket_index].new_entry_counter)

    } else { 
 
        /* no match, add a new one */
        head = va_bucket[bucket_index].head_entry;
        next = va_bucket[bucket_index].next_available_entry;

        FRAG_DEBUG_PRINTF5(
	    "\nVA_ADD_NEW: Filling bucket %d, index %d with key 0x%llx %x\n",
	    bucket_index, next, key->k.key64, key->k.key32)

        va_bucket[bucket_index].va_entry[next] = key->e;

        /* increase next pointer */
        va_bucket[bucket_index].next_available_entry = (next+1) & VA_BUCKET_MASK;

        if (PREDICT_FALSE(head == va_bucket[bucket_index].next_available_entry))  { 
            /* adjust head circular pointer */
            va_bucket[bucket_index].head_entry = (head+1) & VA_BUCKET_MASK;
        }

	va_bucket[bucket_index].new_entry_counter++;

        FRAG_DEBUG_PRINTF4(
	    "VA_ADD_NEW: NEW bucket %d, entry %d counter %d\n", 
	    bucket_index, next, va_bucket[bucket_index].new_entry_counter)
    }
} 


/* 
 * use the key, 
 * return pointer to the entry if found, 
 * NULL if not 
 */

inline 
va_entry_t * va_db_lookup (u32 bucket_index, va_lookup_key * key) 
{

    u32 index, next;
    va_entry_t * entry_p;
    va_bucket_t * bucket;
 
    bucket  = &va_bucket[bucket_index];
    index   = bucket->head_entry;
    next    = bucket->next_available_entry;
    entry_p = NULL;

    FRAG_DEBUG_PRINTF4(
        "\nVA_DB_LOOKUP: bucket index %d head %d next %d\n",
	bucket_index, index, next)

    /* loop through the entries in the bucket */
    while( index != next) {

        if(PREDICT_TRUE(memcmp(&bucket->va_entry[index], key, VA_KEY_SIZE)==0)) {

            entry_p = &bucket->va_entry[index];
	    /*In add frag entry function we are again assigning key's src 
	      port to entry_p's src port. So when a main DB entry is deleted/
	      timed out, and again another entry is created for the same
	      src ip and src port pair, the frag's entry_p will have the
	      previous port info stored and not updated. Hence the below 
	      line is not required*/
	      
            /* *(u32*)&key->e.src_port = *(u32*)&entry_p->src_port; */
	    /* do two ports as u32 :) */
	    
            break;
        }

        index = (index +1) & VA_BUCKET_MASK;

    }

#ifdef FRAG_DEBUG
    if (PREDICT_TRUE(entry_p)) {
        FRAG_DEBUG_PRINTF3("VA_DB_LOOKUP: bucket index %d entry index %d\n",
	                   bucket_index, index)
        FRAG_DEBUG_PRINTF5("VA_DB_LOOKUP: SRC-->DST [0x%x, %d] [0x%x, %d]\n",
	                    entry_p->src_ip, entry_p->src_port, 
			    entry_p->dst_ip, entry_p->dst_port)
        FRAG_DEBUG_PRINTF3("[vrf 0x%x, id 0x%x]\n", 
	                   entry_p->vrf, entry_p->ip_id)
    } else {
        FRAG_DEBUG_PRINTF1("\nNULL ENTRY\n")
    }
#endif

    return entry_p;

}

inline 
int va_db_delete_entry (u32 bucket_index, va_lookup_key * key) 
{

    u32 index, next;
    int entry_found = 0;
    va_bucket_t * bucket;
 
    bucket  = &va_bucket[bucket_index];
    index   = bucket->head_entry;
    next    = bucket->next_available_entry;

    FRAG_DEBUG_PRINTF4(
        "\nVA_DB_DELETE_ENTRY: bucket index %d head %d next %d\n",
	bucket_index, index, next);

    /* loop through the entries in the bucket */
    while( index != next) {
        if(PREDICT_TRUE(memcmp(&bucket->va_entry[index], key,
	                       VA_KEY_SIZE)==0)) {
            /* Clear the entry */
	    FRAG_DEBUG_PRINTF1("Entry found in delete API");
 	    memset(&bucket->va_entry[index], 0, sizeof(va_entry_t));
 	    entry_found = 1;
            break;
        }
        index = (index +1) & VA_BUCKET_MASK;
    }
    return entry_found;
}    
  


void cnat_va_bucket_used (int argc, unsigned long * argv) 
{

    u32 i, sum = 0;;

    for(i=0;i<VA_BUCKETS;i++)  {

        if(PREDICT_TRUE(va_bucket[i].new_entry_counter)) sum++;

    }

    if (PREDICT_FALSE(!sum)) {
        printf("no bucket in use\n");
        return;
    }

    printf("index head next counter (%d bucket in use)\n", sum);

    for(i=0;i<VA_BUCKETS;i++) {

        if (PREDICT_FALSE(!va_bucket[i].new_entry_counter)) continue; 

        printf(" %04d %04d %04d %d\n", i,
                va_bucket[i].head_entry,
                va_bucket[i].next_available_entry,
                va_bucket[i].new_entry_counter);

    }
}

void cnat_va_dump (int argc, unsigned long * argv) 
{

    u32 i, sum, index ;

    PLATFORM_DEBUG_PRINT("====== SUMMARY ======\n");
    PLATFORM_DEBUG_PRINT("Total buckets:      %d\n", VA_BUCKETS);
    PLATFORM_DEBUG_PRINT("Entries per bucket: %d\n", VA_ENTRY_PER_BUCKET);

    sum = 0;

    for(i=0; i<VA_BUCKETS; i++) {
        if (PREDICT_TRUE(va_bucket[i].new_entry_counter > 0)) sum ++;
    }

    PLATFORM_DEBUG_PRINT("buckets in use:     %d\n", sum); 

    sum = 0;
    for(i=0; i<VA_BUCKETS; i++) {

        if ( PREDICT_FALSE(((va_bucket[i].next_available_entry+1) & VA_BUCKET_MASK)  
               == va_bucket[i].head_entry)) {

            sum ++;
        }
    }

    PLATFORM_DEBUG_PRINT("bucket full:        %d\n", sum); 

    /* dump per bucket info */

    if (argc == 0 ) return;

    index = (u32) argv[0];

    if (PREDICT_FALSE(index >= VA_BUCKETS)) {
        PLATFORM_DEBUG_PRINT("invalid bucket index %d\n", index);
        return;
    }

    PLATFORM_DEBUG_PRINT("\n====== Bucket %d ======\n", index);

    PLATFORM_DEBUG_PRINT("bucket head index %d\n", va_bucket[index].head_entry);

    PLATFORM_DEBUG_PRINT("bucket next index %d\n", va_bucket[index].next_available_entry);

    PLATFORM_DEBUG_PRINT(" source IP     dest IP     VRF  ip-id   srcP  dstP\n");

    for(i=0;i<VA_ENTRY_PER_BUCKET;i++) {
        hex_dump((u8*)&va_bucket[index].va_entry[i], sizeof(va_entry_t));
    }

}