-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpcc_table.c
402 lines (360 loc) · 9.38 KB
/
pcc_table.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* SPDX-License-Identifier: Apache-2.0
* Copyright(c) 2017 Intel Corporation
*/
#include <rte_mbuf.h>
#include "vepc_cp_dp_api.h"
#include "main.h"
#include "util.h"
#include "acl_dp.h"
#include "meter.h"
#include "interface.h"
#include "structs.h"
struct rte_hash *rte_pcc_hash;
extern struct rte_hash *rte_sdf_pcc_hash;
extern struct rte_hash *rte_adc_pcc_hash;
/**
* @brief Called by DP to lookup key-value in PCC table.
*
* This function is thread safe (Read Only).
*/
int iface_lookup_pcc_data(const uint32_t key32,
struct dp_pcc_rules **value)
{
return rte_hash_lookup_data(rte_pcc_hash, &key32, (void **)value);
}
int
dp_pcc_table_create(struct dp_id dp_id, uint32_t max_elements)
{
if (rte_pcc_hash) {
RTE_LOG_DP(INFO, DP, "PCC table: \"%s\" exist\n", dp_id.name);
return 0;
}
return hash_create(dp_id.name, &rte_pcc_hash, max_elements * 4,
sizeof(uint32_t));
}
int
dp_pcc_table_delete(struct dp_id dp_id)
{
RTE_SET_USED(dp_id);
rte_hash_free(rte_pcc_hash);
return 0;
}
int
dp_pcc_entry_add(struct dp_id dp_id, struct pcc_rules *entry)
{
struct dp_pcc_rules *pcc;
uint32_t key32;
int ret;
pcc = rte_zmalloc("data", sizeof(struct dp_pcc_rules),
RTE_CACHE_LINE_SIZE);
if (pcc == NULL)
return -1;
memcpy(pcc, entry, sizeof(struct pcc_rules));
key32 = entry->rule_id;
ret = rte_hash_add_key_data(rte_pcc_hash, &key32,
pcc);
if (ret < 0) {
RTE_LOG_DP(ERR, DP, "Failed to add entry in hash table");
return -1;
}
RTE_LOG_DP(INFO, DP, "PCC_TBL ADD: rule_id:%u, addr:0x%"PRIx64
", ul_mtr_idx:%u, dl_mtr_idx:%u, sdf_cnt=%d, adc_idx=%d\n",
pcc->rule_id, (uint64_t)pcc,
pcc->qos.ul_mtr_profile_index,
pcc->qos.dl_mtr_profile_index, pcc->sdf_idx_cnt, pcc->adc_idx);
/*If there are no SDF indices send(count <0) then ADC parameters are passed.
* Either ADC or SDF will be passed.*/
if (entry->sdf_idx_cnt > 0)
filter_pcc_entry_add(FILTER_SDF, key32, entry->precedence,
entry->gate_status, entry->sdf_idx_cnt, entry->sdf_idx);
else
filter_pcc_entry_add(FILTER_ADC, key32, entry->precedence,
entry->gate_status, 1, &entry->adc_idx);
return 0;
}
int
dp_pcc_entry_delete(struct dp_id dp_id, struct pcc_rules *entry)
{
struct dp_pcc_rules *pcc;
uint32_t key32;
int ret;
key32 = entry->rule_id;
ret = rte_hash_lookup_data(rte_pcc_hash, &key32,
(void **)&pcc);
if (ret < 0) {
RTE_LOG_DP(ERR, DP, "Failed to del\n"
"pcc key 0x%x to hash table\n",
key32);
return -1;
}
ret = rte_hash_del_key(rte_pcc_hash, &key32);
if (ret < 0)
return -1;
rte_free(pcc);
return 0;
}
/******************** Call back functions **********************/
/**
* Call back to parse msg to create pcc rules table
*
* @param msg_payload
* payload from CP
* @return
* - 0 Success.
* - -1 Failure.
*/
static int
cb_pcc_table_create(struct msgbuf *msg_payload)
{
return pcc_table_create(msg_payload->dp_id,
msg_payload->msg_union.msg_table.max_elements);
}
/**
* Call back to parse msg to delete table
*
* @param msg_payload
* payload from CP
* @return
* - 0 Success.
* - -1 Failure.
*/
static int
cb_pcc_table_delete(struct msgbuf *msg_payload)
{
return pcc_table_delete(msg_payload->dp_id);
}
/**
* Call back to parse msg to add pcc rules.
*
* @param msg_payload
* payload from CP
* @return
* - 0 Success.
* - -1 Failure.
*/
static int
cb_pcc_entry_add(struct msgbuf *msg_payload)
{
return pcc_entry_add(msg_payload->dp_id,
msg_payload->msg_union.pcc_entry);
}
/**
* Call back to delete pcc rules.
* @param msg_payload
* payload from CP
* @return
* - 0 Success.
* - -1 Failure.
*/
static int
cb_pcc_entry_delete(struct msgbuf *msg_payload)
{
return pcc_entry_delete(msg_payload->dp_id,
msg_payload->msg_union.pcc_entry);
}
/**
* Initialization of PCC Table Callback functions.
*/
void app_pcc_tbl_init(void)
{
/* register msg type in DB*/
iface_ipc_register_msg_cb(MSG_PCC_TBL_CRE, cb_pcc_table_create);
iface_ipc_register_msg_cb(MSG_PCC_TBL_DES, cb_pcc_table_delete);
iface_ipc_register_msg_cb(MSG_PCC_TBL_ADD, cb_pcc_entry_add);
iface_ipc_register_msg_cb(MSG_PCC_TBL_DEL, cb_pcc_entry_delete);
}
/**
* Returns insertion position in sorted array, after moving elements.
* Capacity of pcc should be n+1
* @param pcc
* Pointer to pcc_id_precedence structure
* @param n
* no of entries in pcc
* @param precedence
* Comparison is based on precedence.
* @return
* Insert position for new element.
*/
static uint32_t
get_insert_position(struct pcc_id_precedence *pcc, uint32_t n,
uint32_t precedence)
{
int i;
for(i = n-1; (i >= 0 && pcc[i].precedence <= precedence); i--)
pcc[i+1] = pcc[i];
return i+1;
}
/**
* Returns delete position in sorted array, after moving elements.
* Capacity of pcc should be n-1
* @param pcc
* Pointer to pcc_id_precedence structure
* @param n
* no of entries in pcc
* @param precedence
* Comparison is based on precedence.
* @return
* Insert position for new element.
*/
/* --> GCC_security flag */
#if 0
static uint32_t
get_delete_position(struct pcc_id_precedence *pcc, uint32_t n,
uint32_t pcc_id)
{
int i;
for(i = n-1; (i >= 0 && pcc[i].pcc_id == pcc_id); i--)
return i;
return -1;
}
#endif
/* <-- GCC_security flag */
/**
* Add entry into SDF-PCC or ADC-PCC association hash.
* @param type
* Type of hash table, SDF/ADC.
* @param pcc_id
* PCC rule id to be added.
* @param precedence
* PCC rule precedence.
* @param gate_status
* PCC rule gate status.
* @param n
* Number of SDF/ADC rules.
* @param rule_ids
* Pointer to SDF/ADC rule ids.
*
* @return
* 0 - on success
* -1 - on failure
*/
int
filter_pcc_entry_add(enum filter_pcc_type type, uint32_t pcc_id,
uint32_t precedence, uint8_t gate_status, uint32_t n,
uint32_t rule_ids[])
{
int ret;
uint32_t i;
struct filter_pcc_data *pinfo = NULL;
struct rte_hash *hash = NULL;
if (type == FILTER_SDF)
hash = rte_sdf_pcc_hash;
else if (type == FILTER_ADC)
hash = rte_adc_pcc_hash;
else
return -1;
for (i = 0; i < n; i++) {
ret = rte_hash_lookup_data(hash, &rule_ids[i], (void **)&pinfo);
if (ret < 0 || pinfo == NULL) {
/* No data found for sdf id, insert new entry*/
struct pcc_id_precedence *pcc = rte_zmalloc("pcc_id_preced",
sizeof(struct pcc_id_precedence), RTE_CACHE_LINE_SIZE);
if (pcc == NULL)
rte_panic("Failed to allocate memory for pcc_id_precedence");
pcc->pcc_id = pcc_id;
pcc->precedence = precedence;
pcc->gate_status = gate_status;
struct filter_pcc_data *data = rte_zmalloc("filter_pcc_data",
sizeof(struct filter_pcc_data), RTE_CACHE_LINE_SIZE);
if (data == NULL) {
rte_free(pcc);
rte_panic("Failed to allocate memory for filter_pcc_data");
}
data->entries = 1;
data->pcc_info = pcc;
ret = rte_hash_add_key_data(hash, &rule_ids[i], data);
if (ret < 0) {
rte_free(pcc);
rte_free(data);
RTE_LOG_DP(DEBUG, DP, "Failed to add entry in rte_sdf_pcc hash.\n");
continue;
}
} else {
struct pcc_id_precedence *pcc = rte_zmalloc("pcc_id_precedence",
(pinfo->entries + 1) * sizeof(struct pcc_id_precedence),
RTE_CACHE_LINE_SIZE);
if (pcc == NULL)
rte_panic("Failed to allocate memory for pcc_id_precedence");
rte_memcpy(pcc, pinfo->pcc_info,
(pinfo->entries) * sizeof(struct pcc_id_precedence));
uint32_t insert_pos = get_insert_position(pcc, pinfo->entries,
precedence);
pcc[insert_pos].pcc_id = pcc_id;
pcc[insert_pos].precedence = precedence;
pcc[insert_pos].gate_status = gate_status;
struct filter_pcc_data *data = rte_zmalloc("filter_pcc_data",
sizeof(struct filter_pcc_data), RTE_CACHE_LINE_SIZE);
if (data == NULL)
rte_panic("Failed to allocate memory for filter_pcc_data");
data->entries = pinfo->entries + 1;
data->pcc_info = pcc;
ret = rte_hash_del_key(hash, &rule_ids[i]);
if (ret < 0) {
rte_free(pcc);
rte_free(data);
RTE_LOG_DP(DEBUG, DP, "Failed to delete from rte_sdf_pcc hash\n");
continue;
}
rte_free(pinfo->pcc_info);
rte_free(pinfo);
pinfo = NULL;
ret = rte_hash_add_key_data(hash,
&rule_ids[i], data);
if (ret < 0) {
rte_free(pcc);
rte_free(data);
RTE_LOG_DP(DEBUG, DP,
"Failed to add entry in sdf_pcc hash table\n");
continue;
}
}
}
return 0;
}
/**
* Search SDF-PCC or ADC-PCC association hash for SDF/ADC ruleid as a key
* @param type
* Type of hash table, SDF/ADC.
* @param pcc_id
* SDF/ADC rule ids to be used for searching.
* @param n
* Number of SDF/ADC rules.
* @param pcc_info
* Pointer to matched PCC info.
*
* @return
* 0 - on success
* -1 - on failure
*/
int
filter_pcc_entry_lookup(enum filter_pcc_type type, uint32_t* rule_ids,
uint32_t n, struct pcc_id_precedence *pcc_ids)
{
int ret = 0;
uint32_t i;
struct filter_pcc_data *pinfo = NULL;
struct rte_hash *hash = NULL;
if (type == FILTER_SDF)
hash = rte_sdf_pcc_hash;
else if (type == FILTER_ADC)
hash = rte_adc_pcc_hash;
else {
RTE_LOG_DP(INFO, DP, "filter_pcc_entry_lookup hash type mistmatch");
return -1;
}
for (i = 0; i < n; i++) {
ret = rte_hash_lookup_data(hash, &rule_ids[i], (void **)&pinfo);
if (ret < 0 || pinfo == NULL) {
/* TODO : If there is no matching pcc rule, what should be
* values of pcc? Currently hardcoding to 1 with
* gate-status 1 (pass traffic) : Default policy in pcc
*/
pcc_ids[i].pcc_id = 1;
pcc_ids[i].precedence = 255;
pcc_ids[i].gate_status = 1;
} else {
pcc_ids[i] = pinfo->pcc_info[pinfo->entries - 1];
}
}
return 0;
}