1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
7 #include <linux/types.h>
9 #include "vcap_api_private.h"
11 static int keyfield_size_table[] = {
12 [VCAP_FIELD_BIT] = sizeof(struct vcap_u1_key),
13 [VCAP_FIELD_U32] = sizeof(struct vcap_u32_key),
14 [VCAP_FIELD_U48] = sizeof(struct vcap_u48_key),
15 [VCAP_FIELD_U56] = sizeof(struct vcap_u56_key),
16 [VCAP_FIELD_U64] = sizeof(struct vcap_u64_key),
17 [VCAP_FIELD_U72] = sizeof(struct vcap_u72_key),
18 [VCAP_FIELD_U112] = sizeof(struct vcap_u112_key),
19 [VCAP_FIELD_U128] = sizeof(struct vcap_u128_key),
22 static int actionfield_size_table[] = {
23 [VCAP_FIELD_BIT] = sizeof(struct vcap_u1_action),
24 [VCAP_FIELD_U32] = sizeof(struct vcap_u32_action),
25 [VCAP_FIELD_U48] = sizeof(struct vcap_u48_action),
26 [VCAP_FIELD_U56] = sizeof(struct vcap_u56_action),
27 [VCAP_FIELD_U64] = sizeof(struct vcap_u64_action),
28 [VCAP_FIELD_U72] = sizeof(struct vcap_u72_action),
29 [VCAP_FIELD_U112] = sizeof(struct vcap_u112_action),
30 [VCAP_FIELD_U128] = sizeof(struct vcap_u128_action),
33 /* Moving a rule in the VCAP address space */
34 struct vcap_rule_move {
35 int addr; /* address to move */
36 int offset; /* change in address */
37 int count; /* blocksize of addresses to move */
40 /* Stores the filter cookie and chain id that enabled the port */
41 struct vcap_enabled_port {
42 struct list_head list; /* for insertion in enabled ports list */
43 struct net_device *ndev; /* the enabled port */
44 unsigned long cookie; /* filter that enabled the port */
45 int src_cid; /* source chain id */
46 int dst_cid; /* destination chain id */
49 void vcap_iter_set(struct vcap_stream_iter *itr, int sw_width,
50 const struct vcap_typegroup *tg, u32 offset)
52 memset(itr, 0, sizeof(*itr));
54 itr->sw_width = sw_width;
55 itr->regs_per_sw = DIV_ROUND_UP(sw_width, 32);
59 static void vcap_iter_skip_tg(struct vcap_stream_iter *itr)
61 /* Compensate the field offset for preceding typegroups.
62 * A typegroup table ends with an all-zero terminator.
64 while (itr->tg->width && itr->offset >= itr->tg->offset) {
65 itr->offset += itr->tg->width;
66 itr->tg++; /* next typegroup */
70 void vcap_iter_update(struct vcap_stream_iter *itr)
72 int sw_idx, sw_bitpos;
74 /* Calculate the subword index and bitposition for current bit */
75 sw_idx = itr->offset / itr->sw_width;
76 sw_bitpos = itr->offset % itr->sw_width;
77 /* Calculate the register index and bitposition for current bit */
78 itr->reg_idx = (sw_idx * itr->regs_per_sw) + (sw_bitpos / 32);
79 itr->reg_bitpos = sw_bitpos % 32;
82 void vcap_iter_init(struct vcap_stream_iter *itr, int sw_width,
83 const struct vcap_typegroup *tg, u32 offset)
85 vcap_iter_set(itr, sw_width, tg, offset);
86 vcap_iter_skip_tg(itr);
87 vcap_iter_update(itr);
90 void vcap_iter_next(struct vcap_stream_iter *itr)
93 vcap_iter_skip_tg(itr);
94 vcap_iter_update(itr);
97 static void vcap_set_bit(u32 *stream, struct vcap_stream_iter *itr, bool value)
99 u32 mask = BIT(itr->reg_bitpos);
100 u32 *p = &stream[itr->reg_idx];
108 static void vcap_encode_bit(u32 *stream, struct vcap_stream_iter *itr, bool val)
110 /* When intersected by a type group field, stream the type group bits
111 * before continuing with the value bit
113 while (itr->tg->width &&
114 itr->offset >= itr->tg->offset &&
115 itr->offset < itr->tg->offset + itr->tg->width) {
116 int tg_bitpos = itr->tg->offset - itr->offset;
118 vcap_set_bit(stream, itr, (itr->tg->value >> tg_bitpos) & 0x1);
120 vcap_iter_update(itr);
122 vcap_set_bit(stream, itr, val);
125 static void vcap_encode_field(u32 *stream, struct vcap_stream_iter *itr,
126 int width, const u8 *value)
130 /* Loop over the field value bits and add the value bits one by one to
133 for (idx = 0; idx < width; idx++) {
134 u8 bidx = idx & GENMASK(2, 0);
136 /* Encode one field value bit */
137 vcap_encode_bit(stream, itr, (value[idx / 8] >> bidx) & 0x1);
142 static void vcap_encode_typegroups(u32 *stream, int sw_width,
143 const struct vcap_typegroup *tg,
146 struct vcap_stream_iter iter;
149 /* Mask bits must be set to zeros (inverted later when writing to the
150 * mask cache register), so that the mask typegroup bits consist of
151 * match-1 or match-0, or both
153 vcap_iter_set(&iter, sw_width, tg, 0);
154 while (iter.tg->width) {
155 /* Set position to current typegroup bit */
156 iter.offset = iter.tg->offset;
157 vcap_iter_update(&iter);
158 for (idx = 0; idx < iter.tg->width; idx++) {
159 /* Iterate over current typegroup bits. Mask typegroup
160 * bits are always set
163 vcap_set_bit(stream, &iter, 0x1);
165 vcap_set_bit(stream, &iter,
166 (iter.tg->value >> idx) & 0x1);
168 vcap_iter_update(&iter);
170 iter.tg++; /* next typegroup */
174 static bool vcap_bitarray_zero(int width, u8 *value)
176 int bytes = DIV_ROUND_UP(width, BITS_PER_BYTE);
177 u8 total = 0, bmask = 0xff;
181 for (idx = 0; idx < bytes; ++idx, rwidth -= BITS_PER_BYTE) {
182 if (rwidth && rwidth < BITS_PER_BYTE)
183 bmask = (1 << rwidth) - 1;
184 total += value[idx] & bmask;
189 static bool vcap_get_bit(u32 *stream, struct vcap_stream_iter *itr)
191 u32 mask = BIT(itr->reg_bitpos);
192 u32 *p = &stream[itr->reg_idx];
194 return !!(*p & mask);
197 static void vcap_decode_field(u32 *stream, struct vcap_stream_iter *itr,
198 int width, u8 *value)
202 /* Loop over the field value bits and get the field bits and
203 * set them in the output value byte array
205 for (idx = 0; idx < width; idx++) {
208 /* Decode one field value bit */
209 if (vcap_get_bit(stream, itr))
217 /* Verify that the type id in the stream matches the type id of the keyset */
218 static bool vcap_verify_keystream_keyset(struct vcap_control *vctrl,
222 enum vcap_keyfield_set keyset)
224 const struct vcap_info *vcap = &vctrl->vcaps[vt];
225 const struct vcap_field *typefld;
226 const struct vcap_typegroup *tgt;
227 const struct vcap_field *fields;
228 struct vcap_stream_iter iter;
229 const struct vcap_set *info;
233 if (vcap_keyfield_count(vctrl, vt, keyset) == 0)
236 info = vcap_keyfieldset(vctrl, vt, keyset);
237 /* Check that the keyset is valid */
241 /* a type_id of value -1 means that there is no type field */
242 if (info->type_id == (u8)-1)
245 /* Get a valid typegroup for the specific keyset */
246 tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
250 fields = vcap_keyfields(vctrl, vt, keyset);
254 typefld = &fields[VCAP_KF_TYPE];
255 vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
256 vcap_decode_field(mskstream, &iter, typefld->width, (u8 *)&mask);
257 /* no type info if there are no mask bits */
258 if (vcap_bitarray_zero(typefld->width, (u8 *)&mask))
261 /* Get the value of the type field in the stream and compare to the
262 * one define in the vcap keyset
264 vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
265 vcap_decode_field(keystream, &iter, typefld->width, (u8 *)&value);
267 return (value & mask) == (info->type_id & mask);
270 /* Verify that the typegroup bits have the correct values */
271 static int vcap_verify_typegroups(u32 *stream, int sw_width,
272 const struct vcap_typegroup *tgt, bool mask,
275 struct vcap_stream_iter iter;
278 vcap_iter_set(&iter, sw_width, tgt, 0);
280 while (iter.tg->width) {
282 u32 tg_value = iter.tg->value;
285 tg_value = (1 << iter.tg->width) - 1;
286 /* Set position to current typegroup bit */
287 iter.offset = iter.tg->offset;
288 vcap_iter_update(&iter);
289 for (idx = 0; idx < iter.tg->width; idx++) {
290 /* Decode one typegroup bit */
291 if (vcap_get_bit(stream, &iter))
294 vcap_iter_update(&iter);
296 if (value != tg_value)
298 iter.tg++; /* next typegroup */
300 /* Stop checking more typegroups */
301 if (sw_max && sw_cnt >= sw_max)
307 /* Find the subword width of the key typegroup that matches the stream data */
308 static int vcap_find_keystream_typegroup_sw(struct vcap_control *vctrl,
309 enum vcap_type vt, u32 *stream,
310 bool mask, int sw_max)
312 const struct vcap_typegroup **tgt;
315 tgt = vctrl->vcaps[vt].keyfield_set_typegroups;
316 /* Try the longest subword match first */
317 for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
321 res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].sw_width,
322 tgt[sw_idx], mask, sw_max);
329 /* Verify that the typegroup information, subword count, keyset and type id
330 * are in sync and correct, return the list of matchin keysets
333 vcap_find_keystream_keysets(struct vcap_control *vctrl,
337 bool mask, int sw_max,
338 struct vcap_keyset_list *kslist)
340 const struct vcap_set *keyfield_set;
343 sw_count = vcap_find_keystream_typegroup_sw(vctrl, vt, keystream, mask,
348 keyfield_set = vctrl->vcaps[vt].keyfield_set;
349 for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
350 if (keyfield_set[idx].sw_per_item != sw_count)
353 if (vcap_verify_keystream_keyset(vctrl, vt, keystream,
355 vcap_keyset_list_add(kslist, idx);
361 EXPORT_SYMBOL_GPL(vcap_find_keystream_keysets);
363 /* Read key data from a VCAP address and discover if there are any rule keysets
366 int vcap_addr_keysets(struct vcap_control *vctrl,
367 struct net_device *ndev,
368 struct vcap_admin *admin,
370 struct vcap_keyset_list *kslist)
372 enum vcap_type vt = admin->vtype;
373 int keyset_sw_regs, idx;
374 u32 key = 0, mask = 0;
376 /* Read the cache at the specified address */
377 keyset_sw_regs = DIV_ROUND_UP(vctrl->vcaps[vt].sw_width, 32);
378 vctrl->ops->update(ndev, admin, VCAP_CMD_READ, VCAP_SEL_ALL, addr);
379 vctrl->ops->cache_read(ndev, admin, VCAP_SEL_ENTRY, 0,
381 /* Skip uninitialized key/mask entries */
382 for (idx = 0; idx < keyset_sw_regs; ++idx) {
383 key |= ~admin->cache.keystream[idx];
384 mask |= admin->cache.maskstream[idx];
386 if (key == 0 && mask == 0)
388 /* Decode and locate the keysets */
389 return vcap_find_keystream_keysets(vctrl, vt, admin->cache.keystream,
390 admin->cache.maskstream, false, 0,
393 EXPORT_SYMBOL_GPL(vcap_addr_keysets);
395 /* Return the list of keyfields for the keyset */
396 const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl,
398 enum vcap_keyfield_set keyset)
400 /* Check that the keyset exists in the vcap keyset list */
401 if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
403 return vctrl->vcaps[vt].keyfield_set_map[keyset];
406 /* Return the keyset information for the keyset */
407 const struct vcap_set *vcap_keyfieldset(struct vcap_control *vctrl,
409 enum vcap_keyfield_set keyset)
411 const struct vcap_set *kset;
413 /* Check that the keyset exists in the vcap keyset list */
414 if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
416 kset = &vctrl->vcaps[vt].keyfield_set[keyset];
417 if (kset->sw_per_item == 0 || kset->sw_per_item > vctrl->vcaps[vt].sw_count)
421 EXPORT_SYMBOL_GPL(vcap_keyfieldset);
423 /* Return the typegroup table for the matching keyset (using subword size) */
424 const struct vcap_typegroup *
425 vcap_keyfield_typegroup(struct vcap_control *vctrl,
426 enum vcap_type vt, enum vcap_keyfield_set keyset)
428 const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset);
430 /* Check that the keyset is valid */
433 return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item];
436 /* Return the number of keyfields in the keyset */
437 int vcap_keyfield_count(struct vcap_control *vctrl,
438 enum vcap_type vt, enum vcap_keyfield_set keyset)
440 /* Check that the keyset exists in the vcap keyset list */
441 if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
443 return vctrl->vcaps[vt].keyfield_set_map_size[keyset];
446 static void vcap_encode_keyfield(struct vcap_rule_internal *ri,
447 const struct vcap_client_keyfield *kf,
448 const struct vcap_field *rf,
449 const struct vcap_typegroup *tgt)
451 int sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
452 struct vcap_cache_data *cache = &ri->admin->cache;
453 struct vcap_stream_iter iter;
454 const u8 *value, *mask;
456 /* Encode the fields for the key and the mask in their respective
457 * streams, respecting the subword width.
459 switch (kf->ctrl.type) {
461 value = &kf->data.u1.value;
462 mask = &kf->data.u1.mask;
465 value = (const u8 *)&kf->data.u32.value;
466 mask = (const u8 *)&kf->data.u32.mask;
469 value = kf->data.u48.value;
470 mask = kf->data.u48.mask;
473 value = kf->data.u56.value;
474 mask = kf->data.u56.mask;
477 value = kf->data.u64.value;
478 mask = kf->data.u64.mask;
481 value = kf->data.u72.value;
482 mask = kf->data.u72.mask;
484 case VCAP_FIELD_U112:
485 value = kf->data.u112.value;
486 mask = kf->data.u112.mask;
488 case VCAP_FIELD_U128:
489 value = kf->data.u128.value;
490 mask = kf->data.u128.mask;
493 vcap_iter_init(&iter, sw_width, tgt, rf->offset);
494 vcap_encode_field(cache->keystream, &iter, rf->width, value);
495 vcap_iter_init(&iter, sw_width, tgt, rf->offset);
496 vcap_encode_field(cache->maskstream, &iter, rf->width, mask);
499 static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl,
500 struct vcap_rule_internal *ri,
501 const struct vcap_typegroup *tgt)
503 int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width;
504 struct vcap_cache_data *cache = &ri->admin->cache;
506 /* Encode the typegroup bits for the key and the mask in their streams,
507 * respecting the subword width.
509 vcap_encode_typegroups(cache->keystream, sw_width, tgt, false);
510 vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true);
513 /* Copy data from src to dst but reverse the data in chunks of 32bits.
514 * For example if src is 00:11:22:33:44:55 where 55 is LSB the dst will
515 * have the value 22:33:44:55:00:11.
517 static void vcap_copy_to_w32be(u8 *dst, const u8 *src, int size)
519 for (int idx = 0; idx < size; ++idx) {
520 int first_byte_index = 0;
523 first_byte_index = size - (((idx >> 2) + 1) << 2);
524 if (first_byte_index < 0)
525 first_byte_index = 0;
526 nidx = idx + first_byte_index - (idx & ~0x3);
527 dst[nidx] = src[idx];
532 vcap_copy_from_client_keyfield(struct vcap_rule *rule,
533 struct vcap_client_keyfield *dst,
534 const struct vcap_client_keyfield *src)
536 struct vcap_rule_internal *ri = to_intrule(rule);
537 const struct vcap_client_keyfield_data *sdata;
538 struct vcap_client_keyfield_data *ddata;
541 dst->ctrl.type = src->ctrl.type;
542 dst->ctrl.key = src->ctrl.key;
543 INIT_LIST_HEAD(&dst->ctrl.list);
547 if (!ri->admin->w32be) {
548 memcpy(ddata, sdata, sizeof(dst->data));
552 size = keyfield_size_table[dst->ctrl.type] / 2;
554 switch (dst->ctrl.type) {
557 memcpy(ddata, sdata, sizeof(dst->data));
560 vcap_copy_to_w32be(ddata->u48.value, src->data.u48.value, size);
561 vcap_copy_to_w32be(ddata->u48.mask, src->data.u48.mask, size);
564 vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size);
565 vcap_copy_to_w32be(ddata->u56.mask, sdata->u56.mask, size);
568 vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size);
569 vcap_copy_to_w32be(ddata->u64.mask, sdata->u64.mask, size);
572 vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size);
573 vcap_copy_to_w32be(ddata->u72.mask, sdata->u72.mask, size);
575 case VCAP_FIELD_U112:
576 vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size);
577 vcap_copy_to_w32be(ddata->u112.mask, sdata->u112.mask, size);
579 case VCAP_FIELD_U128:
580 vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size);
581 vcap_copy_to_w32be(ddata->u128.mask, sdata->u128.mask, size);
587 vcap_copy_from_client_actionfield(struct vcap_rule *rule,
588 struct vcap_client_actionfield *dst,
589 const struct vcap_client_actionfield *src)
591 struct vcap_rule_internal *ri = to_intrule(rule);
592 const struct vcap_client_actionfield_data *sdata;
593 struct vcap_client_actionfield_data *ddata;
596 dst->ctrl.type = src->ctrl.type;
597 dst->ctrl.action = src->ctrl.action;
598 INIT_LIST_HEAD(&dst->ctrl.list);
602 if (!ri->admin->w32be) {
603 memcpy(ddata, sdata, sizeof(dst->data));
607 size = actionfield_size_table[dst->ctrl.type];
609 switch (dst->ctrl.type) {
612 memcpy(ddata, sdata, sizeof(dst->data));
615 vcap_copy_to_w32be(ddata->u48.value, sdata->u48.value, size);
618 vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size);
621 vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size);
624 vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size);
626 case VCAP_FIELD_U112:
627 vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size);
629 case VCAP_FIELD_U128:
630 vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size);
635 static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri)
637 const struct vcap_client_keyfield *ckf;
638 const struct vcap_typegroup *tg_table;
639 struct vcap_client_keyfield tempkf;
640 const struct vcap_field *kf_table;
643 /* Get a valid set of fields for the specific keyset */
644 kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset);
646 pr_err("%s:%d: no fields available for this keyset: %d\n",
647 __func__, __LINE__, ri->data.keyset);
650 /* Get a valid typegroup for the specific keyset */
651 tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype,
654 pr_err("%s:%d: no typegroups available for this keyset: %d\n",
655 __func__, __LINE__, ri->data.keyset);
658 /* Get a valid size for the specific keyset */
659 keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype,
661 if (keyset_size == 0) {
662 pr_err("%s:%d: zero field count for this keyset: %d\n",
663 __func__, __LINE__, ri->data.keyset);
666 /* Iterate over the keyfields (key, mask) in the rule
667 * and encode these bits
669 if (list_empty(&ri->data.keyfields)) {
670 pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__);
673 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
674 /* Check that the client entry exists in the keyset */
675 if (ckf->ctrl.key >= keyset_size) {
676 pr_err("%s:%d: key %d is not in vcap\n",
677 __func__, __LINE__, ckf->ctrl.key);
680 vcap_copy_from_client_keyfield(&ri->data, &tempkf, ckf);
681 vcap_encode_keyfield(ri, &tempkf, &kf_table[ckf->ctrl.key],
684 /* Add typegroup bits to the key/mask bitstreams */
685 vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table);
689 /* Return the list of actionfields for the actionset */
690 const struct vcap_field *
691 vcap_actionfields(struct vcap_control *vctrl,
692 enum vcap_type vt, enum vcap_actionfield_set actionset)
694 /* Check that the actionset exists in the vcap actionset list */
695 if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
697 return vctrl->vcaps[vt].actionfield_set_map[actionset];
700 const struct vcap_set *
701 vcap_actionfieldset(struct vcap_control *vctrl,
702 enum vcap_type vt, enum vcap_actionfield_set actionset)
704 const struct vcap_set *aset;
706 /* Check that the actionset exists in the vcap actionset list */
707 if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
709 aset = &vctrl->vcaps[vt].actionfield_set[actionset];
710 if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count)
715 /* Return the typegroup table for the matching actionset (using subword size) */
716 const struct vcap_typegroup *
717 vcap_actionfield_typegroup(struct vcap_control *vctrl,
718 enum vcap_type vt, enum vcap_actionfield_set actionset)
720 const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset);
722 /* Check that the actionset is valid */
725 return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item];
728 /* Return the number of actionfields in the actionset */
729 int vcap_actionfield_count(struct vcap_control *vctrl,
731 enum vcap_actionfield_set actionset)
733 /* Check that the actionset exists in the vcap actionset list */
734 if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
736 return vctrl->vcaps[vt].actionfield_set_map_size[actionset];
739 static void vcap_encode_actionfield(struct vcap_rule_internal *ri,
740 const struct vcap_client_actionfield *af,
741 const struct vcap_field *rf,
742 const struct vcap_typegroup *tgt)
744 int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
746 struct vcap_cache_data *cache = &ri->admin->cache;
747 struct vcap_stream_iter iter;
750 /* Encode the action field in the stream, respecting the subword width */
751 switch (af->ctrl.type) {
753 value = &af->data.u1.value;
756 value = (const u8 *)&af->data.u32.value;
759 value = af->data.u48.value;
762 value = af->data.u56.value;
765 value = af->data.u64.value;
768 value = af->data.u72.value;
770 case VCAP_FIELD_U112:
771 value = af->data.u112.value;
773 case VCAP_FIELD_U128:
774 value = af->data.u128.value;
777 vcap_iter_init(&iter, act_width, tgt, rf->offset);
778 vcap_encode_field(cache->actionstream, &iter, rf->width, value);
781 static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri,
782 const struct vcap_typegroup *tgt)
784 int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
785 struct vcap_cache_data *cache = &ri->admin->cache;
787 /* Encode the typegroup bits for the actionstream respecting the subword
790 vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false);
793 static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri)
795 const struct vcap_client_actionfield *caf;
796 const struct vcap_typegroup *tg_table;
797 struct vcap_client_actionfield tempaf;
798 const struct vcap_field *af_table;
801 /* Get a valid set of actionset fields for the specific actionset */
802 af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype,
805 pr_err("%s:%d: no fields available for this actionset: %d\n",
806 __func__, __LINE__, ri->data.actionset);
809 /* Get a valid typegroup for the specific actionset */
810 tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype,
813 pr_err("%s:%d: no typegroups available for this actionset: %d\n",
814 __func__, __LINE__, ri->data.actionset);
817 /* Get a valid actionset size for the specific actionset */
818 actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype,
820 if (actionset_size == 0) {
821 pr_err("%s:%d: zero field count for this actionset: %d\n",
822 __func__, __LINE__, ri->data.actionset);
825 /* Iterate over the actionfields in the rule
826 * and encode these bits
828 if (list_empty(&ri->data.actionfields))
829 pr_warn("%s:%d: no actionfields in the rule\n",
831 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
832 /* Check that the client action exists in the actionset */
833 if (caf->ctrl.action >= actionset_size) {
834 pr_err("%s:%d: action %d is not in vcap\n",
835 __func__, __LINE__, caf->ctrl.action);
838 vcap_copy_from_client_actionfield(&ri->data, &tempaf, caf);
839 vcap_encode_actionfield(ri, &tempaf,
840 &af_table[caf->ctrl.action], tg_table);
842 /* Add typegroup bits to the entry bitstreams */
843 vcap_encode_actionfield_typegroups(ri, tg_table);
847 static int vcap_encode_rule(struct vcap_rule_internal *ri)
851 err = vcap_encode_rule_keyset(ri);
854 err = vcap_encode_rule_actionset(ri);
860 int vcap_api_check(struct vcap_control *ctrl)
863 pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__);
866 if (!ctrl->ops || !ctrl->ops->validate_keyset ||
867 !ctrl->ops->add_default_fields || !ctrl->ops->cache_erase ||
868 !ctrl->ops->cache_write || !ctrl->ops->cache_read ||
869 !ctrl->ops->init || !ctrl->ops->update || !ctrl->ops->move ||
870 !ctrl->ops->port_info) {
871 pr_err("%s:%d: client operations are missing\n",
878 void vcap_erase_cache(struct vcap_rule_internal *ri)
880 ri->vctrl->ops->cache_erase(ri->admin);
883 /* Update the keyset for the rule */
884 int vcap_set_rule_set_keyset(struct vcap_rule *rule,
885 enum vcap_keyfield_set keyset)
887 struct vcap_rule_internal *ri = to_intrule(rule);
888 const struct vcap_set *kset;
891 kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset);
892 /* Check that the keyset is valid */
895 ri->keyset_sw = kset->sw_per_item;
896 sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
897 ri->keyset_sw_regs = DIV_ROUND_UP(sw_width, 32);
898 ri->data.keyset = keyset;
901 EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset);
903 /* Update the actionset for the rule */
904 int vcap_set_rule_set_actionset(struct vcap_rule *rule,
905 enum vcap_actionfield_set actionset)
907 struct vcap_rule_internal *ri = to_intrule(rule);
908 const struct vcap_set *aset;
911 aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset);
912 /* Check that the actionset is valid */
915 ri->actionset_sw = aset->sw_per_item;
916 act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
917 ri->actionset_sw_regs = DIV_ROUND_UP(act_width, 32);
918 ri->data.actionset = actionset;
921 EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset);
923 /* Check if a rule with this id exists */
924 static bool vcap_rule_exists(struct vcap_control *vctrl, u32 id)
926 struct vcap_rule_internal *ri;
927 struct vcap_admin *admin;
929 /* Look for the rule id in all vcaps */
930 list_for_each_entry(admin, &vctrl->list, list)
931 list_for_each_entry(ri, &admin->rules, list)
932 if (ri->data.id == id)
937 /* Find a rule with a provided rule id return a locked vcap */
938 static struct vcap_rule_internal *
939 vcap_get_locked_rule(struct vcap_control *vctrl, u32 id)
941 struct vcap_rule_internal *ri;
942 struct vcap_admin *admin;
944 /* Look for the rule id in all vcaps */
945 list_for_each_entry(admin, &vctrl->list, list) {
946 mutex_lock(&admin->lock);
947 list_for_each_entry(ri, &admin->rules, list)
948 if (ri->data.id == id)
950 mutex_unlock(&admin->lock);
955 /* Find a rule id with a provided cookie */
956 int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
958 struct vcap_rule_internal *ri;
959 struct vcap_admin *admin;
962 /* Look for the rule id in all vcaps */
963 list_for_each_entry(admin, &vctrl->list, list) {
964 mutex_lock(&admin->lock);
965 list_for_each_entry(ri, &admin->rules, list) {
966 if (ri->data.cookie == cookie) {
971 mutex_unlock(&admin->lock);
977 EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
979 /* Get number of rules in a vcap instance lookup chain id range */
980 int vcap_admin_rule_count(struct vcap_admin *admin, int cid)
982 int max_cid = roundup(cid + 1, VCAP_CID_LOOKUP_SIZE);
983 int min_cid = rounddown(cid, VCAP_CID_LOOKUP_SIZE);
984 struct vcap_rule_internal *elem;
987 list_for_each_entry(elem, &admin->rules, list) {
988 mutex_lock(&admin->lock);
989 if (elem->data.vcap_chain_id >= min_cid &&
990 elem->data.vcap_chain_id < max_cid)
992 mutex_unlock(&admin->lock);
996 EXPORT_SYMBOL_GPL(vcap_admin_rule_count);
998 /* Make a copy of the rule, shallow or full */
999 static struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri,
1002 struct vcap_client_actionfield *caf, *newcaf;
1003 struct vcap_client_keyfield *ckf, *newckf;
1004 struct vcap_rule_internal *duprule;
1006 /* Allocate the client part */
1007 duprule = kzalloc(sizeof(*duprule), GFP_KERNEL);
1009 return ERR_PTR(-ENOMEM);
1011 /* Not inserted in the VCAP */
1012 INIT_LIST_HEAD(&duprule->list);
1013 /* No elements in these lists */
1014 INIT_LIST_HEAD(&duprule->data.keyfields);
1015 INIT_LIST_HEAD(&duprule->data.actionfields);
1017 /* A full rule copy includes keys and actions */
1021 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
1022 newckf = kmemdup(ckf, sizeof(*newckf), GFP_KERNEL);
1025 list_add_tail(&newckf->ctrl.list, &duprule->data.keyfields);
1028 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
1029 newcaf = kmemdup(caf, sizeof(*newcaf), GFP_KERNEL);
1032 list_add_tail(&newcaf->ctrl.list, &duprule->data.actionfields);
1038 list_for_each_entry_safe(ckf, newckf, &duprule->data.keyfields, ctrl.list) {
1039 list_del(&ckf->ctrl.list);
1043 list_for_each_entry_safe(caf, newcaf, &duprule->data.actionfields, ctrl.list) {
1044 list_del(&caf->ctrl.list);
1049 return ERR_PTR(-ENOMEM);
1052 static void vcap_apply_width(u8 *dst, int width, int bytes)
1057 for (idx = 0; idx < bytes; idx++) {
1060 bmask = (1 << width) - 1;
1070 static void vcap_copy_from_w32be(u8 *dst, u8 *src, int size, int width)
1072 int idx, ridx, wstart, nidx;
1073 int tail_bytes = (((size + 4) >> 2) << 2) - size;
1075 for (idx = 0, ridx = size - 1; idx < size; ++idx, --ridx) {
1076 wstart = (idx >> 2) << 2;
1077 nidx = wstart + 3 - (idx & 0x3);
1080 dst[nidx] = src[ridx];
1083 vcap_apply_width(dst, width, size);
1086 static void vcap_copy_action_bit_field(struct vcap_u1_action *field, u8 *value)
1088 field->value = (*value) & 0x1;
1091 static void vcap_copy_limited_actionfield(u8 *dstvalue, u8 *srcvalue,
1092 int width, int bytes)
1094 memcpy(dstvalue, srcvalue, bytes);
1095 vcap_apply_width(dstvalue, width, bytes);
1098 static void vcap_copy_to_client_actionfield(struct vcap_rule_internal *ri,
1099 struct vcap_client_actionfield *field,
1100 u8 *value, u16 width)
1102 int field_size = actionfield_size_table[field->ctrl.type];
1104 if (ri->admin->w32be) {
1105 switch (field->ctrl.type) {
1106 case VCAP_FIELD_BIT:
1107 vcap_copy_action_bit_field(&field->data.u1, value);
1109 case VCAP_FIELD_U32:
1110 vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
1114 case VCAP_FIELD_U48:
1115 vcap_copy_from_w32be(field->data.u48.value, value,
1118 case VCAP_FIELD_U56:
1119 vcap_copy_from_w32be(field->data.u56.value, value,
1122 case VCAP_FIELD_U64:
1123 vcap_copy_from_w32be(field->data.u64.value, value,
1126 case VCAP_FIELD_U72:
1127 vcap_copy_from_w32be(field->data.u72.value, value,
1130 case VCAP_FIELD_U112:
1131 vcap_copy_from_w32be(field->data.u112.value, value,
1134 case VCAP_FIELD_U128:
1135 vcap_copy_from_w32be(field->data.u128.value, value,
1140 switch (field->ctrl.type) {
1141 case VCAP_FIELD_BIT:
1142 vcap_copy_action_bit_field(&field->data.u1, value);
1144 case VCAP_FIELD_U32:
1145 vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
1149 case VCAP_FIELD_U48:
1150 vcap_copy_limited_actionfield(field->data.u48.value,
1154 case VCAP_FIELD_U56:
1155 vcap_copy_limited_actionfield(field->data.u56.value,
1159 case VCAP_FIELD_U64:
1160 vcap_copy_limited_actionfield(field->data.u64.value,
1164 case VCAP_FIELD_U72:
1165 vcap_copy_limited_actionfield(field->data.u72.value,
1169 case VCAP_FIELD_U112:
1170 vcap_copy_limited_actionfield(field->data.u112.value,
1174 case VCAP_FIELD_U128:
1175 vcap_copy_limited_actionfield(field->data.u128.value,
1183 static void vcap_copy_key_bit_field(struct vcap_u1_key *field,
1184 u8 *value, u8 *mask)
1186 field->value = (*value) & 0x1;
1187 field->mask = (*mask) & 0x1;
1190 static void vcap_copy_limited_keyfield(u8 *dstvalue, u8 *dstmask,
1191 u8 *srcvalue, u8 *srcmask,
1192 int width, int bytes)
1194 memcpy(dstvalue, srcvalue, bytes);
1195 vcap_apply_width(dstvalue, width, bytes);
1196 memcpy(dstmask, srcmask, bytes);
1197 vcap_apply_width(dstmask, width, bytes);
1200 static void vcap_copy_to_client_keyfield(struct vcap_rule_internal *ri,
1201 struct vcap_client_keyfield *field,
1202 u8 *value, u8 *mask, u16 width)
1204 int field_size = keyfield_size_table[field->ctrl.type] / 2;
1206 if (ri->admin->w32be) {
1207 switch (field->ctrl.type) {
1208 case VCAP_FIELD_BIT:
1209 vcap_copy_key_bit_field(&field->data.u1, value, mask);
1211 case VCAP_FIELD_U32:
1212 vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1213 (u8 *)&field->data.u32.mask,
1217 case VCAP_FIELD_U48:
1218 vcap_copy_from_w32be(field->data.u48.value, value,
1220 vcap_copy_from_w32be(field->data.u48.mask, mask,
1223 case VCAP_FIELD_U56:
1224 vcap_copy_from_w32be(field->data.u56.value, value,
1226 vcap_copy_from_w32be(field->data.u56.mask, mask,
1229 case VCAP_FIELD_U64:
1230 vcap_copy_from_w32be(field->data.u64.value, value,
1232 vcap_copy_from_w32be(field->data.u64.mask, mask,
1235 case VCAP_FIELD_U72:
1236 vcap_copy_from_w32be(field->data.u72.value, value,
1238 vcap_copy_from_w32be(field->data.u72.mask, mask,
1241 case VCAP_FIELD_U112:
1242 vcap_copy_from_w32be(field->data.u112.value, value,
1244 vcap_copy_from_w32be(field->data.u112.mask, mask,
1247 case VCAP_FIELD_U128:
1248 vcap_copy_from_w32be(field->data.u128.value, value,
1250 vcap_copy_from_w32be(field->data.u128.mask, mask,
1255 switch (field->ctrl.type) {
1256 case VCAP_FIELD_BIT:
1257 vcap_copy_key_bit_field(&field->data.u1, value, mask);
1259 case VCAP_FIELD_U32:
1260 vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1261 (u8 *)&field->data.u32.mask,
1265 case VCAP_FIELD_U48:
1266 vcap_copy_limited_keyfield(field->data.u48.value,
1267 field->data.u48.mask,
1271 case VCAP_FIELD_U56:
1272 vcap_copy_limited_keyfield(field->data.u56.value,
1273 field->data.u56.mask,
1277 case VCAP_FIELD_U64:
1278 vcap_copy_limited_keyfield(field->data.u64.value,
1279 field->data.u64.mask,
1283 case VCAP_FIELD_U72:
1284 vcap_copy_limited_keyfield(field->data.u72.value,
1285 field->data.u72.mask,
1289 case VCAP_FIELD_U112:
1290 vcap_copy_limited_keyfield(field->data.u112.value,
1291 field->data.u112.mask,
1295 case VCAP_FIELD_U128:
1296 vcap_copy_limited_keyfield(field->data.u128.value,
1297 field->data.u128.mask,
1305 static void vcap_rule_alloc_keyfield(struct vcap_rule_internal *ri,
1306 const struct vcap_field *keyfield,
1307 enum vcap_key_field key,
1308 u8 *value, u8 *mask)
1310 struct vcap_client_keyfield *field;
1312 field = kzalloc(sizeof(*field), GFP_KERNEL);
1315 INIT_LIST_HEAD(&field->ctrl.list);
1316 field->ctrl.key = key;
1317 field->ctrl.type = keyfield->type;
1318 vcap_copy_to_client_keyfield(ri, field, value, mask, keyfield->width);
1319 list_add_tail(&field->ctrl.list, &ri->data.keyfields);
1322 /* Read key data from a VCAP address and discover if there is a rule keyset
1326 vcap_verify_actionstream_actionset(struct vcap_control *vctrl,
1329 enum vcap_actionfield_set actionset)
1331 const struct vcap_typegroup *tgt;
1332 const struct vcap_field *fields;
1333 const struct vcap_set *info;
1335 if (vcap_actionfield_count(vctrl, vt, actionset) == 0)
1338 info = vcap_actionfieldset(vctrl, vt, actionset);
1339 /* Check that the actionset is valid */
1343 /* a type_id of value -1 means that there is no type field */
1344 if (info->type_id == (u8)-1)
1347 /* Get a valid typegroup for the specific actionset */
1348 tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1352 fields = vcap_actionfields(vctrl, vt, actionset);
1356 /* Later this will be expanded with a check of the type id */
1360 /* Find the subword width of the action typegroup that matches the stream data
1362 static int vcap_find_actionstream_typegroup_sw(struct vcap_control *vctrl,
1363 enum vcap_type vt, u32 *stream,
1366 const struct vcap_typegroup **tgt;
1369 tgt = vctrl->vcaps[vt].actionfield_set_typegroups;
1370 /* Try the longest subword match first */
1371 for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
1374 res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].act_width,
1375 tgt[sw_idx], false, sw_max);
1382 /* Verify that the typegroup information, subword count, actionset and type id
1383 * are in sync and correct, return the actionset
1385 static enum vcap_actionfield_set
1386 vcap_find_actionstream_actionset(struct vcap_control *vctrl,
1391 const struct vcap_set *actionfield_set;
1395 sw_count = vcap_find_actionstream_typegroup_sw(vctrl, vt, stream,
1400 actionfield_set = vctrl->vcaps[vt].actionfield_set;
1401 for (idx = 0; idx < vctrl->vcaps[vt].actionfield_set_size; ++idx) {
1402 if (actionfield_set[idx].sw_per_item != sw_count)
1405 res = vcap_verify_actionstream_actionset(vctrl, vt,
1413 /* Store action value in an element in a list for the client */
1414 static void vcap_rule_alloc_actionfield(struct vcap_rule_internal *ri,
1415 const struct vcap_field *actionfield,
1416 enum vcap_action_field action,
1419 struct vcap_client_actionfield *field;
1421 field = kzalloc(sizeof(*field), GFP_KERNEL);
1424 INIT_LIST_HEAD(&field->ctrl.list);
1425 field->ctrl.action = action;
1426 field->ctrl.type = actionfield->type;
1427 vcap_copy_to_client_actionfield(ri, field, value, actionfield->width);
1428 list_add_tail(&field->ctrl.list, &ri->data.actionfields);
1431 static int vcap_decode_actionset(struct vcap_rule_internal *ri)
1433 struct vcap_control *vctrl = ri->vctrl;
1434 struct vcap_admin *admin = ri->admin;
1435 const struct vcap_field *actionfield;
1436 enum vcap_actionfield_set actionset;
1437 enum vcap_type vt = admin->vtype;
1438 const struct vcap_typegroup *tgt;
1439 struct vcap_stream_iter iter;
1440 int idx, res, actfield_count;
1444 actstream = admin->cache.actionstream;
1445 res = vcap_find_actionstream_actionset(vctrl, vt, actstream, 0);
1447 pr_err("%s:%d: could not find valid actionset: %d\n",
1448 __func__, __LINE__, res);
1452 actfield_count = vcap_actionfield_count(vctrl, vt, actionset);
1453 actionfield = vcap_actionfields(vctrl, vt, actionset);
1454 tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1455 /* Start decoding the stream */
1456 for (idx = 0; idx < actfield_count; ++idx) {
1457 if (actionfield[idx].width <= 0)
1459 /* Get the action */
1460 memset(value, 0, DIV_ROUND_UP(actionfield[idx].width, 8));
1461 vcap_iter_init(&iter, vctrl->vcaps[vt].act_width, tgt,
1462 actionfield[idx].offset);
1463 vcap_decode_field(actstream, &iter, actionfield[idx].width,
1465 /* Skip if no bits are set */
1466 if (vcap_bitarray_zero(actionfield[idx].width, value))
1468 vcap_rule_alloc_actionfield(ri, &actionfield[idx], idx, value);
1469 /* Later the action id will also be checked */
1471 return vcap_set_rule_set_actionset((struct vcap_rule *)ri, actionset);
1474 static int vcap_decode_keyset(struct vcap_rule_internal *ri)
1476 struct vcap_control *vctrl = ri->vctrl;
1477 struct vcap_stream_iter kiter, miter;
1478 struct vcap_admin *admin = ri->admin;
1479 enum vcap_keyfield_set keysets[10];
1480 const struct vcap_field *keyfield;
1481 enum vcap_type vt = admin->vtype;
1482 const struct vcap_typegroup *tgt;
1483 struct vcap_keyset_list matches;
1484 enum vcap_keyfield_set keyset;
1485 int idx, res, keyfield_count;
1491 keystream = admin->cache.keystream;
1492 maskstream = admin->cache.maskstream;
1493 matches.keysets = keysets;
1495 matches.max = ARRAY_SIZE(keysets);
1496 res = vcap_find_keystream_keysets(vctrl, vt, keystream, maskstream,
1497 false, 0, &matches);
1499 pr_err("%s:%d: could not find valid keysets: %d\n",
1500 __func__, __LINE__, res);
1503 keyset = matches.keysets[0];
1504 keyfield_count = vcap_keyfield_count(vctrl, vt, keyset);
1505 keyfield = vcap_keyfields(vctrl, vt, keyset);
1506 tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
1507 /* Start decoding the streams */
1508 for (idx = 0; idx < keyfield_count; ++idx) {
1509 if (keyfield[idx].width <= 0)
1511 /* First get the mask */
1512 memset(mask, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1513 vcap_iter_init(&miter, vctrl->vcaps[vt].sw_width, tgt,
1514 keyfield[idx].offset);
1515 vcap_decode_field(maskstream, &miter, keyfield[idx].width,
1517 /* Skip if no mask bits are set */
1518 if (vcap_bitarray_zero(keyfield[idx].width, mask))
1521 memset(value, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1522 vcap_iter_init(&kiter, vctrl->vcaps[vt].sw_width, tgt,
1523 keyfield[idx].offset);
1524 vcap_decode_field(keystream, &kiter, keyfield[idx].width,
1526 vcap_rule_alloc_keyfield(ri, &keyfield[idx], idx, value, mask);
1528 return vcap_set_rule_set_keyset((struct vcap_rule *)ri, keyset);
1531 /* Read VCAP content into the VCAP cache */
1532 static int vcap_read_rule(struct vcap_rule_internal *ri)
1534 struct vcap_admin *admin = ri->admin;
1535 int sw_idx, ent_idx = 0, act_idx = 0;
1536 u32 addr = ri->addr;
1538 if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1539 pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1542 vcap_erase_cache(ri);
1543 /* Use the values in the streams to read the VCAP cache */
1544 for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1545 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ,
1546 VCAP_SEL_ALL, addr);
1547 ri->vctrl->ops->cache_read(ri->ndev, admin,
1548 VCAP_SEL_ENTRY, ent_idx,
1549 ri->keyset_sw_regs);
1550 ri->vctrl->ops->cache_read(ri->ndev, admin,
1551 VCAP_SEL_ACTION, act_idx,
1552 ri->actionset_sw_regs);
1554 ri->vctrl->ops->cache_read(ri->ndev, admin,
1557 ent_idx += ri->keyset_sw_regs;
1558 act_idx += ri->actionset_sw_regs;
1563 /* Write VCAP cache content to the VCAP HW instance */
1564 static int vcap_write_rule(struct vcap_rule_internal *ri)
1566 struct vcap_admin *admin = ri->admin;
1567 int sw_idx, ent_idx = 0, act_idx = 0;
1568 u32 addr = ri->addr;
1570 if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1571 pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1574 /* Use the values in the streams to write the VCAP cache */
1575 for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1576 ri->vctrl->ops->cache_write(ri->ndev, admin,
1577 VCAP_SEL_ENTRY, ent_idx,
1578 ri->keyset_sw_regs);
1579 ri->vctrl->ops->cache_write(ri->ndev, admin,
1580 VCAP_SEL_ACTION, act_idx,
1581 ri->actionset_sw_regs);
1582 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1583 VCAP_SEL_ALL, addr);
1584 ent_idx += ri->keyset_sw_regs;
1585 act_idx += ri->actionset_sw_regs;
1590 static int vcap_write_counter(struct vcap_rule_internal *ri,
1591 struct vcap_counter *ctr)
1593 struct vcap_admin *admin = ri->admin;
1595 admin->cache.counter = ctr->value;
1596 admin->cache.sticky = ctr->sticky;
1597 ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER,
1599 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1600 VCAP_SEL_COUNTER, ri->addr);
1604 /* Convert a chain id to a VCAP lookup index */
1605 int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid)
1607 int lookup_first = admin->vinst * admin->lookups_per_instance;
1608 int lookup_last = lookup_first + admin->lookups_per_instance;
1609 int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE;
1610 int cid = admin->first_cid;
1613 for (lookup = lookup_first; lookup < lookup_last; ++lookup,
1614 cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE)
1615 if (cur_cid >= cid && cur_cid < cid_next)
1619 EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup);
1621 /* Lookup a vcap instance using chain id */
1622 struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
1624 struct vcap_admin *admin;
1626 if (vcap_api_check(vctrl))
1629 list_for_each_entry(admin, &vctrl->list, list) {
1630 if (cid >= admin->first_cid && cid <= admin->last_cid)
1635 EXPORT_SYMBOL_GPL(vcap_find_admin);
1637 /* Is this the last admin instance ordered by chain id and direction */
1638 static bool vcap_admin_is_last(struct vcap_control *vctrl,
1639 struct vcap_admin *admin,
1642 struct vcap_admin *iter, *last = NULL;
1645 list_for_each_entry(iter, &vctrl->list, list) {
1646 if (iter->first_cid > max_cid &&
1647 iter->ingress == ingress) {
1649 max_cid = iter->first_cid;
1655 return admin == last;
1658 /* Calculate the value used for chaining VCAP rules */
1659 int vcap_chain_offset(struct vcap_control *vctrl, int from_cid, int to_cid)
1661 int diff = to_cid - from_cid;
1663 if (diff < 0) /* Wrong direction */
1665 to_cid %= VCAP_CID_LOOKUP_SIZE;
1666 if (to_cid == 0) /* Destination aligned to a lookup == no chaining */
1668 diff %= VCAP_CID_LOOKUP_SIZE; /* Limit to a value within a lookup */
1671 EXPORT_SYMBOL_GPL(vcap_chain_offset);
1673 /* Is the next chain id in one of the following lookups
1674 * For now this does not support filters linked to other filters using
1675 * keys and actions. That will be added later.
1677 bool vcap_is_next_lookup(struct vcap_control *vctrl, int src_cid, int dst_cid)
1679 struct vcap_admin *admin;
1682 if (vcap_api_check(vctrl))
1685 /* The offset must be at least one lookup so round up one chain */
1686 next_cid = roundup(src_cid + 1, VCAP_CID_LOOKUP_SIZE);
1688 if (dst_cid < next_cid)
1691 admin = vcap_find_admin(vctrl, dst_cid);
1697 EXPORT_SYMBOL_GPL(vcap_is_next_lookup);
1699 /* Check if there is room for a new rule */
1700 static int vcap_rule_space(struct vcap_admin *admin, int size)
1702 if (admin->last_used_addr - size < admin->first_valid_addr) {
1703 pr_err("%s:%d: No room for rule size: %u, %u\n",
1704 __func__, __LINE__, size, admin->first_valid_addr);
1710 /* Add the keyset typefield to the list of rule keyfields */
1711 static int vcap_add_type_keyfield(struct vcap_rule *rule)
1713 struct vcap_rule_internal *ri = to_intrule(rule);
1714 enum vcap_keyfield_set keyset = rule->keyset;
1715 enum vcap_type vt = ri->admin->vtype;
1716 const struct vcap_field *fields;
1717 const struct vcap_set *kset;
1720 kset = vcap_keyfieldset(ri->vctrl, vt, keyset);
1723 if (kset->type_id == (u8)-1) /* No type field is needed */
1726 fields = vcap_keyfields(ri->vctrl, vt, keyset);
1729 if (fields[VCAP_KF_TYPE].width > 1) {
1730 ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE,
1731 kset->type_id, 0xff);
1734 ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1737 ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1743 /* Add the actionset typefield to the list of rule actionfields */
1744 static int vcap_add_type_actionfield(struct vcap_rule *rule)
1746 enum vcap_actionfield_set actionset = rule->actionset;
1747 struct vcap_rule_internal *ri = to_intrule(rule);
1748 enum vcap_type vt = ri->admin->vtype;
1749 const struct vcap_field *fields;
1750 const struct vcap_set *aset;
1753 aset = vcap_actionfieldset(ri->vctrl, vt, actionset);
1756 if (aset->type_id == (u8)-1) /* No type field is needed */
1759 fields = vcap_actionfields(ri->vctrl, vt, actionset);
1762 if (fields[VCAP_AF_TYPE].width > 1) {
1763 ret = vcap_rule_add_action_u32(rule, VCAP_AF_TYPE,
1767 ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE,
1770 ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE,
1776 /* Add a keyset to a keyset list */
1777 bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist,
1778 enum vcap_keyfield_set keyset)
1782 if (keysetlist->cnt < keysetlist->max) {
1783 /* Avoid duplicates */
1784 for (idx = 0; idx < keysetlist->cnt; ++idx)
1785 if (keysetlist->keysets[idx] == keyset)
1786 return keysetlist->cnt < keysetlist->max;
1787 keysetlist->keysets[keysetlist->cnt++] = keyset;
1789 return keysetlist->cnt < keysetlist->max;
1791 EXPORT_SYMBOL_GPL(vcap_keyset_list_add);
1793 /* Add a actionset to a actionset list */
1794 static bool vcap_actionset_list_add(struct vcap_actionset_list *actionsetlist,
1795 enum vcap_actionfield_set actionset)
1799 if (actionsetlist->cnt < actionsetlist->max) {
1800 /* Avoid duplicates */
1801 for (idx = 0; idx < actionsetlist->cnt; ++idx)
1802 if (actionsetlist->actionsets[idx] == actionset)
1803 return actionsetlist->cnt < actionsetlist->max;
1804 actionsetlist->actionsets[actionsetlist->cnt++] = actionset;
1806 return actionsetlist->cnt < actionsetlist->max;
1809 /* map keyset id to a string with the keyset name */
1810 const char *vcap_keyset_name(struct vcap_control *vctrl,
1811 enum vcap_keyfield_set keyset)
1813 return vctrl->stats->keyfield_set_names[keyset];
1815 EXPORT_SYMBOL_GPL(vcap_keyset_name);
1817 /* map key field id to a string with the key name */
1818 const char *vcap_keyfield_name(struct vcap_control *vctrl,
1819 enum vcap_key_field key)
1821 return vctrl->stats->keyfield_names[key];
1823 EXPORT_SYMBOL_GPL(vcap_keyfield_name);
1825 /* map actionset id to a string with the actionset name */
1826 const char *vcap_actionset_name(struct vcap_control *vctrl,
1827 enum vcap_actionfield_set actionset)
1829 return vctrl->stats->actionfield_set_names[actionset];
1832 /* map action field id to a string with the action name */
1833 const char *vcap_actionfield_name(struct vcap_control *vctrl,
1834 enum vcap_action_field action)
1836 return vctrl->stats->actionfield_names[action];
1839 /* Return the keyfield that matches a key in a keyset */
1840 static const struct vcap_field *
1841 vcap_find_keyset_keyfield(struct vcap_control *vctrl,
1842 enum vcap_type vtype,
1843 enum vcap_keyfield_set keyset,
1844 enum vcap_key_field key)
1846 const struct vcap_field *fields;
1849 fields = vcap_keyfields(vctrl, vtype, keyset);
1853 /* Iterate the keyfields of the keyset */
1854 count = vcap_keyfield_count(vctrl, vtype, keyset);
1855 for (idx = 0; idx < count; ++idx) {
1856 if (fields[idx].width == 0)
1860 return &fields[idx];
1866 /* Match a list of keys against the keysets available in a vcap type */
1867 static bool _vcap_rule_find_keysets(struct vcap_rule_internal *ri,
1868 struct vcap_keyset_list *matches)
1870 const struct vcap_client_keyfield *ckf;
1871 int keyset, found, keycount, map_size;
1872 const struct vcap_field **map;
1873 enum vcap_type vtype;
1875 vtype = ri->admin->vtype;
1876 map = ri->vctrl->vcaps[vtype].keyfield_set_map;
1877 map_size = ri->vctrl->vcaps[vtype].keyfield_set_size;
1879 /* Get a count of the keyfields we want to match */
1881 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1885 /* Iterate the keysets of the VCAP */
1886 for (keyset = 0; keyset < map_size; ++keyset) {
1890 /* Iterate the keys in the rule */
1892 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1893 if (vcap_find_keyset_keyfield(ri->vctrl, vtype,
1894 keyset, ckf->ctrl.key))
1897 /* Save the keyset if all keyfields were found */
1898 if (found == keycount)
1899 if (!vcap_keyset_list_add(matches, keyset))
1900 /* bail out when the quota is filled */
1904 return matches->cnt > 0;
1907 /* Match a list of keys against the keysets available in a vcap type */
1908 bool vcap_rule_find_keysets(struct vcap_rule *rule,
1909 struct vcap_keyset_list *matches)
1911 struct vcap_rule_internal *ri = to_intrule(rule);
1913 return _vcap_rule_find_keysets(ri, matches);
1915 EXPORT_SYMBOL_GPL(vcap_rule_find_keysets);
1917 /* Return the actionfield that matches a action in a actionset */
1918 static const struct vcap_field *
1919 vcap_find_actionset_actionfield(struct vcap_control *vctrl,
1920 enum vcap_type vtype,
1921 enum vcap_actionfield_set actionset,
1922 enum vcap_action_field action)
1924 const struct vcap_field *fields;
1927 fields = vcap_actionfields(vctrl, vtype, actionset);
1931 /* Iterate the actionfields of the actionset */
1932 count = vcap_actionfield_count(vctrl, vtype, actionset);
1933 for (idx = 0; idx < count; ++idx) {
1934 if (fields[idx].width == 0)
1938 return &fields[idx];
1944 /* Match a list of actions against the actionsets available in a vcap type */
1945 static bool vcap_rule_find_actionsets(struct vcap_rule_internal *ri,
1946 struct vcap_actionset_list *matches)
1948 int actionset, found, actioncount, map_size;
1949 const struct vcap_client_actionfield *ckf;
1950 const struct vcap_field **map;
1951 enum vcap_type vtype;
1953 vtype = ri->admin->vtype;
1954 map = ri->vctrl->vcaps[vtype].actionfield_set_map;
1955 map_size = ri->vctrl->vcaps[vtype].actionfield_set_size;
1957 /* Get a count of the actionfields we want to match */
1959 list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list)
1963 /* Iterate the actionsets of the VCAP */
1964 for (actionset = 0; actionset < map_size; ++actionset) {
1965 if (!map[actionset])
1968 /* Iterate the actions in the rule */
1970 list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list)
1971 if (vcap_find_actionset_actionfield(ri->vctrl, vtype,
1976 /* Save the actionset if all actionfields were found */
1977 if (found == actioncount)
1978 if (!vcap_actionset_list_add(matches, actionset))
1979 /* bail out when the quota is filled */
1983 return matches->cnt > 0;
1986 /* Validate a rule with respect to available port keys */
1987 int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
1989 struct vcap_rule_internal *ri = to_intrule(rule);
1990 struct vcap_keyset_list matches = {};
1991 enum vcap_keyfield_set keysets[10];
1994 ret = vcap_api_check(ri->vctrl);
1998 ri->data.exterr = VCAP_ERR_NO_ADMIN;
2002 ri->data.exterr = VCAP_ERR_NO_NETDEV;
2006 matches.keysets = keysets;
2007 matches.max = ARRAY_SIZE(keysets);
2008 if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
2009 /* Iterate over rule keyfields and select keysets that fits */
2010 if (!_vcap_rule_find_keysets(ri, &matches)) {
2011 ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
2015 /* prepare for keyset validation */
2016 keysets[0] = ri->data.keyset;
2020 /* Pick a keyset that is supported in the port lookups */
2021 ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule,
2022 &matches, l3_proto);
2024 pr_err("%s:%d: keyset validation failed: %d\n",
2025 __func__, __LINE__, ret);
2026 ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH;
2029 /* use the keyset that is supported in the port lookups */
2030 ret = vcap_set_rule_set_keyset(rule, ret);
2032 pr_err("%s:%d: keyset was not updated: %d\n",
2033 __func__, __LINE__, ret);
2036 if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
2037 struct vcap_actionset_list matches = {};
2038 enum vcap_actionfield_set actionsets[10];
2040 matches.actionsets = actionsets;
2041 matches.max = ARRAY_SIZE(actionsets);
2043 /* Find an actionset that fits the rule actions */
2044 if (!vcap_rule_find_actionsets(ri, &matches)) {
2045 ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
2048 ret = vcap_set_rule_set_actionset(rule, actionsets[0]);
2050 pr_err("%s:%d: actionset was not updated: %d\n",
2051 __func__, __LINE__, ret);
2055 vcap_add_type_keyfield(rule);
2056 vcap_add_type_actionfield(rule);
2057 /* Add default fields to this rule */
2058 ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule);
2060 /* Rule size is the maximum of the entry and action subword count */
2061 ri->size = max(ri->keyset_sw, ri->actionset_sw);
2063 /* Finally check if there is room for the rule in the VCAP */
2064 return vcap_rule_space(ri->admin, ri->size);
2066 EXPORT_SYMBOL_GPL(vcap_val_rule);
2068 /* Entries are sorted with increasing values of sort_key.
2069 * I.e. Lowest numerical sort_key is first in list.
2070 * In order to locate largest keys first in list we negate the key size with
2071 * (max_size - size).
2073 static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio)
2075 return ((max_size - size) << 24) | (user << 16) | prio;
2078 /* calculate the address of the next rule after this (lower address and prio) */
2079 static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri)
2081 return ((addr - ri->size) / ri->size) * ri->size;
2084 /* Assign a unique rule id and autogenerate one if id == 0 */
2085 static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
2087 if (ri->data.id != 0)
2090 for (u32 next_id = 1; next_id < ~0; ++next_id) {
2091 if (!vcap_rule_exists(ri->vctrl, next_id)) {
2092 ri->data.id = next_id;
2099 static int vcap_insert_rule(struct vcap_rule_internal *ri,
2100 struct vcap_rule_move *move)
2102 int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count;
2103 struct vcap_rule_internal *duprule, *iter, *elem = NULL;
2104 struct vcap_admin *admin = ri->admin;
2107 ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user,
2110 /* Insert the new rule in the list of rule based on the sort key
2111 * If the rule needs to be inserted between existing rules then move
2112 * these rules to make room for the new rule and update their start
2115 list_for_each_entry(iter, &admin->rules, list) {
2116 if (ri->sort_key < iter->sort_key) {
2123 ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri);
2124 admin->last_used_addr = ri->addr;
2126 /* Add a copy of the rule to the VCAP list */
2127 duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
2128 if (IS_ERR(duprule))
2129 return PTR_ERR(duprule);
2131 list_add_tail(&duprule->list, &admin->rules);
2135 /* Reuse the space of the current rule */
2136 addr = elem->addr + elem->size;
2137 ri->addr = vcap_next_rule_addr(addr, ri);
2140 /* Add a copy of the rule to the VCAP list */
2141 duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
2142 if (IS_ERR(duprule))
2143 return PTR_ERR(duprule);
2145 /* Add before the current entry */
2146 list_add_tail(&duprule->list, &elem->list);
2148 /* Update the current rule */
2149 elem->addr = vcap_next_rule_addr(addr, elem);
2152 /* Update the address in the remaining rules in the list */
2153 list_for_each_entry_continue(elem, &admin->rules, list) {
2154 elem->addr = vcap_next_rule_addr(addr, elem);
2158 /* Update the move info */
2159 move->addr = admin->last_used_addr;
2160 move->count = ri->addr - addr;
2161 move->offset = admin->last_used_addr - addr;
2162 admin->last_used_addr = addr;
2166 static void vcap_move_rules(struct vcap_rule_internal *ri,
2167 struct vcap_rule_move *move)
2169 ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr,
2170 move->offset, move->count);
2173 /* Check if the chain is already used to enable a VCAP lookup for this port */
2174 static bool vcap_is_chain_used(struct vcap_control *vctrl,
2175 struct net_device *ndev, int src_cid)
2177 struct vcap_enabled_port *eport;
2178 struct vcap_admin *admin;
2180 list_for_each_entry(admin, &vctrl->list, list)
2181 list_for_each_entry(eport, &admin->enabled, list)
2182 if (eport->src_cid == src_cid && eport->ndev == ndev)
2188 /* Fetch the next chain in the enabled list for the port */
2189 static int vcap_get_next_chain(struct vcap_control *vctrl,
2190 struct net_device *ndev,
2193 struct vcap_enabled_port *eport;
2194 struct vcap_admin *admin;
2196 list_for_each_entry(admin, &vctrl->list, list) {
2197 list_for_each_entry(eport, &admin->enabled, list) {
2198 if (eport->ndev != ndev)
2200 if (eport->src_cid == dst_cid)
2201 return eport->dst_cid;
2208 static bool vcap_path_exist(struct vcap_control *vctrl, struct net_device *ndev,
2211 int cid = rounddown(dst_cid, VCAP_CID_LOOKUP_SIZE);
2212 struct vcap_enabled_port *eport = NULL;
2213 struct vcap_enabled_port *elem;
2214 struct vcap_admin *admin;
2217 if (cid == 0) /* Chain zero is always available */
2220 /* Find first entry that starts from chain 0*/
2221 list_for_each_entry(admin, &vctrl->list, list) {
2222 list_for_each_entry(elem, &admin->enabled, list) {
2223 if (elem->src_cid == 0 && elem->ndev == ndev) {
2235 tmp = eport->dst_cid;
2236 while (tmp != cid && tmp != 0)
2237 tmp = vcap_get_next_chain(vctrl, ndev, tmp);
2242 /* Internal clients can always store their rules in HW
2243 * External clients can store their rules if the chain is enabled all
2244 * the way from chain 0, otherwise the rule will be cached until
2245 * the chain is enabled.
2247 static void vcap_rule_set_state(struct vcap_rule_internal *ri)
2249 if (ri->data.user <= VCAP_USER_QOS)
2250 ri->state = VCAP_RS_PERMANENT;
2251 else if (vcap_path_exist(ri->vctrl, ri->ndev, ri->data.vcap_chain_id))
2252 ri->state = VCAP_RS_ENABLED;
2254 ri->state = VCAP_RS_DISABLED;
2257 /* Encode and write a validated rule to the VCAP */
2258 int vcap_add_rule(struct vcap_rule *rule)
2260 struct vcap_rule_internal *ri = to_intrule(rule);
2261 struct vcap_rule_move move = {0};
2262 struct vcap_counter ctr = {0};
2265 ret = vcap_api_check(ri->vctrl);
2268 /* Insert the new rule in the list of vcap rules */
2269 mutex_lock(&ri->admin->lock);
2271 vcap_rule_set_state(ri);
2272 ret = vcap_insert_rule(ri, &move);
2274 pr_err("%s:%d: could not insert rule in vcap list: %d\n",
2275 __func__, __LINE__, ret);
2279 vcap_move_rules(ri, &move);
2281 /* Set the counter to zero */
2282 ret = vcap_write_counter(ri, &ctr);
2286 if (ri->state == VCAP_RS_DISABLED) {
2287 /* Erase the rule area */
2288 ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
2292 vcap_erase_cache(ri);
2293 ret = vcap_encode_rule(ri);
2295 pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret);
2299 ret = vcap_write_rule(ri);
2301 pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret);
2305 mutex_unlock(&ri->admin->lock);
2308 EXPORT_SYMBOL_GPL(vcap_add_rule);
2310 /* Allocate a new rule with the provided arguments */
2311 struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
2312 struct net_device *ndev, int vcap_chain_id,
2313 enum vcap_user user, u16 priority,
2316 struct vcap_rule_internal *ri;
2317 struct vcap_admin *admin;
2320 err = vcap_api_check(vctrl);
2322 return ERR_PTR(err);
2324 return ERR_PTR(-ENODEV);
2325 /* Get the VCAP instance */
2326 admin = vcap_find_admin(vctrl, vcap_chain_id);
2328 return ERR_PTR(-ENOENT);
2329 /* Sanity check that this VCAP is supported on this platform */
2330 if (vctrl->vcaps[admin->vtype].rows == 0)
2331 return ERR_PTR(-EINVAL);
2333 mutex_lock(&admin->lock);
2334 /* Check if a rule with this id already exists */
2335 if (vcap_rule_exists(vctrl, id)) {
2340 /* Check if there is room for the rule in the block(s) of the VCAP */
2341 maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */
2342 if (vcap_rule_space(admin, maxsize)) {
2347 /* Create a container for the rule and return it */
2348 ri = kzalloc(sizeof(*ri), GFP_KERNEL);
2354 ri->data.vcap_chain_id = vcap_chain_id;
2355 ri->data.user = user;
2356 ri->data.priority = priority;
2358 ri->data.keyset = VCAP_KFS_NO_VALUE;
2359 ri->data.actionset = VCAP_AFS_NO_VALUE;
2360 INIT_LIST_HEAD(&ri->list);
2361 INIT_LIST_HEAD(&ri->data.keyfields);
2362 INIT_LIST_HEAD(&ri->data.actionfields);
2364 ri->admin = admin; /* refer to the vcap instance */
2365 ri->vctrl = vctrl; /* refer to the client */
2367 if (vcap_set_rule_id(ri) == 0) {
2372 mutex_unlock(&admin->lock);
2373 return (struct vcap_rule *)ri;
2378 mutex_unlock(&admin->lock);
2379 return ERR_PTR(err);
2382 EXPORT_SYMBOL_GPL(vcap_alloc_rule);
2384 /* Free mem of a rule owned by client after the rule as been added to the VCAP */
2385 void vcap_free_rule(struct vcap_rule *rule)
2387 struct vcap_rule_internal *ri = to_intrule(rule);
2388 struct vcap_client_actionfield *caf, *next_caf;
2389 struct vcap_client_keyfield *ckf, *next_ckf;
2391 /* Deallocate the list of keys and actions */
2392 list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
2393 list_del(&ckf->ctrl.list);
2396 list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
2397 list_del(&caf->ctrl.list);
2400 /* Deallocate the rule */
2403 EXPORT_SYMBOL_GPL(vcap_free_rule);
2405 /* Decode a rule from the VCAP cache and return a copy */
2406 struct vcap_rule *vcap_decode_rule(struct vcap_rule_internal *elem)
2408 struct vcap_rule_internal *ri;
2411 ri = vcap_dup_rule(elem, elem->state == VCAP_RS_DISABLED);
2413 return ERR_CAST(ri);
2415 if (ri->state == VCAP_RS_DISABLED)
2418 err = vcap_read_rule(ri);
2420 return ERR_PTR(err);
2422 err = vcap_decode_keyset(ri);
2424 return ERR_PTR(err);
2426 err = vcap_decode_actionset(ri);
2428 return ERR_PTR(err);
2434 struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id)
2436 struct vcap_rule_internal *elem;
2437 struct vcap_rule *rule;
2440 err = vcap_api_check(vctrl);
2442 return ERR_PTR(err);
2444 elem = vcap_get_locked_rule(vctrl, id);
2446 return ERR_PTR(-ENOENT);
2448 rule = vcap_decode_rule(elem);
2449 mutex_unlock(&elem->admin->lock);
2452 EXPORT_SYMBOL_GPL(vcap_get_rule);
2454 /* Update existing rule */
2455 int vcap_mod_rule(struct vcap_rule *rule)
2457 struct vcap_rule_internal *ri = to_intrule(rule);
2458 struct vcap_counter ctr;
2461 err = vcap_api_check(ri->vctrl);
2465 if (!vcap_get_locked_rule(ri->vctrl, ri->data.id))
2468 vcap_rule_set_state(ri);
2469 if (ri->state == VCAP_RS_DISABLED)
2472 /* Encode the bitstreams to the VCAP cache */
2473 vcap_erase_cache(ri);
2474 err = vcap_encode_rule(ri);
2478 err = vcap_write_rule(ri);
2482 memset(&ctr, 0, sizeof(ctr));
2483 err = vcap_write_counter(ri, &ctr);
2486 mutex_unlock(&ri->admin->lock);
2489 EXPORT_SYMBOL_GPL(vcap_mod_rule);
2491 /* Return the alignment offset for a new rule address */
2492 static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset)
2494 return (el->addr + offset) % el->size;
2497 /* Update the rule address with an offset */
2498 static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset)
2503 /* Rules needs to be moved to fill the gap of the deleted rule */
2504 static int vcap_fill_rule_gap(struct vcap_rule_internal *ri)
2506 struct vcap_admin *admin = ri->admin;
2507 struct vcap_rule_internal *elem;
2508 struct vcap_rule_move move;
2509 int gap = 0, offset = 0;
2511 /* If the first rule is deleted: Move other rules to the top */
2512 if (list_is_first(&ri->list, &admin->rules))
2513 offset = admin->last_valid_addr + 1 - ri->addr - ri->size;
2515 /* Locate gaps between odd size rules and adjust the move */
2517 list_for_each_entry_continue(elem, &admin->rules, list)
2518 gap += vcap_valid_rule_move(elem, ri->size);
2520 /* Update the address in the remaining rules in the list */
2522 list_for_each_entry_continue(elem, &admin->rules, list)
2523 vcap_adjust_rule_addr(elem, ri->size + gap + offset);
2525 /* Update the move info */
2526 move.addr = admin->last_used_addr;
2527 move.count = ri->addr - admin->last_used_addr - gap;
2528 move.offset = -(ri->size + gap + offset);
2530 /* Do the actual move operation */
2531 vcap_move_rules(ri, &move);
2533 return gap + offset;
2536 /* Delete rule in a VCAP instance */
2537 int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
2539 struct vcap_rule_internal *ri, *elem;
2540 struct vcap_admin *admin;
2543 /* This will later also handle rule moving */
2546 err = vcap_api_check(vctrl);
2549 /* Look for the rule id in all vcaps */
2550 ri = vcap_get_locked_rule(vctrl, id);
2556 if (ri->addr > admin->last_used_addr)
2557 gap = vcap_fill_rule_gap(ri);
2559 /* Delete the rule from the list of rules and the cache */
2560 list_del(&ri->list);
2561 vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap);
2562 vcap_free_rule(&ri->data);
2564 /* Update the last used address, set to default when no rules */
2565 if (list_empty(&admin->rules)) {
2566 admin->last_used_addr = admin->last_valid_addr + 1;
2568 elem = list_last_entry(&admin->rules, struct vcap_rule_internal,
2570 admin->last_used_addr = elem->addr;
2573 mutex_unlock(&admin->lock);
2576 EXPORT_SYMBOL_GPL(vcap_del_rule);
2578 /* Delete all rules in the VCAP instance */
2579 int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin)
2581 struct vcap_enabled_port *eport, *next_eport;
2582 struct vcap_rule_internal *ri, *next_ri;
2583 int ret = vcap_api_check(vctrl);
2588 mutex_lock(&admin->lock);
2589 list_for_each_entry_safe(ri, next_ri, &admin->rules, list) {
2590 vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size);
2591 list_del(&ri->list);
2592 vcap_free_rule(&ri->data);
2594 admin->last_used_addr = admin->last_valid_addr;
2596 /* Remove list of enabled ports */
2597 list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) {
2598 list_del(&eport->list);
2601 mutex_unlock(&admin->lock);
2605 EXPORT_SYMBOL_GPL(vcap_del_rules);
2607 /* Find a client key field in a rule */
2608 static struct vcap_client_keyfield *
2609 vcap_find_keyfield(struct vcap_rule *rule, enum vcap_key_field key)
2611 struct vcap_rule_internal *ri = to_intrule(rule);
2612 struct vcap_client_keyfield *ckf;
2614 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2615 if (ckf->ctrl.key == key)
2620 /* Find information on a key field in a rule */
2621 const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
2622 enum vcap_key_field key)
2624 struct vcap_rule_internal *ri = to_intrule(rule);
2625 enum vcap_keyfield_set keyset = rule->keyset;
2626 enum vcap_type vt = ri->admin->vtype;
2627 const struct vcap_field *fields;
2629 if (keyset == VCAP_KFS_NO_VALUE)
2631 fields = vcap_keyfields(ri->vctrl, vt, keyset);
2634 return &fields[key];
2636 EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
2638 /* Check if the keyfield is already in the rule */
2639 static bool vcap_keyfield_unique(struct vcap_rule *rule,
2640 enum vcap_key_field key)
2642 struct vcap_rule_internal *ri = to_intrule(rule);
2643 const struct vcap_client_keyfield *ckf;
2645 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2646 if (ckf->ctrl.key == key)
2651 /* Check if the keyfield is in the keyset */
2652 static bool vcap_keyfield_match_keyset(struct vcap_rule *rule,
2653 enum vcap_key_field key)
2655 struct vcap_rule_internal *ri = to_intrule(rule);
2656 enum vcap_keyfield_set keyset = rule->keyset;
2657 enum vcap_type vt = ri->admin->vtype;
2658 const struct vcap_field *fields;
2660 /* the field is accepted if the rule has no keyset yet */
2661 if (keyset == VCAP_KFS_NO_VALUE)
2663 fields = vcap_keyfields(ri->vctrl, vt, keyset);
2666 /* if there is a width there is a way */
2667 return fields[key].width > 0;
2670 static int vcap_rule_add_key(struct vcap_rule *rule,
2671 enum vcap_key_field key,
2672 enum vcap_field_type ftype,
2673 struct vcap_client_keyfield_data *data)
2675 struct vcap_rule_internal *ri = to_intrule(rule);
2676 struct vcap_client_keyfield *field;
2678 if (!vcap_keyfield_unique(rule, key)) {
2679 pr_warn("%s:%d: keyfield %s is already in the rule\n",
2681 vcap_keyfield_name(ri->vctrl, key));
2685 if (!vcap_keyfield_match_keyset(rule, key)) {
2686 pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n",
2688 vcap_keyfield_name(ri->vctrl, key));
2692 field = kzalloc(sizeof(*field), GFP_KERNEL);
2695 memcpy(&field->data, data, sizeof(field->data));
2696 field->ctrl.key = key;
2697 field->ctrl.type = ftype;
2698 list_add_tail(&field->ctrl.list, &rule->keyfields);
2702 static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
2720 /* Add a bit key with value and mask to the rule */
2721 int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
2724 struct vcap_client_keyfield_data data;
2726 vcap_rule_set_key_bitsize(&data.u1, val);
2727 return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
2729 EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
2731 /* Add a 32 bit key field with value and mask to the rule */
2732 int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2733 u32 value, u32 mask)
2735 struct vcap_client_keyfield_data data;
2737 data.u32.value = value;
2738 data.u32.mask = mask;
2739 return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
2741 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
2743 /* Add a 48 bit key with value and mask to the rule */
2744 int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
2745 struct vcap_u48_key *fieldval)
2747 struct vcap_client_keyfield_data data;
2749 memcpy(&data.u48, fieldval, sizeof(data.u48));
2750 return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
2752 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
2754 /* Add a 72 bit key with value and mask to the rule */
2755 int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
2756 struct vcap_u72_key *fieldval)
2758 struct vcap_client_keyfield_data data;
2760 memcpy(&data.u72, fieldval, sizeof(data.u72));
2761 return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
2763 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
2765 /* Add a 128 bit key with value and mask to the rule */
2766 int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key,
2767 struct vcap_u128_key *fieldval)
2769 struct vcap_client_keyfield_data data;
2771 memcpy(&data.u128, fieldval, sizeof(data.u128));
2772 return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data);
2774 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128);
2776 int vcap_rule_get_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2777 u32 *value, u32 *mask)
2779 struct vcap_client_keyfield *ckf;
2781 ckf = vcap_find_keyfield(rule, key);
2785 *value = ckf->data.u32.value;
2786 *mask = ckf->data.u32.mask;
2790 EXPORT_SYMBOL_GPL(vcap_rule_get_key_u32);
2792 /* Find a client action field in a rule */
2793 struct vcap_client_actionfield *
2794 vcap_find_actionfield(struct vcap_rule *rule, enum vcap_action_field act)
2796 struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule;
2797 struct vcap_client_actionfield *caf;
2799 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2800 if (caf->ctrl.action == act)
2804 EXPORT_SYMBOL_GPL(vcap_find_actionfield);
2806 /* Check if the actionfield is already in the rule */
2807 static bool vcap_actionfield_unique(struct vcap_rule *rule,
2808 enum vcap_action_field act)
2810 struct vcap_rule_internal *ri = to_intrule(rule);
2811 const struct vcap_client_actionfield *caf;
2813 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2814 if (caf->ctrl.action == act)
2819 /* Check if the actionfield is in the actionset */
2820 static bool vcap_actionfield_match_actionset(struct vcap_rule *rule,
2821 enum vcap_action_field action)
2823 enum vcap_actionfield_set actionset = rule->actionset;
2824 struct vcap_rule_internal *ri = to_intrule(rule);
2825 enum vcap_type vt = ri->admin->vtype;
2826 const struct vcap_field *fields;
2828 /* the field is accepted if the rule has no actionset yet */
2829 if (actionset == VCAP_AFS_NO_VALUE)
2831 fields = vcap_actionfields(ri->vctrl, vt, actionset);
2834 /* if there is a width there is a way */
2835 return fields[action].width > 0;
2838 static int vcap_rule_add_action(struct vcap_rule *rule,
2839 enum vcap_action_field action,
2840 enum vcap_field_type ftype,
2841 struct vcap_client_actionfield_data *data)
2843 struct vcap_rule_internal *ri = to_intrule(rule);
2844 struct vcap_client_actionfield *field;
2846 if (!vcap_actionfield_unique(rule, action)) {
2847 pr_warn("%s:%d: actionfield %s is already in the rule\n",
2849 vcap_actionfield_name(ri->vctrl, action));
2853 if (!vcap_actionfield_match_actionset(rule, action)) {
2854 pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n",
2856 vcap_actionfield_name(ri->vctrl, action));
2860 field = kzalloc(sizeof(*field), GFP_KERNEL);
2863 memcpy(&field->data, data, sizeof(field->data));
2864 field->ctrl.action = action;
2865 field->ctrl.type = ftype;
2866 list_add_tail(&field->ctrl.list, &rule->actionfields);
2870 static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
2886 /* Add a bit action with value to the rule */
2887 int vcap_rule_add_action_bit(struct vcap_rule *rule,
2888 enum vcap_action_field action,
2891 struct vcap_client_actionfield_data data;
2893 vcap_rule_set_action_bitsize(&data.u1, val);
2894 return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
2896 EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
2898 /* Add a 32 bit action field with value to the rule */
2899 int vcap_rule_add_action_u32(struct vcap_rule *rule,
2900 enum vcap_action_field action,
2903 struct vcap_client_actionfield_data data;
2905 data.u32.value = value;
2906 return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
2908 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
2910 static int vcap_read_counter(struct vcap_rule_internal *ri,
2911 struct vcap_counter *ctr)
2913 struct vcap_admin *admin = ri->admin;
2915 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER,
2917 ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER,
2919 ctr->value = admin->cache.counter;
2920 ctr->sticky = admin->cache.sticky;
2924 /* Copy to host byte order */
2925 void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
2929 for (idx = 0; idx < count; ++idx, ++dst)
2930 *dst = src[count - idx - 1];
2932 EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
2934 /* Convert validation error code into tc extact error message */
2935 void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
2937 switch (vrule->exterr) {
2940 case VCAP_ERR_NO_ADMIN:
2941 NL_SET_ERR_MSG_MOD(fco->common.extack,
2942 "Missing VCAP instance");
2944 case VCAP_ERR_NO_NETDEV:
2945 NL_SET_ERR_MSG_MOD(fco->common.extack,
2946 "Missing network interface");
2948 case VCAP_ERR_NO_KEYSET_MATCH:
2949 NL_SET_ERR_MSG_MOD(fco->common.extack,
2950 "No keyset matched the filter keys");
2952 case VCAP_ERR_NO_ACTIONSET_MATCH:
2953 NL_SET_ERR_MSG_MOD(fco->common.extack,
2954 "No actionset matched the filter actions");
2956 case VCAP_ERR_NO_PORT_KEYSET_MATCH:
2957 NL_SET_ERR_MSG_MOD(fco->common.extack,
2958 "No port keyset matched the filter keys");
2962 EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
2964 /* Write a rule to VCAP HW to enable it */
2965 static int vcap_enable_rule(struct vcap_rule_internal *ri)
2967 struct vcap_client_actionfield *af, *naf;
2968 struct vcap_client_keyfield *kf, *nkf;
2971 vcap_erase_cache(ri);
2972 err = vcap_encode_rule(ri);
2975 err = vcap_write_rule(ri);
2979 /* Deallocate the list of keys and actions */
2980 list_for_each_entry_safe(kf, nkf, &ri->data.keyfields, ctrl.list) {
2981 list_del(&kf->ctrl.list);
2984 list_for_each_entry_safe(af, naf, &ri->data.actionfields, ctrl.list) {
2985 list_del(&af->ctrl.list);
2988 ri->state = VCAP_RS_ENABLED;
2993 /* Enable all disabled rules for a specific chain/port in the VCAP HW */
2994 static int vcap_enable_rules(struct vcap_control *vctrl,
2995 struct net_device *ndev, int chain)
2997 int next_chain = chain + VCAP_CID_LOOKUP_SIZE;
2998 struct vcap_rule_internal *ri;
2999 struct vcap_admin *admin;
3002 list_for_each_entry(admin, &vctrl->list, list) {
3003 if (!(chain >= admin->first_cid && chain <= admin->last_cid))
3006 /* Found the admin, now find the offloadable rules */
3007 mutex_lock(&admin->lock);
3008 list_for_each_entry(ri, &admin->rules, list) {
3009 /* Is the rule in the lookup defined by the chain */
3010 if (!(ri->data.vcap_chain_id >= chain &&
3011 ri->data.vcap_chain_id < next_chain)) {
3015 if (ri->ndev != ndev)
3018 if (ri->state != VCAP_RS_DISABLED)
3021 err = vcap_enable_rule(ri);
3025 mutex_unlock(&admin->lock);
3032 /* Read and erase a rule from VCAP HW to disable it */
3033 static int vcap_disable_rule(struct vcap_rule_internal *ri)
3037 err = vcap_read_rule(ri);
3040 err = vcap_decode_keyset(ri);
3043 err = vcap_decode_actionset(ri);
3047 ri->state = VCAP_RS_DISABLED;
3048 ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
3052 /* Disable all enabled rules for a specific chain/port in the VCAP HW */
3053 static int vcap_disable_rules(struct vcap_control *vctrl,
3054 struct net_device *ndev, int chain)
3056 struct vcap_rule_internal *ri;
3057 struct vcap_admin *admin;
3060 list_for_each_entry(admin, &vctrl->list, list) {
3061 if (!(chain >= admin->first_cid && chain <= admin->last_cid))
3064 /* Found the admin, now find the rules on the chain */
3065 mutex_lock(&admin->lock);
3066 list_for_each_entry(ri, &admin->rules, list) {
3067 if (ri->data.vcap_chain_id != chain)
3070 if (ri->ndev != ndev)
3073 if (ri->state != VCAP_RS_ENABLED)
3076 err = vcap_disable_rule(ri);
3080 mutex_unlock(&admin->lock);
3087 /* Check if this port is already enabled for this VCAP instance */
3088 static bool vcap_is_enabled(struct vcap_control *vctrl, struct net_device *ndev,
3091 struct vcap_enabled_port *eport;
3092 struct vcap_admin *admin;
3094 list_for_each_entry(admin, &vctrl->list, list)
3095 list_for_each_entry(eport, &admin->enabled, list)
3096 if (eport->dst_cid == dst_cid && eport->ndev == ndev)
3102 /* Enable this port and chain id in a VCAP instance */
3103 static int vcap_enable(struct vcap_control *vctrl, struct net_device *ndev,
3104 unsigned long cookie, int src_cid, int dst_cid)
3106 struct vcap_enabled_port *eport;
3107 struct vcap_admin *admin;
3109 if (src_cid >= dst_cid)
3112 admin = vcap_find_admin(vctrl, dst_cid);
3116 eport = kzalloc(sizeof(*eport), GFP_KERNEL);
3121 eport->cookie = cookie;
3122 eport->src_cid = src_cid;
3123 eport->dst_cid = dst_cid;
3124 mutex_lock(&admin->lock);
3125 list_add_tail(&eport->list, &admin->enabled);
3126 mutex_unlock(&admin->lock);
3128 if (vcap_path_exist(vctrl, ndev, src_cid)) {
3129 /* Enable chained lookups */
3131 admin = vcap_find_admin(vctrl, dst_cid);
3135 vcap_enable_rules(vctrl, ndev, dst_cid);
3136 dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid);
3142 /* Disable this port and chain id for a VCAP instance */
3143 static int vcap_disable(struct vcap_control *vctrl, struct net_device *ndev,
3144 unsigned long cookie)
3146 struct vcap_enabled_port *elem, *eport = NULL;
3147 struct vcap_admin *found = NULL, *admin;
3150 list_for_each_entry(admin, &vctrl->list, list) {
3151 list_for_each_entry(elem, &admin->enabled, list) {
3152 if (elem->cookie == cookie && elem->ndev == ndev) {
3165 /* Disable chained lookups */
3166 dst_cid = eport->dst_cid;
3168 admin = vcap_find_admin(vctrl, dst_cid);
3172 vcap_disable_rules(vctrl, ndev, dst_cid);
3173 dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid);
3176 mutex_lock(&found->lock);
3177 list_del(&eport->list);
3178 mutex_unlock(&found->lock);
3183 /* Enable/Disable the VCAP instance lookups */
3184 int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev,
3185 int src_cid, int dst_cid, unsigned long cookie,
3190 err = vcap_api_check(vctrl);
3197 /* Source and destination must be the first chain in a lookup */
3198 if (src_cid % VCAP_CID_LOOKUP_SIZE)
3200 if (dst_cid % VCAP_CID_LOOKUP_SIZE)
3204 if (vcap_is_enabled(vctrl, ndev, dst_cid))
3206 if (vcap_is_chain_used(vctrl, ndev, src_cid))
3207 return -EADDRNOTAVAIL;
3208 err = vcap_enable(vctrl, ndev, cookie, src_cid, dst_cid);
3210 err = vcap_disable(vctrl, ndev, cookie);
3215 EXPORT_SYMBOL_GPL(vcap_enable_lookups);
3217 /* Is this chain id the last lookup of all VCAPs */
3218 bool vcap_is_last_chain(struct vcap_control *vctrl, int cid, bool ingress)
3220 struct vcap_admin *admin;
3223 if (vcap_api_check(vctrl))
3226 admin = vcap_find_admin(vctrl, cid);
3230 if (!vcap_admin_is_last(vctrl, admin, ingress))
3233 /* This must be the last lookup in this VCAP type */
3234 lookup = vcap_chain_id_to_lookup(admin, cid);
3235 return lookup == admin->lookups - 1;
3237 EXPORT_SYMBOL_GPL(vcap_is_last_chain);
3239 /* Set a rule counter id (for certain vcaps only) */
3240 void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id)
3242 struct vcap_rule_internal *ri = to_intrule(rule);
3244 ri->counter_id = counter_id;
3246 EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id);
3248 int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
3250 struct vcap_rule_internal *ri = to_intrule(rule);
3253 err = vcap_api_check(ri->vctrl);
3257 pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
3261 mutex_lock(&ri->admin->lock);
3262 err = vcap_write_counter(ri, ctr);
3263 mutex_unlock(&ri->admin->lock);
3267 EXPORT_SYMBOL_GPL(vcap_rule_set_counter);
3269 int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
3271 struct vcap_rule_internal *ri = to_intrule(rule);
3274 err = vcap_api_check(ri->vctrl);
3278 pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
3282 mutex_lock(&ri->admin->lock);
3283 err = vcap_read_counter(ri, ctr);
3284 mutex_unlock(&ri->admin->lock);
3288 EXPORT_SYMBOL_GPL(vcap_rule_get_counter);
3290 /* Get a copy of a client key field */
3291 static int vcap_rule_get_key(struct vcap_rule *rule,
3292 enum vcap_key_field key,
3293 struct vcap_client_keyfield *ckf)
3295 struct vcap_client_keyfield *field;
3297 field = vcap_find_keyfield(rule, key);
3300 memcpy(ckf, field, sizeof(*ckf));
3301 INIT_LIST_HEAD(&ckf->ctrl.list);
3305 /* Find a keyset having the same size as the provided rule, where the keyset
3306 * does not have a type id.
3308 static int vcap_rule_get_untyped_keyset(struct vcap_rule_internal *ri,
3309 struct vcap_keyset_list *matches)
3311 struct vcap_control *vctrl = ri->vctrl;
3312 enum vcap_type vt = ri->admin->vtype;
3313 const struct vcap_set *keyfield_set;
3316 keyfield_set = vctrl->vcaps[vt].keyfield_set;
3317 for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
3318 if (keyfield_set[idx].sw_per_item == ri->keyset_sw &&
3319 keyfield_set[idx].type_id == (u8)-1) {
3320 vcap_keyset_list_add(matches, idx);
3327 /* Get the keysets that matches the rule key type/mask */
3328 int vcap_rule_get_keysets(struct vcap_rule_internal *ri,
3329 struct vcap_keyset_list *matches)
3331 struct vcap_control *vctrl = ri->vctrl;
3332 enum vcap_type vt = ri->admin->vtype;
3333 const struct vcap_set *keyfield_set;
3334 struct vcap_client_keyfield kf = {};
3338 err = vcap_rule_get_key(&ri->data, VCAP_KF_TYPE, &kf);
3340 return vcap_rule_get_untyped_keyset(ri, matches);
3342 if (kf.ctrl.type == VCAP_FIELD_BIT) {
3343 value = kf.data.u1.value;
3344 mask = kf.data.u1.mask;
3345 } else if (kf.ctrl.type == VCAP_FIELD_U32) {
3346 value = kf.data.u32.value;
3347 mask = kf.data.u32.mask;
3352 keyfield_set = vctrl->vcaps[vt].keyfield_set;
3353 for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
3354 if (keyfield_set[idx].sw_per_item != ri->keyset_sw)
3357 if (keyfield_set[idx].type_id == (u8)-1) {
3358 vcap_keyset_list_add(matches, idx);
3362 if ((keyfield_set[idx].type_id & mask) == value)
3363 vcap_keyset_list_add(matches, idx);
3365 if (matches->cnt > 0)
3371 /* Collect packet counts from all rules with the same cookie */
3372 int vcap_get_rule_count_by_cookie(struct vcap_control *vctrl,
3373 struct vcap_counter *ctr, u64 cookie)
3375 struct vcap_rule_internal *ri;
3376 struct vcap_counter temp = {};
3377 struct vcap_admin *admin;
3380 err = vcap_api_check(vctrl);
3384 /* Iterate all rules in each VCAP instance */
3385 list_for_each_entry(admin, &vctrl->list, list) {
3386 mutex_lock(&admin->lock);
3387 list_for_each_entry(ri, &admin->rules, list) {
3388 if (ri->data.cookie != cookie)
3391 err = vcap_read_counter(ri, &temp);
3394 ctr->value += temp.value;
3396 /* Reset the rule counter */
3399 err = vcap_write_counter(ri, &temp);
3403 mutex_unlock(&admin->lock);
3408 mutex_unlock(&admin->lock);
3411 EXPORT_SYMBOL_GPL(vcap_get_rule_count_by_cookie);
3413 static int vcap_rule_mod_key(struct vcap_rule *rule,
3414 enum vcap_key_field key,
3415 enum vcap_field_type ftype,
3416 struct vcap_client_keyfield_data *data)
3418 struct vcap_client_keyfield *field;
3420 field = vcap_find_keyfield(rule, key);
3422 return vcap_rule_add_key(rule, key, ftype, data);
3423 memcpy(&field->data, data, sizeof(field->data));
3427 /* Modify a 32 bit key field with value and mask in the rule */
3428 int vcap_rule_mod_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
3429 u32 value, u32 mask)
3431 struct vcap_client_keyfield_data data;
3433 data.u32.value = value;
3434 data.u32.mask = mask;
3435 return vcap_rule_mod_key(rule, key, VCAP_FIELD_U32, &data);
3437 EXPORT_SYMBOL_GPL(vcap_rule_mod_key_u32);
3439 /* Remove a key field with value and mask in the rule */
3440 int vcap_rule_rem_key(struct vcap_rule *rule, enum vcap_key_field key)
3442 struct vcap_rule_internal *ri = to_intrule(rule);
3443 struct vcap_client_keyfield *field;
3445 field = vcap_find_keyfield(rule, key);
3447 pr_err("%s:%d: key %s is not in the rule\n",
3448 __func__, __LINE__, vcap_keyfield_name(ri->vctrl, key));
3451 /* Deallocate the key field */
3452 list_del(&field->ctrl.list);
3456 EXPORT_SYMBOL_GPL(vcap_rule_rem_key);
3458 static int vcap_rule_mod_action(struct vcap_rule *rule,
3459 enum vcap_action_field action,
3460 enum vcap_field_type ftype,
3461 struct vcap_client_actionfield_data *data)
3463 struct vcap_client_actionfield *field;
3465 field = vcap_find_actionfield(rule, action);
3467 return vcap_rule_add_action(rule, action, ftype, data);
3468 memcpy(&field->data, data, sizeof(field->data));
3472 /* Modify a 32 bit action field with value in the rule */
3473 int vcap_rule_mod_action_u32(struct vcap_rule *rule,
3474 enum vcap_action_field action,
3477 struct vcap_client_actionfield_data data;
3479 data.u32.value = value;
3480 return vcap_rule_mod_action(rule, action, VCAP_FIELD_U32, &data);
3482 EXPORT_SYMBOL_GPL(vcap_rule_mod_action_u32);
3484 /* Drop keys in a keylist and any keys that are not supported by the keyset */
3485 int vcap_filter_rule_keys(struct vcap_rule *rule,
3486 enum vcap_key_field keylist[], int length,
3487 bool drop_unsupported)
3489 struct vcap_rule_internal *ri = to_intrule(rule);
3490 struct vcap_client_keyfield *ckf, *next_ckf;
3491 const struct vcap_field *fields;
3492 enum vcap_key_field key;
3498 list_for_each_entry_safe(ckf, next_ckf,
3499 &ri->data.keyfields, ctrl.list) {
3500 key = ckf->ctrl.key;
3501 for (idx = 0; idx < length; ++idx)
3502 if (key == keylist[idx]) {
3503 list_del(&ckf->ctrl.list);
3510 if (drop_unsupported) {
3512 fields = vcap_keyfields(ri->vctrl, ri->admin->vtype,
3516 list_for_each_entry_safe(ckf, next_ckf,
3517 &ri->data.keyfields, ctrl.list) {
3518 key = ckf->ctrl.key;
3519 if (fields[key].width == 0) {
3520 list_del(&ckf->ctrl.list);
3528 EXPORT_SYMBOL_GPL(vcap_filter_rule_keys);
3530 /* Select the keyset from the list that results in the smallest rule size */
3531 enum vcap_keyfield_set
3532 vcap_select_min_rule_keyset(struct vcap_control *vctrl,
3533 enum vcap_type vtype,
3534 struct vcap_keyset_list *kslist)
3536 enum vcap_keyfield_set ret = VCAP_KFS_NO_VALUE;
3537 const struct vcap_set *kset;
3540 for (idx = 0; idx < kslist->cnt; ++idx) {
3541 kset = vcap_keyfieldset(vctrl, vtype, kslist->keysets[idx]);
3544 if (kset->sw_per_item >= max)
3546 max = kset->sw_per_item;
3547 ret = kslist->keysets[idx];
3551 EXPORT_SYMBOL_GPL(vcap_select_min_rule_keyset);
3553 /* Make a full copy of an existing rule with a new rule id */
3554 struct vcap_rule *vcap_copy_rule(struct vcap_rule *erule)
3556 struct vcap_rule_internal *ri = to_intrule(erule);
3557 struct vcap_client_actionfield *caf;
3558 struct vcap_client_keyfield *ckf;
3559 struct vcap_rule *rule;
3562 err = vcap_api_check(ri->vctrl);
3564 return ERR_PTR(err);
3566 rule = vcap_alloc_rule(ri->vctrl, ri->ndev, ri->data.vcap_chain_id,
3567 ri->data.user, ri->data.priority, 0);
3571 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
3572 /* Add a key duplicate in the new rule */
3573 err = vcap_rule_add_key(rule,
3581 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
3582 /* Add a action duplicate in the new rule */
3583 err = vcap_rule_add_action(rule,
3592 vcap_free_rule(rule);
3593 return ERR_PTR(err);
3595 EXPORT_SYMBOL_GPL(vcap_copy_rule);
3597 #ifdef CONFIG_VCAP_KUNIT_TEST
3598 #include "vcap_api_kunit.c"