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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
// SPDX-License-Identifier: GPL-2.0+
/* Microchip VCAP API
*
* Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
*/
#include <linux/types.h>
#include "vcap_api.h"
#include "vcap_api_client.h"
#define to_intrule(rule) container_of((rule), struct vcap_rule_internal, data)
/* Private VCAP API rule data */
struct vcap_rule_internal {
struct vcap_rule data; /* provided by the client */
struct list_head list; /* for insertion in the vcap admin list of rules */
struct vcap_admin *admin; /* vcap hw instance */
struct net_device *ndev; /* the interface that the rule applies to */
struct vcap_control *vctrl; /* the client control */
u32 addr; /* address in the VCAP at insertion */
};
/* Return the list of keyfields for the keyset */
static const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl,
enum vcap_type vt,
enum vcap_keyfield_set keyset)
{
/* Check that the keyset exists in the vcap keyset list */
if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
return NULL;
return vctrl->vcaps[vt].keyfield_set_map[keyset];
}
/* Update the keyset for the rule */
int vcap_set_rule_set_keyset(struct vcap_rule *rule,
enum vcap_keyfield_set keyset)
{
/* This will be expanded with more information later */
rule->keyset = keyset;
return 0;
}
EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset);
/* Update the actionset for the rule */
int vcap_set_rule_set_actionset(struct vcap_rule *rule,
enum vcap_actionfield_set actionset)
{
/* This will be expanded with more information later */
rule->actionset = actionset;
return 0;
}
EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset);
/* Find a rule with a provided rule id */
static struct vcap_rule_internal *vcap_lookup_rule(struct vcap_control *vctrl,
u32 id)
{
struct vcap_rule_internal *ri;
struct vcap_admin *admin;
/* Look for the rule id in all vcaps */
list_for_each_entry(admin, &vctrl->list, list)
list_for_each_entry(ri, &admin->rules, list)
if (ri->data.id == id)
return ri;
return NULL;
}
/* Find a rule id with a provided cookie */
int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
{
struct vcap_rule_internal *ri;
struct vcap_admin *admin;
/* Look for the rule id in all vcaps */
list_for_each_entry(admin, &vctrl->list, list)
list_for_each_entry(ri, &admin->rules, list)
if (ri->data.cookie == cookie)
return ri->data.id;
return -ENOENT;
}
EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
/* Lookup a vcap instance using chain id */
struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
{
struct vcap_admin *admin;
list_for_each_entry(admin, &vctrl->list, list) {
if (cid >= admin->first_cid && cid <= admin->last_cid)
return admin;
}
return NULL;
}
EXPORT_SYMBOL_GPL(vcap_find_admin);
/* Validate a rule with respect to available port keys */
int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
{
struct vcap_rule_internal *ri = to_intrule(rule);
/* This validation will be much expanded later */
if (!ri->admin) {
ri->data.exterr = VCAP_ERR_NO_ADMIN;
return -EINVAL;
}
if (!ri->ndev) {
ri->data.exterr = VCAP_ERR_NO_NETDEV;
return -EINVAL;
}
if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
return -EINVAL;
}
if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
return -EINVAL;
}
return 0;
}
EXPORT_SYMBOL_GPL(vcap_val_rule);
/* Assign a unique rule id and autogenerate one if id == 0 */
static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
{
u32 next_id;
if (ri->data.id != 0)
return ri->data.id;
next_id = ri->vctrl->rule_id + 1;
for (next_id = ri->vctrl->rule_id + 1; next_id < ~0; ++next_id) {
if (!vcap_lookup_rule(ri->vctrl, next_id)) {
ri->data.id = next_id;
ri->vctrl->rule_id = next_id;
break;
}
}
return ri->data.id;
}
/* Encode and write a validated rule to the VCAP */
int vcap_add_rule(struct vcap_rule *rule)
{
/* This will later handling the encode and writing of the rule */
return 0;
}
EXPORT_SYMBOL_GPL(vcap_add_rule);
/* Allocate a new rule with the provided arguments */
struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
struct net_device *ndev, int vcap_chain_id,
enum vcap_user user, u16 priority,
u32 id)
{
struct vcap_rule_internal *ri;
struct vcap_admin *admin;
if (!ndev)
return ERR_PTR(-ENODEV);
/* Get the VCAP instance */
admin = vcap_find_admin(vctrl, vcap_chain_id);
if (!admin)
return ERR_PTR(-ENOENT);
/* Create a container for the rule and return it */
ri = kzalloc(sizeof(*ri), GFP_KERNEL);
if (!ri)
return ERR_PTR(-ENOMEM);
ri->data.vcap_chain_id = vcap_chain_id;
ri->data.user = user;
ri->data.priority = priority;
ri->data.id = id;
ri->data.keyset = VCAP_KFS_NO_VALUE;
ri->data.actionset = VCAP_AFS_NO_VALUE;
INIT_LIST_HEAD(&ri->list);
INIT_LIST_HEAD(&ri->data.keyfields);
INIT_LIST_HEAD(&ri->data.actionfields);
ri->ndev = ndev;
ri->admin = admin; /* refer to the vcap instance */
ri->vctrl = vctrl; /* refer to the client */
if (vcap_set_rule_id(ri) == 0)
goto out_free;
return (struct vcap_rule *)ri;
out_free:
kfree(ri);
return ERR_PTR(-EINVAL);
}
EXPORT_SYMBOL_GPL(vcap_alloc_rule);
/* Free mem of a rule owned by client after the rule as been added to the VCAP */
void vcap_free_rule(struct vcap_rule *rule)
{
struct vcap_rule_internal *ri = to_intrule(rule);
struct vcap_client_actionfield *caf, *next_caf;
struct vcap_client_keyfield *ckf, *next_ckf;
/* Deallocate the list of keys and actions */
list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
list_del(&ckf->ctrl.list);
kfree(ckf);
}
list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
list_del(&caf->ctrl.list);
kfree(caf);
}
/* Deallocate the rule */
kfree(rule);
}
EXPORT_SYMBOL_GPL(vcap_free_rule);
/* Delete rule in a VCAP instance */
int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
{
struct vcap_rule_internal *ri, *elem;
struct vcap_admin *admin;
/* This will later also handle rule moving */
if (!ndev)
return -ENODEV;
/* Look for the rule id in all vcaps */
ri = vcap_lookup_rule(vctrl, id);
if (!ri)
return -EINVAL;
admin = ri->admin;
list_del(&ri->list);
if (list_empty(&admin->rules)) {
admin->last_used_addr = admin->last_valid_addr;
} else {
/* update the address range end marker from the last rule in the list */
elem = list_last_entry(&admin->rules, struct vcap_rule_internal, list);
admin->last_used_addr = elem->addr;
}
kfree(ri);
return 0;
}
EXPORT_SYMBOL_GPL(vcap_del_rule);
/* Find information on a key field in a rule */
const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
enum vcap_key_field key)
{
struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule;
enum vcap_keyfield_set keyset = rule->keyset;
enum vcap_type vt = ri->admin->vtype;
const struct vcap_field *fields;
if (keyset == VCAP_KFS_NO_VALUE)
return NULL;
fields = vcap_keyfields(ri->vctrl, vt, keyset);
if (!fields)
return NULL;
return &fields[key];
}
EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
static void vcap_copy_from_client_keyfield(struct vcap_rule *rule,
struct vcap_client_keyfield *field,
struct vcap_client_keyfield_data *data)
{
/* This will be expanded later to handle different vcap memory layouts */
memcpy(&field->data, data, sizeof(field->data));
}
static int vcap_rule_add_key(struct vcap_rule *rule,
enum vcap_key_field key,
enum vcap_field_type ftype,
struct vcap_client_keyfield_data *data)
{
struct vcap_client_keyfield *field;
/* More validation will be added here later */
field = kzalloc(sizeof(*field), GFP_KERNEL);
if (!field)
return -ENOMEM;
field->ctrl.key = key;
field->ctrl.type = ftype;
vcap_copy_from_client_keyfield(rule, field, data);
list_add_tail(&field->ctrl.list, &rule->keyfields);
return 0;
}
static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
{
switch (val) {
case VCAP_BIT_0:
u1->value = 0;
u1->mask = 1;
break;
case VCAP_BIT_1:
u1->value = 1;
u1->mask = 1;
break;
case VCAP_BIT_ANY:
u1->value = 0;
u1->mask = 0;
break;
}
}
/* Add a bit key with value and mask to the rule */
int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
enum vcap_bit val)
{
struct vcap_client_keyfield_data data;
vcap_rule_set_key_bitsize(&data.u1, val);
return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
}
EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
/* Add a 32 bit key field with value and mask to the rule */
int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
u32 value, u32 mask)
{
struct vcap_client_keyfield_data data;
data.u32.value = value;
data.u32.mask = mask;
return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
}
EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
/* Add a 48 bit key with value and mask to the rule */
int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
struct vcap_u48_key *fieldval)
{
struct vcap_client_keyfield_data data;
memcpy(&data.u48, fieldval, sizeof(data.u48));
return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
}
EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
/* Add a 72 bit key with value and mask to the rule */
int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
struct vcap_u72_key *fieldval)
{
struct vcap_client_keyfield_data data;
memcpy(&data.u72, fieldval, sizeof(data.u72));
return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
}
EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
static void vcap_copy_from_client_actionfield(struct vcap_rule *rule,
struct vcap_client_actionfield *field,
struct vcap_client_actionfield_data *data)
{
/* This will be expanded later to handle different vcap memory layouts */
memcpy(&field->data, data, sizeof(field->data));
}
static int vcap_rule_add_action(struct vcap_rule *rule,
enum vcap_action_field action,
enum vcap_field_type ftype,
struct vcap_client_actionfield_data *data)
{
struct vcap_client_actionfield *field;
/* More validation will be added here later */
field = kzalloc(sizeof(*field), GFP_KERNEL);
if (!field)
return -ENOMEM;
field->ctrl.action = action;
field->ctrl.type = ftype;
vcap_copy_from_client_actionfield(rule, field, data);
list_add_tail(&field->ctrl.list, &rule->actionfields);
return 0;
}
static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
enum vcap_bit val)
{
switch (val) {
case VCAP_BIT_0:
u1->value = 0;
break;
case VCAP_BIT_1:
u1->value = 1;
break;
case VCAP_BIT_ANY:
u1->value = 0;
break;
}
}
/* Add a bit action with value to the rule */
int vcap_rule_add_action_bit(struct vcap_rule *rule,
enum vcap_action_field action,
enum vcap_bit val)
{
struct vcap_client_actionfield_data data;
vcap_rule_set_action_bitsize(&data.u1, val);
return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
}
EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
/* Add a 32 bit action field with value to the rule */
int vcap_rule_add_action_u32(struct vcap_rule *rule,
enum vcap_action_field action,
u32 value)
{
struct vcap_client_actionfield_data data;
data.u32.value = value;
return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
}
EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
/* Copy to host byte order */
void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
{
int idx;
for (idx = 0; idx < count; ++idx, ++dst)
*dst = src[count - idx - 1];
}
EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
/* Convert validation error code into tc extact error message */
void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
{
switch (vrule->exterr) {
case VCAP_ERR_NONE:
break;
case VCAP_ERR_NO_ADMIN:
NL_SET_ERR_MSG_MOD(fco->common.extack,
"Missing VCAP instance");
break;
case VCAP_ERR_NO_NETDEV:
NL_SET_ERR_MSG_MOD(fco->common.extack,
"Missing network interface");
break;
case VCAP_ERR_NO_KEYSET_MATCH:
NL_SET_ERR_MSG_MOD(fco->common.extack,
"No keyset matched the filter keys");
break;
case VCAP_ERR_NO_ACTIONSET_MATCH:
NL_SET_ERR_MSG_MOD(fco->common.extack,
"No actionset matched the filter actions");
break;
case VCAP_ERR_NO_PORT_KEYSET_MATCH:
NL_SET_ERR_MSG_MOD(fco->common.extack,
"No port keyset matched the filter keys");
break;
}
}
EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
|