1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Microchip VCAP API 3 * 4 * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries. 5 */ 6 7 #include <linux/types.h> 8 9 #include "vcap_api_private.h" 10 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), 20 }; 21 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), 31 }; 32 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 */ 38 }; 39 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 */ 47 }; 48 49 void vcap_iter_set(struct vcap_stream_iter *itr, int sw_width, 50 const struct vcap_typegroup *tg, u32 offset) 51 { 52 memset(itr, 0, sizeof(*itr)); 53 itr->offset = offset; 54 itr->sw_width = sw_width; 55 itr->regs_per_sw = DIV_ROUND_UP(sw_width, 32); 56 itr->tg = tg; 57 } 58 59 static void vcap_iter_skip_tg(struct vcap_stream_iter *itr) 60 { 61 /* Compensate the field offset for preceding typegroups. 62 * A typegroup table ends with an all-zero terminator. 63 */ 64 while (itr->tg->width && itr->offset >= itr->tg->offset) { 65 itr->offset += itr->tg->width; 66 itr->tg++; /* next typegroup */ 67 } 68 } 69 70 void vcap_iter_update(struct vcap_stream_iter *itr) 71 { 72 int sw_idx, sw_bitpos; 73 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; 80 } 81 82 void vcap_iter_init(struct vcap_stream_iter *itr, int sw_width, 83 const struct vcap_typegroup *tg, u32 offset) 84 { 85 vcap_iter_set(itr, sw_width, tg, offset); 86 vcap_iter_skip_tg(itr); 87 vcap_iter_update(itr); 88 } 89 90 void vcap_iter_next(struct vcap_stream_iter *itr) 91 { 92 itr->offset++; 93 vcap_iter_skip_tg(itr); 94 vcap_iter_update(itr); 95 } 96 97 static void vcap_set_bit(u32 *stream, struct vcap_stream_iter *itr, bool value) 98 { 99 u32 mask = BIT(itr->reg_bitpos); 100 u32 *p = &stream[itr->reg_idx]; 101 102 if (value) 103 *p |= mask; 104 else 105 *p &= ~mask; 106 } 107 108 static void vcap_encode_bit(u32 *stream, struct vcap_stream_iter *itr, bool val) 109 { 110 /* When intersected by a type group field, stream the type group bits 111 * before continuing with the value bit 112 */ 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; 117 118 vcap_set_bit(stream, itr, (itr->tg->value >> tg_bitpos) & 0x1); 119 itr->offset++; 120 vcap_iter_update(itr); 121 } 122 vcap_set_bit(stream, itr, val); 123 } 124 125 static void vcap_encode_field(u32 *stream, struct vcap_stream_iter *itr, 126 int width, const u8 *value) 127 { 128 int idx; 129 130 /* Loop over the field value bits and add the value bits one by one to 131 * the output stream. 132 */ 133 for (idx = 0; idx < width; idx++) { 134 u8 bidx = idx & GENMASK(2, 0); 135 136 /* Encode one field value bit */ 137 vcap_encode_bit(stream, itr, (value[idx / 8] >> bidx) & 0x1); 138 vcap_iter_next(itr); 139 } 140 } 141 142 static void vcap_encode_typegroups(u32 *stream, int sw_width, 143 const struct vcap_typegroup *tg, 144 bool mask) 145 { 146 struct vcap_stream_iter iter; 147 int idx; 148 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 152 */ 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 161 */ 162 if (mask) 163 vcap_set_bit(stream, &iter, 0x1); 164 else 165 vcap_set_bit(stream, &iter, 166 (iter.tg->value >> idx) & 0x1); 167 iter.offset++; 168 vcap_iter_update(&iter); 169 } 170 iter.tg++; /* next typegroup */ 171 } 172 } 173 174 static bool vcap_bitarray_zero(int width, u8 *value) 175 { 176 int bytes = DIV_ROUND_UP(width, BITS_PER_BYTE); 177 u8 total = 0, bmask = 0xff; 178 int rwidth = width; 179 int idx; 180 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; 185 } 186 return total == 0; 187 } 188 189 static bool vcap_get_bit(u32 *stream, struct vcap_stream_iter *itr) 190 { 191 u32 mask = BIT(itr->reg_bitpos); 192 u32 *p = &stream[itr->reg_idx]; 193 194 return !!(*p & mask); 195 } 196 197 static void vcap_decode_field(u32 *stream, struct vcap_stream_iter *itr, 198 int width, u8 *value) 199 { 200 int idx; 201 202 /* Loop over the field value bits and get the field bits and 203 * set them in the output value byte array 204 */ 205 for (idx = 0; idx < width; idx++) { 206 u8 bidx = idx & 0x7; 207 208 /* Decode one field value bit */ 209 if (vcap_get_bit(stream, itr)) 210 *value |= 1 << bidx; 211 vcap_iter_next(itr); 212 if (bidx == 7) 213 value++; 214 } 215 } 216 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, 219 enum vcap_type vt, 220 u32 *keystream, 221 u32 *mskstream, 222 enum vcap_keyfield_set keyset) 223 { 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; 230 u32 value = 0; 231 u32 mask = 0; 232 233 if (vcap_keyfield_count(vctrl, vt, keyset) == 0) 234 return false; 235 236 info = vcap_keyfieldset(vctrl, vt, keyset); 237 /* Check that the keyset is valid */ 238 if (!info) 239 return false; 240 241 /* a type_id of value -1 means that there is no type field */ 242 if (info->type_id == (u8)-1) 243 return true; 244 245 /* Get a valid typegroup for the specific keyset */ 246 tgt = vcap_keyfield_typegroup(vctrl, vt, keyset); 247 if (!tgt) 248 return false; 249 250 fields = vcap_keyfields(vctrl, vt, keyset); 251 if (!fields) 252 return false; 253 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)) 259 return false; 260 261 /* Get the value of the type field in the stream and compare to the 262 * one define in the vcap keyset 263 */ 264 vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset); 265 vcap_decode_field(keystream, &iter, typefld->width, (u8 *)&value); 266 267 return (value & mask) == (info->type_id & mask); 268 } 269 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, 273 int sw_max) 274 { 275 struct vcap_stream_iter iter; 276 int sw_cnt, idx; 277 278 vcap_iter_set(&iter, sw_width, tgt, 0); 279 sw_cnt = 0; 280 while (iter.tg->width) { 281 u32 value = 0; 282 u32 tg_value = iter.tg->value; 283 284 if (mask) 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)) 292 value |= 1 << idx; 293 iter.offset++; 294 vcap_iter_update(&iter); 295 } 296 if (value != tg_value) 297 return -EINVAL; 298 iter.tg++; /* next typegroup */ 299 sw_cnt++; 300 /* Stop checking more typegroups */ 301 if (sw_max && sw_cnt >= sw_max) 302 break; 303 } 304 return 0; 305 } 306 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) 311 { 312 const struct vcap_typegroup **tgt; 313 int sw_idx, res; 314 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--) { 318 if (!tgt[sw_idx]) 319 continue; 320 321 res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].sw_width, 322 tgt[sw_idx], mask, sw_max); 323 if (res == 0) 324 return sw_idx; 325 } 326 return -EINVAL; 327 } 328 329 /* Verify that the typegroup information, subword count, keyset and type id 330 * are in sync and correct, return the list of matching keysets 331 */ 332 int 333 vcap_find_keystream_keysets(struct vcap_control *vctrl, 334 enum vcap_type vt, 335 u32 *keystream, 336 u32 *mskstream, 337 bool mask, int sw_max, 338 struct vcap_keyset_list *kslist) 339 { 340 const struct vcap_set *keyfield_set; 341 int sw_count, idx; 342 343 sw_count = vcap_find_keystream_typegroup_sw(vctrl, vt, keystream, mask, 344 sw_max); 345 if (sw_count < 0) 346 return sw_count; 347 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) 351 continue; 352 353 if (vcap_verify_keystream_keyset(vctrl, vt, keystream, 354 mskstream, idx)) 355 vcap_keyset_list_add(kslist, idx); 356 } 357 if (kslist->cnt > 0) 358 return 0; 359 return -EINVAL; 360 } 361 EXPORT_SYMBOL_GPL(vcap_find_keystream_keysets); 362 363 /* Read key data from a VCAP address and discover if there are any rule keysets 364 * here 365 */ 366 int vcap_addr_keysets(struct vcap_control *vctrl, 367 struct net_device *ndev, 368 struct vcap_admin *admin, 369 int addr, 370 struct vcap_keyset_list *kslist) 371 { 372 enum vcap_type vt = admin->vtype; 373 int keyset_sw_regs, idx; 374 u32 key = 0, mask = 0; 375 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, 380 keyset_sw_regs); 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]; 385 } 386 if (key == 0 && mask == 0) 387 return -EINVAL; 388 /* Decode and locate the keysets */ 389 return vcap_find_keystream_keysets(vctrl, vt, admin->cache.keystream, 390 admin->cache.maskstream, false, 0, 391 kslist); 392 } 393 EXPORT_SYMBOL_GPL(vcap_addr_keysets); 394 395 /* Return the list of keyfields for the keyset */ 396 const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl, 397 enum vcap_type vt, 398 enum vcap_keyfield_set keyset) 399 { 400 /* Check that the keyset exists in the vcap keyset list */ 401 if (keyset >= vctrl->vcaps[vt].keyfield_set_size) 402 return NULL; 403 return vctrl->vcaps[vt].keyfield_set_map[keyset]; 404 } 405 406 /* Return the keyset information for the keyset */ 407 const struct vcap_set *vcap_keyfieldset(struct vcap_control *vctrl, 408 enum vcap_type vt, 409 enum vcap_keyfield_set keyset) 410 { 411 const struct vcap_set *kset; 412 413 /* Check that the keyset exists in the vcap keyset list */ 414 if (keyset >= vctrl->vcaps[vt].keyfield_set_size) 415 return NULL; 416 kset = &vctrl->vcaps[vt].keyfield_set[keyset]; 417 if (kset->sw_per_item == 0 || kset->sw_per_item > vctrl->vcaps[vt].sw_count) 418 return NULL; 419 return kset; 420 } 421 EXPORT_SYMBOL_GPL(vcap_keyfieldset); 422 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) 427 { 428 const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset); 429 430 /* Check that the keyset is valid */ 431 if (!kset) 432 return NULL; 433 return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item]; 434 } 435 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) 439 { 440 /* Check that the keyset exists in the vcap keyset list */ 441 if (keyset >= vctrl->vcaps[vt].keyfield_set_size) 442 return 0; 443 return vctrl->vcaps[vt].keyfield_set_map_size[keyset]; 444 } 445 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) 450 { 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; 455 456 /* Encode the fields for the key and the mask in their respective 457 * streams, respecting the subword width. 458 */ 459 switch (kf->ctrl.type) { 460 case VCAP_FIELD_BIT: 461 value = &kf->data.u1.value; 462 mask = &kf->data.u1.mask; 463 break; 464 case VCAP_FIELD_U32: 465 value = (const u8 *)&kf->data.u32.value; 466 mask = (const u8 *)&kf->data.u32.mask; 467 break; 468 case VCAP_FIELD_U48: 469 value = kf->data.u48.value; 470 mask = kf->data.u48.mask; 471 break; 472 case VCAP_FIELD_U56: 473 value = kf->data.u56.value; 474 mask = kf->data.u56.mask; 475 break; 476 case VCAP_FIELD_U64: 477 value = kf->data.u64.value; 478 mask = kf->data.u64.mask; 479 break; 480 case VCAP_FIELD_U72: 481 value = kf->data.u72.value; 482 mask = kf->data.u72.mask; 483 break; 484 case VCAP_FIELD_U112: 485 value = kf->data.u112.value; 486 mask = kf->data.u112.mask; 487 break; 488 case VCAP_FIELD_U128: 489 value = kf->data.u128.value; 490 mask = kf->data.u128.mask; 491 break; 492 } 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); 497 } 498 499 static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl, 500 struct vcap_rule_internal *ri, 501 const struct vcap_typegroup *tgt) 502 { 503 int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width; 504 struct vcap_cache_data *cache = &ri->admin->cache; 505 506 /* Encode the typegroup bits for the key and the mask in their streams, 507 * respecting the subword width. 508 */ 509 vcap_encode_typegroups(cache->keystream, sw_width, tgt, false); 510 vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true); 511 } 512 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. 516 */ 517 static void vcap_copy_to_w32be(u8 *dst, const u8 *src, int size) 518 { 519 for (int idx = 0; idx < size; ++idx) { 520 int first_byte_index = 0; 521 int nidx; 522 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]; 528 } 529 } 530 531 static void 532 vcap_copy_from_client_keyfield(struct vcap_rule *rule, 533 struct vcap_client_keyfield *dst, 534 const struct vcap_client_keyfield *src) 535 { 536 struct vcap_rule_internal *ri = to_intrule(rule); 537 const struct vcap_client_keyfield_data *sdata; 538 struct vcap_client_keyfield_data *ddata; 539 int size; 540 541 dst->ctrl.type = src->ctrl.type; 542 dst->ctrl.key = src->ctrl.key; 543 INIT_LIST_HEAD(&dst->ctrl.list); 544 sdata = &src->data; 545 ddata = &dst->data; 546 547 if (!ri->admin->w32be) { 548 memcpy(ddata, sdata, sizeof(dst->data)); 549 return; 550 } 551 552 size = keyfield_size_table[dst->ctrl.type] / 2; 553 554 switch (dst->ctrl.type) { 555 case VCAP_FIELD_BIT: 556 case VCAP_FIELD_U32: 557 memcpy(ddata, sdata, sizeof(dst->data)); 558 break; 559 case VCAP_FIELD_U48: 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); 562 break; 563 case VCAP_FIELD_U56: 564 vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size); 565 vcap_copy_to_w32be(ddata->u56.mask, sdata->u56.mask, size); 566 break; 567 case VCAP_FIELD_U64: 568 vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size); 569 vcap_copy_to_w32be(ddata->u64.mask, sdata->u64.mask, size); 570 break; 571 case VCAP_FIELD_U72: 572 vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size); 573 vcap_copy_to_w32be(ddata->u72.mask, sdata->u72.mask, size); 574 break; 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); 578 break; 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); 582 break; 583 } 584 } 585 586 static void 587 vcap_copy_from_client_actionfield(struct vcap_rule *rule, 588 struct vcap_client_actionfield *dst, 589 const struct vcap_client_actionfield *src) 590 { 591 struct vcap_rule_internal *ri = to_intrule(rule); 592 const struct vcap_client_actionfield_data *sdata; 593 struct vcap_client_actionfield_data *ddata; 594 int size; 595 596 dst->ctrl.type = src->ctrl.type; 597 dst->ctrl.action = src->ctrl.action; 598 INIT_LIST_HEAD(&dst->ctrl.list); 599 sdata = &src->data; 600 ddata = &dst->data; 601 602 if (!ri->admin->w32be) { 603 memcpy(ddata, sdata, sizeof(dst->data)); 604 return; 605 } 606 607 size = actionfield_size_table[dst->ctrl.type]; 608 609 switch (dst->ctrl.type) { 610 case VCAP_FIELD_BIT: 611 case VCAP_FIELD_U32: 612 memcpy(ddata, sdata, sizeof(dst->data)); 613 break; 614 case VCAP_FIELD_U48: 615 vcap_copy_to_w32be(ddata->u48.value, sdata->u48.value, size); 616 break; 617 case VCAP_FIELD_U56: 618 vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size); 619 break; 620 case VCAP_FIELD_U64: 621 vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size); 622 break; 623 case VCAP_FIELD_U72: 624 vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size); 625 break; 626 case VCAP_FIELD_U112: 627 vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size); 628 break; 629 case VCAP_FIELD_U128: 630 vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size); 631 break; 632 } 633 } 634 635 static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri) 636 { 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; 641 int keyset_size; 642 643 /* Get a valid set of fields for the specific keyset */ 644 kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset); 645 if (!kf_table) { 646 pr_err("%s:%d: no fields available for this keyset: %d\n", 647 __func__, __LINE__, ri->data.keyset); 648 return -EINVAL; 649 } 650 /* Get a valid typegroup for the specific keyset */ 651 tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype, 652 ri->data.keyset); 653 if (!tg_table) { 654 pr_err("%s:%d: no typegroups available for this keyset: %d\n", 655 __func__, __LINE__, ri->data.keyset); 656 return -EINVAL; 657 } 658 /* Get a valid size for the specific keyset */ 659 keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype, 660 ri->data.keyset); 661 if (keyset_size == 0) { 662 pr_err("%s:%d: zero field count for this keyset: %d\n", 663 __func__, __LINE__, ri->data.keyset); 664 return -EINVAL; 665 } 666 /* Iterate over the keyfields (key, mask) in the rule 667 * and encode these bits 668 */ 669 if (list_empty(&ri->data.keyfields)) { 670 pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__); 671 return -EINVAL; 672 } 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); 678 return -EINVAL; 679 } 680 vcap_copy_from_client_keyfield(&ri->data, &tempkf, ckf); 681 vcap_encode_keyfield(ri, &tempkf, &kf_table[ckf->ctrl.key], 682 tg_table); 683 } 684 /* Add typegroup bits to the key/mask bitstreams */ 685 vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table); 686 return 0; 687 } 688 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) 693 { 694 /* Check that the actionset exists in the vcap actionset list */ 695 if (actionset >= vctrl->vcaps[vt].actionfield_set_size) 696 return NULL; 697 return vctrl->vcaps[vt].actionfield_set_map[actionset]; 698 } 699 700 const struct vcap_set * 701 vcap_actionfieldset(struct vcap_control *vctrl, 702 enum vcap_type vt, enum vcap_actionfield_set actionset) 703 { 704 const struct vcap_set *aset; 705 706 /* Check that the actionset exists in the vcap actionset list */ 707 if (actionset >= vctrl->vcaps[vt].actionfield_set_size) 708 return NULL; 709 aset = &vctrl->vcaps[vt].actionfield_set[actionset]; 710 if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count) 711 return NULL; 712 return aset; 713 } 714 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) 719 { 720 const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset); 721 722 /* Check that the actionset is valid */ 723 if (!aset) 724 return NULL; 725 return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item]; 726 } 727 728 /* Return the number of actionfields in the actionset */ 729 int vcap_actionfield_count(struct vcap_control *vctrl, 730 enum vcap_type vt, 731 enum vcap_actionfield_set actionset) 732 { 733 /* Check that the actionset exists in the vcap actionset list */ 734 if (actionset >= vctrl->vcaps[vt].actionfield_set_size) 735 return 0; 736 return vctrl->vcaps[vt].actionfield_set_map_size[actionset]; 737 } 738 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) 743 { 744 int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width; 745 746 struct vcap_cache_data *cache = &ri->admin->cache; 747 struct vcap_stream_iter iter; 748 const u8 *value; 749 750 /* Encode the action field in the stream, respecting the subword width */ 751 switch (af->ctrl.type) { 752 case VCAP_FIELD_BIT: 753 value = &af->data.u1.value; 754 break; 755 case VCAP_FIELD_U32: 756 value = (const u8 *)&af->data.u32.value; 757 break; 758 case VCAP_FIELD_U48: 759 value = af->data.u48.value; 760 break; 761 case VCAP_FIELD_U56: 762 value = af->data.u56.value; 763 break; 764 case VCAP_FIELD_U64: 765 value = af->data.u64.value; 766 break; 767 case VCAP_FIELD_U72: 768 value = af->data.u72.value; 769 break; 770 case VCAP_FIELD_U112: 771 value = af->data.u112.value; 772 break; 773 case VCAP_FIELD_U128: 774 value = af->data.u128.value; 775 break; 776 } 777 vcap_iter_init(&iter, act_width, tgt, rf->offset); 778 vcap_encode_field(cache->actionstream, &iter, rf->width, value); 779 } 780 781 static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri, 782 const struct vcap_typegroup *tgt) 783 { 784 int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width; 785 struct vcap_cache_data *cache = &ri->admin->cache; 786 787 /* Encode the typegroup bits for the actionstream respecting the subword 788 * width. 789 */ 790 vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false); 791 } 792 793 static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri) 794 { 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; 799 int actionset_size; 800 801 /* Get a valid set of actionset fields for the specific actionset */ 802 af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype, 803 ri->data.actionset); 804 if (!af_table) { 805 pr_err("%s:%d: no fields available for this actionset: %d\n", 806 __func__, __LINE__, ri->data.actionset); 807 return -EINVAL; 808 } 809 /* Get a valid typegroup for the specific actionset */ 810 tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype, 811 ri->data.actionset); 812 if (!tg_table) { 813 pr_err("%s:%d: no typegroups available for this actionset: %d\n", 814 __func__, __LINE__, ri->data.actionset); 815 return -EINVAL; 816 } 817 /* Get a valid actionset size for the specific actionset */ 818 actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype, 819 ri->data.actionset); 820 if (actionset_size == 0) { 821 pr_err("%s:%d: zero field count for this actionset: %d\n", 822 __func__, __LINE__, ri->data.actionset); 823 return -EINVAL; 824 } 825 /* Iterate over the actionfields in the rule 826 * and encode these bits 827 */ 828 if (list_empty(&ri->data.actionfields)) 829 pr_warn("%s:%d: no actionfields in the rule\n", 830 __func__, __LINE__); 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); 836 return -EINVAL; 837 } 838 vcap_copy_from_client_actionfield(&ri->data, &tempaf, caf); 839 vcap_encode_actionfield(ri, &tempaf, 840 &af_table[caf->ctrl.action], tg_table); 841 } 842 /* Add typegroup bits to the entry bitstreams */ 843 vcap_encode_actionfield_typegroups(ri, tg_table); 844 return 0; 845 } 846 847 static int vcap_encode_rule(struct vcap_rule_internal *ri) 848 { 849 int err; 850 851 err = vcap_encode_rule_keyset(ri); 852 if (err) 853 return err; 854 err = vcap_encode_rule_actionset(ri); 855 if (err) 856 return err; 857 return 0; 858 } 859 860 int vcap_api_check(struct vcap_control *ctrl) 861 { 862 if (!ctrl) { 863 pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__); 864 return -EINVAL; 865 } 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", 872 __func__, __LINE__); 873 return -ENOENT; 874 } 875 return 0; 876 } 877 878 void vcap_erase_cache(struct vcap_rule_internal *ri) 879 { 880 ri->vctrl->ops->cache_erase(ri->admin); 881 } 882 883 /* Update the keyset for the rule */ 884 int vcap_set_rule_set_keyset(struct vcap_rule *rule, 885 enum vcap_keyfield_set keyset) 886 { 887 struct vcap_rule_internal *ri = to_intrule(rule); 888 const struct vcap_set *kset; 889 int sw_width; 890 891 kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset); 892 /* Check that the keyset is valid */ 893 if (!kset) 894 return -EINVAL; 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; 899 return 0; 900 } 901 EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset); 902 903 /* Update the actionset for the rule */ 904 int vcap_set_rule_set_actionset(struct vcap_rule *rule, 905 enum vcap_actionfield_set actionset) 906 { 907 struct vcap_rule_internal *ri = to_intrule(rule); 908 const struct vcap_set *aset; 909 int act_width; 910 911 aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset); 912 /* Check that the actionset is valid */ 913 if (!aset) 914 return -EINVAL; 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; 919 return 0; 920 } 921 EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset); 922 923 /* Check if a rule with this id exists */ 924 static bool vcap_rule_exists(struct vcap_control *vctrl, u32 id) 925 { 926 struct vcap_rule_internal *ri; 927 struct vcap_admin *admin; 928 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) 933 return true; 934 return false; 935 } 936 937 void vcap_lock(struct vcap_admin *admin) 938 { 939 mutex_lock(&admin->vctrl->lock); 940 } 941 942 void vcap_unlock(struct vcap_admin *admin) 943 { 944 mutex_unlock(&admin->vctrl->lock); 945 } 946 947 /* Find a rule with a provided rule id return a locked vcap */ 948 static struct vcap_rule_internal * 949 vcap_get_locked_rule(struct vcap_control *vctrl, u32 id) 950 { 951 struct vcap_rule_internal *ri; 952 struct vcap_admin *admin; 953 954 /* Look for the rule id in all vcaps */ 955 list_for_each_entry(admin, &vctrl->list, list) { 956 vcap_lock(admin); 957 list_for_each_entry(ri, &admin->rules, list) 958 if (ri->data.id == id) 959 return ri; 960 vcap_unlock(admin); 961 } 962 return NULL; 963 } 964 965 /* Find a rule id with a provided cookie */ 966 int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie) 967 { 968 struct vcap_rule_internal *ri; 969 struct vcap_admin *admin; 970 int id = 0; 971 972 /* Look for the rule id in all vcaps */ 973 list_for_each_entry(admin, &vctrl->list, list) { 974 vcap_lock(admin); 975 list_for_each_entry(ri, &admin->rules, list) { 976 if (ri->data.cookie == cookie) { 977 id = ri->data.id; 978 break; 979 } 980 } 981 vcap_unlock(admin); 982 if (id) 983 return id; 984 } 985 return -ENOENT; 986 } 987 EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie); 988 989 /* Get number of rules in a vcap instance lookup chain id range */ 990 int vcap_admin_rule_count(struct vcap_admin *admin, int cid) 991 { 992 int max_cid = roundup(cid + 1, VCAP_CID_LOOKUP_SIZE); 993 int min_cid = rounddown(cid, VCAP_CID_LOOKUP_SIZE); 994 struct vcap_rule_internal *elem; 995 int count = 0; 996 997 list_for_each_entry(elem, &admin->rules, list) { 998 vcap_lock(admin); 999 if (elem->data.vcap_chain_id >= min_cid && 1000 elem->data.vcap_chain_id < max_cid) 1001 ++count; 1002 vcap_unlock(admin); 1003 } 1004 return count; 1005 } 1006 EXPORT_SYMBOL_GPL(vcap_admin_rule_count); 1007 1008 /* Make a copy of the rule, shallow or full */ 1009 static struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri, 1010 bool full) 1011 { 1012 struct vcap_client_actionfield *caf, *newcaf; 1013 struct vcap_client_keyfield *ckf, *newckf; 1014 struct vcap_rule_internal *duprule; 1015 1016 /* Allocate the client part */ 1017 duprule = kzalloc_obj(*duprule); 1018 if (!duprule) 1019 return ERR_PTR(-ENOMEM); 1020 *duprule = *ri; 1021 /* Not inserted in the VCAP */ 1022 INIT_LIST_HEAD(&duprule->list); 1023 /* No elements in these lists */ 1024 INIT_LIST_HEAD(&duprule->data.keyfields); 1025 INIT_LIST_HEAD(&duprule->data.actionfields); 1026 1027 /* A full rule copy includes keys and actions */ 1028 if (!full) 1029 return duprule; 1030 1031 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) { 1032 newckf = kmemdup(ckf, sizeof(*newckf), GFP_KERNEL); 1033 if (!newckf) 1034 goto err; 1035 list_add_tail(&newckf->ctrl.list, &duprule->data.keyfields); 1036 } 1037 1038 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) { 1039 newcaf = kmemdup(caf, sizeof(*newcaf), GFP_KERNEL); 1040 if (!newcaf) 1041 goto err; 1042 list_add_tail(&newcaf->ctrl.list, &duprule->data.actionfields); 1043 } 1044 1045 return duprule; 1046 1047 err: 1048 list_for_each_entry_safe(ckf, newckf, &duprule->data.keyfields, ctrl.list) { 1049 list_del(&ckf->ctrl.list); 1050 kfree(ckf); 1051 } 1052 1053 list_for_each_entry_safe(caf, newcaf, &duprule->data.actionfields, ctrl.list) { 1054 list_del(&caf->ctrl.list); 1055 kfree(caf); 1056 } 1057 1058 kfree(duprule); 1059 return ERR_PTR(-ENOMEM); 1060 } 1061 1062 static void vcap_apply_width(u8 *dst, int width, int bytes) 1063 { 1064 u8 bmask; 1065 int idx; 1066 1067 for (idx = 0; idx < bytes; idx++) { 1068 if (width > 0) 1069 if (width < 8) 1070 bmask = (1 << width) - 1; 1071 else 1072 bmask = ~0; 1073 else 1074 bmask = 0; 1075 dst[idx] &= bmask; 1076 width -= 8; 1077 } 1078 } 1079 1080 static void vcap_copy_from_w32be(u8 *dst, u8 *src, int size, int width) 1081 { 1082 int idx, ridx, wstart, nidx; 1083 int tail_bytes = (((size + 4) >> 2) << 2) - size; 1084 1085 for (idx = 0, ridx = size - 1; idx < size; ++idx, --ridx) { 1086 wstart = (idx >> 2) << 2; 1087 nidx = wstart + 3 - (idx & 0x3); 1088 if (nidx >= size) 1089 nidx -= tail_bytes; 1090 dst[nidx] = src[ridx]; 1091 } 1092 1093 vcap_apply_width(dst, width, size); 1094 } 1095 1096 static void vcap_copy_action_bit_field(struct vcap_u1_action *field, u8 *value) 1097 { 1098 field->value = (*value) & 0x1; 1099 } 1100 1101 static void vcap_copy_limited_actionfield(u8 *dstvalue, u8 *srcvalue, 1102 int width, int bytes) 1103 { 1104 memcpy(dstvalue, srcvalue, bytes); 1105 vcap_apply_width(dstvalue, width, bytes); 1106 } 1107 1108 static void vcap_copy_to_client_actionfield(struct vcap_rule_internal *ri, 1109 struct vcap_client_actionfield *field, 1110 u8 *value, u16 width) 1111 { 1112 int field_size = actionfield_size_table[field->ctrl.type]; 1113 1114 if (ri->admin->w32be) { 1115 switch (field->ctrl.type) { 1116 case VCAP_FIELD_BIT: 1117 vcap_copy_action_bit_field(&field->data.u1, value); 1118 break; 1119 case VCAP_FIELD_U32: 1120 vcap_copy_limited_actionfield((u8 *)&field->data.u32.value, 1121 value, 1122 width, field_size); 1123 break; 1124 case VCAP_FIELD_U48: 1125 vcap_copy_from_w32be(field->data.u48.value, value, 1126 field_size, width); 1127 break; 1128 case VCAP_FIELD_U56: 1129 vcap_copy_from_w32be(field->data.u56.value, value, 1130 field_size, width); 1131 break; 1132 case VCAP_FIELD_U64: 1133 vcap_copy_from_w32be(field->data.u64.value, value, 1134 field_size, width); 1135 break; 1136 case VCAP_FIELD_U72: 1137 vcap_copy_from_w32be(field->data.u72.value, value, 1138 field_size, width); 1139 break; 1140 case VCAP_FIELD_U112: 1141 vcap_copy_from_w32be(field->data.u112.value, value, 1142 field_size, width); 1143 break; 1144 case VCAP_FIELD_U128: 1145 vcap_copy_from_w32be(field->data.u128.value, value, 1146 field_size, width); 1147 break; 1148 } 1149 } else { 1150 switch (field->ctrl.type) { 1151 case VCAP_FIELD_BIT: 1152 vcap_copy_action_bit_field(&field->data.u1, value); 1153 break; 1154 case VCAP_FIELD_U32: 1155 vcap_copy_limited_actionfield((u8 *)&field->data.u32.value, 1156 value, 1157 width, field_size); 1158 break; 1159 case VCAP_FIELD_U48: 1160 vcap_copy_limited_actionfield(field->data.u48.value, 1161 value, 1162 width, field_size); 1163 break; 1164 case VCAP_FIELD_U56: 1165 vcap_copy_limited_actionfield(field->data.u56.value, 1166 value, 1167 width, field_size); 1168 break; 1169 case VCAP_FIELD_U64: 1170 vcap_copy_limited_actionfield(field->data.u64.value, 1171 value, 1172 width, field_size); 1173 break; 1174 case VCAP_FIELD_U72: 1175 vcap_copy_limited_actionfield(field->data.u72.value, 1176 value, 1177 width, field_size); 1178 break; 1179 case VCAP_FIELD_U112: 1180 vcap_copy_limited_actionfield(field->data.u112.value, 1181 value, 1182 width, field_size); 1183 break; 1184 case VCAP_FIELD_U128: 1185 vcap_copy_limited_actionfield(field->data.u128.value, 1186 value, 1187 width, field_size); 1188 break; 1189 } 1190 } 1191 } 1192 1193 static void vcap_copy_key_bit_field(struct vcap_u1_key *field, 1194 u8 *value, u8 *mask) 1195 { 1196 field->value = (*value) & 0x1; 1197 field->mask = (*mask) & 0x1; 1198 } 1199 1200 static void vcap_copy_limited_keyfield(u8 *dstvalue, u8 *dstmask, 1201 u8 *srcvalue, u8 *srcmask, 1202 int width, int bytes) 1203 { 1204 memcpy(dstvalue, srcvalue, bytes); 1205 vcap_apply_width(dstvalue, width, bytes); 1206 memcpy(dstmask, srcmask, bytes); 1207 vcap_apply_width(dstmask, width, bytes); 1208 } 1209 1210 static void vcap_copy_to_client_keyfield(struct vcap_rule_internal *ri, 1211 struct vcap_client_keyfield *field, 1212 u8 *value, u8 *mask, u16 width) 1213 { 1214 int field_size = keyfield_size_table[field->ctrl.type] / 2; 1215 1216 if (ri->admin->w32be) { 1217 switch (field->ctrl.type) { 1218 case VCAP_FIELD_BIT: 1219 vcap_copy_key_bit_field(&field->data.u1, value, mask); 1220 break; 1221 case VCAP_FIELD_U32: 1222 vcap_copy_limited_keyfield((u8 *)&field->data.u32.value, 1223 (u8 *)&field->data.u32.mask, 1224 value, mask, 1225 width, field_size); 1226 break; 1227 case VCAP_FIELD_U48: 1228 vcap_copy_from_w32be(field->data.u48.value, value, 1229 field_size, width); 1230 vcap_copy_from_w32be(field->data.u48.mask, mask, 1231 field_size, width); 1232 break; 1233 case VCAP_FIELD_U56: 1234 vcap_copy_from_w32be(field->data.u56.value, value, 1235 field_size, width); 1236 vcap_copy_from_w32be(field->data.u56.mask, mask, 1237 field_size, width); 1238 break; 1239 case VCAP_FIELD_U64: 1240 vcap_copy_from_w32be(field->data.u64.value, value, 1241 field_size, width); 1242 vcap_copy_from_w32be(field->data.u64.mask, mask, 1243 field_size, width); 1244 break; 1245 case VCAP_FIELD_U72: 1246 vcap_copy_from_w32be(field->data.u72.value, value, 1247 field_size, width); 1248 vcap_copy_from_w32be(field->data.u72.mask, mask, 1249 field_size, width); 1250 break; 1251 case VCAP_FIELD_U112: 1252 vcap_copy_from_w32be(field->data.u112.value, value, 1253 field_size, width); 1254 vcap_copy_from_w32be(field->data.u112.mask, mask, 1255 field_size, width); 1256 break; 1257 case VCAP_FIELD_U128: 1258 vcap_copy_from_w32be(field->data.u128.value, value, 1259 field_size, width); 1260 vcap_copy_from_w32be(field->data.u128.mask, mask, 1261 field_size, width); 1262 break; 1263 } 1264 } else { 1265 switch (field->ctrl.type) { 1266 case VCAP_FIELD_BIT: 1267 vcap_copy_key_bit_field(&field->data.u1, value, mask); 1268 break; 1269 case VCAP_FIELD_U32: 1270 vcap_copy_limited_keyfield((u8 *)&field->data.u32.value, 1271 (u8 *)&field->data.u32.mask, 1272 value, mask, 1273 width, field_size); 1274 break; 1275 case VCAP_FIELD_U48: 1276 vcap_copy_limited_keyfield(field->data.u48.value, 1277 field->data.u48.mask, 1278 value, mask, 1279 width, field_size); 1280 break; 1281 case VCAP_FIELD_U56: 1282 vcap_copy_limited_keyfield(field->data.u56.value, 1283 field->data.u56.mask, 1284 value, mask, 1285 width, field_size); 1286 break; 1287 case VCAP_FIELD_U64: 1288 vcap_copy_limited_keyfield(field->data.u64.value, 1289 field->data.u64.mask, 1290 value, mask, 1291 width, field_size); 1292 break; 1293 case VCAP_FIELD_U72: 1294 vcap_copy_limited_keyfield(field->data.u72.value, 1295 field->data.u72.mask, 1296 value, mask, 1297 width, field_size); 1298 break; 1299 case VCAP_FIELD_U112: 1300 vcap_copy_limited_keyfield(field->data.u112.value, 1301 field->data.u112.mask, 1302 value, mask, 1303 width, field_size); 1304 break; 1305 case VCAP_FIELD_U128: 1306 vcap_copy_limited_keyfield(field->data.u128.value, 1307 field->data.u128.mask, 1308 value, mask, 1309 width, field_size); 1310 break; 1311 } 1312 } 1313 } 1314 1315 static void vcap_rule_alloc_keyfield(struct vcap_rule_internal *ri, 1316 const struct vcap_field *keyfield, 1317 enum vcap_key_field key, 1318 u8 *value, u8 *mask) 1319 { 1320 struct vcap_client_keyfield *field; 1321 1322 field = kzalloc_obj(*field); 1323 if (!field) 1324 return; 1325 INIT_LIST_HEAD(&field->ctrl.list); 1326 field->ctrl.key = key; 1327 field->ctrl.type = keyfield->type; 1328 vcap_copy_to_client_keyfield(ri, field, value, mask, keyfield->width); 1329 list_add_tail(&field->ctrl.list, &ri->data.keyfields); 1330 } 1331 1332 /* Read key data from a VCAP address and discover if there is a rule keyset 1333 * here 1334 */ 1335 static bool 1336 vcap_verify_actionstream_actionset(struct vcap_control *vctrl, 1337 enum vcap_type vt, 1338 u32 *actionstream, 1339 enum vcap_actionfield_set actionset) 1340 { 1341 const struct vcap_typegroup *tgt; 1342 const struct vcap_field *fields; 1343 const struct vcap_set *info; 1344 1345 if (vcap_actionfield_count(vctrl, vt, actionset) == 0) 1346 return false; 1347 1348 info = vcap_actionfieldset(vctrl, vt, actionset); 1349 /* Check that the actionset is valid */ 1350 if (!info) 1351 return false; 1352 1353 /* a type_id of value -1 means that there is no type field */ 1354 if (info->type_id == (u8)-1) 1355 return true; 1356 1357 /* Get a valid typegroup for the specific actionset */ 1358 tgt = vcap_actionfield_typegroup(vctrl, vt, actionset); 1359 if (!tgt) 1360 return false; 1361 1362 fields = vcap_actionfields(vctrl, vt, actionset); 1363 if (!fields) 1364 return false; 1365 1366 /* Later this will be expanded with a check of the type id */ 1367 return true; 1368 } 1369 1370 /* Find the subword width of the action typegroup that matches the stream data 1371 */ 1372 static int vcap_find_actionstream_typegroup_sw(struct vcap_control *vctrl, 1373 enum vcap_type vt, u32 *stream, 1374 int sw_max) 1375 { 1376 const struct vcap_typegroup **tgt; 1377 int sw_idx, res; 1378 1379 tgt = vctrl->vcaps[vt].actionfield_set_typegroups; 1380 /* Try the longest subword match first */ 1381 for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) { 1382 if (!tgt[sw_idx]) 1383 continue; 1384 res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].act_width, 1385 tgt[sw_idx], false, sw_max); 1386 if (res == 0) 1387 return sw_idx; 1388 } 1389 return -EINVAL; 1390 } 1391 1392 /* Verify that the typegroup information, subword count, actionset and type id 1393 * are in sync and correct, return the actionset 1394 */ 1395 static enum vcap_actionfield_set 1396 vcap_find_actionstream_actionset(struct vcap_control *vctrl, 1397 enum vcap_type vt, 1398 u32 *stream, 1399 int sw_max) 1400 { 1401 const struct vcap_set *actionfield_set; 1402 int sw_count, idx; 1403 bool res; 1404 1405 sw_count = vcap_find_actionstream_typegroup_sw(vctrl, vt, stream, 1406 sw_max); 1407 if (sw_count < 0) 1408 return sw_count; 1409 1410 actionfield_set = vctrl->vcaps[vt].actionfield_set; 1411 for (idx = 0; idx < vctrl->vcaps[vt].actionfield_set_size; ++idx) { 1412 if (actionfield_set[idx].sw_per_item != sw_count) 1413 continue; 1414 1415 res = vcap_verify_actionstream_actionset(vctrl, vt, 1416 stream, idx); 1417 if (res) 1418 return idx; 1419 } 1420 return -EINVAL; 1421 } 1422 1423 /* Store action value in an element in a list for the client */ 1424 static void vcap_rule_alloc_actionfield(struct vcap_rule_internal *ri, 1425 const struct vcap_field *actionfield, 1426 enum vcap_action_field action, 1427 u8 *value) 1428 { 1429 struct vcap_client_actionfield *field; 1430 1431 field = kzalloc_obj(*field); 1432 if (!field) 1433 return; 1434 INIT_LIST_HEAD(&field->ctrl.list); 1435 field->ctrl.action = action; 1436 field->ctrl.type = actionfield->type; 1437 vcap_copy_to_client_actionfield(ri, field, value, actionfield->width); 1438 list_add_tail(&field->ctrl.list, &ri->data.actionfields); 1439 } 1440 1441 static int vcap_decode_actionset(struct vcap_rule_internal *ri) 1442 { 1443 struct vcap_control *vctrl = ri->vctrl; 1444 struct vcap_admin *admin = ri->admin; 1445 const struct vcap_field *actionfield; 1446 enum vcap_actionfield_set actionset; 1447 enum vcap_type vt = admin->vtype; 1448 const struct vcap_typegroup *tgt; 1449 struct vcap_stream_iter iter; 1450 int idx, res, actfield_count; 1451 u32 *actstream; 1452 u8 value[16]; 1453 1454 actstream = admin->cache.actionstream; 1455 res = vcap_find_actionstream_actionset(vctrl, vt, actstream, 0); 1456 if (res < 0) { 1457 pr_err("%s:%d: could not find valid actionset: %d\n", 1458 __func__, __LINE__, res); 1459 return -EINVAL; 1460 } 1461 actionset = res; 1462 actfield_count = vcap_actionfield_count(vctrl, vt, actionset); 1463 actionfield = vcap_actionfields(vctrl, vt, actionset); 1464 tgt = vcap_actionfield_typegroup(vctrl, vt, actionset); 1465 /* Start decoding the stream */ 1466 for (idx = 0; idx < actfield_count; ++idx) { 1467 if (actionfield[idx].width <= 0) 1468 continue; 1469 /* Get the action */ 1470 memset(value, 0, DIV_ROUND_UP(actionfield[idx].width, 8)); 1471 vcap_iter_init(&iter, vctrl->vcaps[vt].act_width, tgt, 1472 actionfield[idx].offset); 1473 vcap_decode_field(actstream, &iter, actionfield[idx].width, 1474 value); 1475 /* Skip if no bits are set */ 1476 if (vcap_bitarray_zero(actionfield[idx].width, value)) 1477 continue; 1478 vcap_rule_alloc_actionfield(ri, &actionfield[idx], idx, value); 1479 /* Later the action id will also be checked */ 1480 } 1481 return vcap_set_rule_set_actionset((struct vcap_rule *)ri, actionset); 1482 } 1483 1484 static int vcap_decode_keyset(struct vcap_rule_internal *ri) 1485 { 1486 struct vcap_control *vctrl = ri->vctrl; 1487 struct vcap_stream_iter kiter, miter; 1488 struct vcap_admin *admin = ri->admin; 1489 enum vcap_keyfield_set keysets[10]; 1490 const struct vcap_field *keyfield; 1491 enum vcap_type vt = admin->vtype; 1492 const struct vcap_typegroup *tgt; 1493 struct vcap_keyset_list matches; 1494 enum vcap_keyfield_set keyset; 1495 int idx, res, keyfield_count; 1496 u32 *maskstream; 1497 u32 *keystream; 1498 u8 value[16]; 1499 u8 mask[16]; 1500 1501 keystream = admin->cache.keystream; 1502 maskstream = admin->cache.maskstream; 1503 matches.keysets = keysets; 1504 matches.cnt = 0; 1505 matches.max = ARRAY_SIZE(keysets); 1506 res = vcap_find_keystream_keysets(vctrl, vt, keystream, maskstream, 1507 false, 0, &matches); 1508 if (res < 0) { 1509 pr_err("%s:%d: could not find valid keysets: %d\n", 1510 __func__, __LINE__, res); 1511 return -EINVAL; 1512 } 1513 keyset = matches.keysets[0]; 1514 keyfield_count = vcap_keyfield_count(vctrl, vt, keyset); 1515 keyfield = vcap_keyfields(vctrl, vt, keyset); 1516 tgt = vcap_keyfield_typegroup(vctrl, vt, keyset); 1517 /* Start decoding the streams */ 1518 for (idx = 0; idx < keyfield_count; ++idx) { 1519 if (keyfield[idx].width <= 0) 1520 continue; 1521 /* First get the mask */ 1522 memset(mask, 0, DIV_ROUND_UP(keyfield[idx].width, 8)); 1523 vcap_iter_init(&miter, vctrl->vcaps[vt].sw_width, tgt, 1524 keyfield[idx].offset); 1525 vcap_decode_field(maskstream, &miter, keyfield[idx].width, 1526 mask); 1527 /* Skip if no mask bits are set */ 1528 if (vcap_bitarray_zero(keyfield[idx].width, mask)) 1529 continue; 1530 /* Get the key */ 1531 memset(value, 0, DIV_ROUND_UP(keyfield[idx].width, 8)); 1532 vcap_iter_init(&kiter, vctrl->vcaps[vt].sw_width, tgt, 1533 keyfield[idx].offset); 1534 vcap_decode_field(keystream, &kiter, keyfield[idx].width, 1535 value); 1536 vcap_rule_alloc_keyfield(ri, &keyfield[idx], idx, value, mask); 1537 } 1538 return vcap_set_rule_set_keyset((struct vcap_rule *)ri, keyset); 1539 } 1540 1541 /* Read VCAP content into the VCAP cache */ 1542 static int vcap_read_rule(struct vcap_rule_internal *ri) 1543 { 1544 struct vcap_admin *admin = ri->admin; 1545 int sw_idx, ent_idx = 0, act_idx = 0; 1546 u32 addr = ri->addr; 1547 1548 if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) { 1549 pr_err("%s:%d: rule is empty\n", __func__, __LINE__); 1550 return -EINVAL; 1551 } 1552 vcap_erase_cache(ri); 1553 /* Use the values in the streams to read the VCAP cache */ 1554 for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) { 1555 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, 1556 VCAP_SEL_ALL, addr); 1557 ri->vctrl->ops->cache_read(ri->ndev, admin, 1558 VCAP_SEL_ENTRY, ent_idx, 1559 ri->keyset_sw_regs); 1560 ri->vctrl->ops->cache_read(ri->ndev, admin, 1561 VCAP_SEL_ACTION, act_idx, 1562 ri->actionset_sw_regs); 1563 if (sw_idx == 0) 1564 ri->vctrl->ops->cache_read(ri->ndev, admin, 1565 VCAP_SEL_COUNTER, 1566 ri->counter_id, 0); 1567 ent_idx += ri->keyset_sw_regs; 1568 act_idx += ri->actionset_sw_regs; 1569 } 1570 return 0; 1571 } 1572 1573 /* Write VCAP cache content to the VCAP HW instance */ 1574 static int vcap_write_rule(struct vcap_rule_internal *ri) 1575 { 1576 struct vcap_admin *admin = ri->admin; 1577 int sw_idx, ent_idx = 0, act_idx = 0; 1578 u32 addr = ri->addr; 1579 1580 if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) { 1581 pr_err("%s:%d: rule is empty\n", __func__, __LINE__); 1582 return -EINVAL; 1583 } 1584 /* Use the values in the streams to write the VCAP cache */ 1585 for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) { 1586 ri->vctrl->ops->cache_write(ri->ndev, admin, 1587 VCAP_SEL_ENTRY, ent_idx, 1588 ri->keyset_sw_regs); 1589 ri->vctrl->ops->cache_write(ri->ndev, admin, 1590 VCAP_SEL_ACTION, act_idx, 1591 ri->actionset_sw_regs); 1592 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE, 1593 VCAP_SEL_ALL, addr); 1594 ent_idx += ri->keyset_sw_regs; 1595 act_idx += ri->actionset_sw_regs; 1596 } 1597 return 0; 1598 } 1599 1600 static int vcap_write_counter(struct vcap_rule_internal *ri, 1601 struct vcap_counter *ctr) 1602 { 1603 struct vcap_admin *admin = ri->admin; 1604 1605 admin->cache.counter = ctr->value; 1606 admin->cache.sticky = ctr->sticky; 1607 ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER, 1608 ri->counter_id, 0); 1609 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE, 1610 VCAP_SEL_COUNTER, ri->addr); 1611 return 0; 1612 } 1613 1614 /* Convert a chain id to a VCAP lookup index */ 1615 int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid) 1616 { 1617 int lookup_first = admin->vinst * admin->lookups_per_instance; 1618 int lookup_last = lookup_first + admin->lookups_per_instance; 1619 int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE; 1620 int cid = admin->first_cid; 1621 int lookup; 1622 1623 for (lookup = lookup_first; lookup < lookup_last; ++lookup, 1624 cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE) 1625 if (cur_cid >= cid && cur_cid < cid_next) 1626 return lookup; 1627 return 0; 1628 } 1629 EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup); 1630 1631 /* Lookup a vcap instance using chain id */ 1632 struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid) 1633 { 1634 struct vcap_admin *admin; 1635 1636 if (vcap_api_check(vctrl)) 1637 return NULL; 1638 1639 list_for_each_entry(admin, &vctrl->list, list) { 1640 if (cid >= admin->first_cid && cid <= admin->last_cid) 1641 return admin; 1642 } 1643 return NULL; 1644 } 1645 EXPORT_SYMBOL_GPL(vcap_find_admin); 1646 1647 /* Is this the last admin instance ordered by chain id and direction */ 1648 static bool vcap_admin_is_last(struct vcap_control *vctrl, 1649 struct vcap_admin *admin, 1650 bool ingress) 1651 { 1652 struct vcap_admin *iter, *last = NULL; 1653 int max_cid = 0; 1654 1655 list_for_each_entry(iter, &vctrl->list, list) { 1656 if (iter->first_cid > max_cid && 1657 iter->ingress == ingress) { 1658 last = iter; 1659 max_cid = iter->first_cid; 1660 } 1661 } 1662 if (!last) 1663 return false; 1664 1665 return admin == last; 1666 } 1667 1668 /* Calculate the value used for chaining VCAP rules */ 1669 int vcap_chain_offset(struct vcap_control *vctrl, int from_cid, int to_cid) 1670 { 1671 int diff = to_cid - from_cid; 1672 1673 if (diff < 0) /* Wrong direction */ 1674 return diff; 1675 to_cid %= VCAP_CID_LOOKUP_SIZE; 1676 if (to_cid == 0) /* Destination aligned to a lookup == no chaining */ 1677 return 0; 1678 diff %= VCAP_CID_LOOKUP_SIZE; /* Limit to a value within a lookup */ 1679 return diff; 1680 } 1681 EXPORT_SYMBOL_GPL(vcap_chain_offset); 1682 1683 /* Is the next chain id in one of the following lookups 1684 * For now this does not support filters linked to other filters using 1685 * keys and actions. That will be added later. 1686 */ 1687 bool vcap_is_next_lookup(struct vcap_control *vctrl, int src_cid, int dst_cid) 1688 { 1689 struct vcap_admin *admin; 1690 int next_cid; 1691 1692 if (vcap_api_check(vctrl)) 1693 return false; 1694 1695 /* The offset must be at least one lookup so round up one chain */ 1696 next_cid = roundup(src_cid + 1, VCAP_CID_LOOKUP_SIZE); 1697 1698 if (dst_cid < next_cid) 1699 return false; 1700 1701 admin = vcap_find_admin(vctrl, dst_cid); 1702 if (!admin) 1703 return false; 1704 1705 return true; 1706 } 1707 EXPORT_SYMBOL_GPL(vcap_is_next_lookup); 1708 1709 /* Check if there is room for a new rule */ 1710 static int vcap_rule_space(struct vcap_admin *admin, int size) 1711 { 1712 if (admin->last_used_addr - size < admin->first_valid_addr) { 1713 pr_err("%s:%d: No room for rule size: %u, %u\n", 1714 __func__, __LINE__, size, admin->first_valid_addr); 1715 return -ENOSPC; 1716 } 1717 return 0; 1718 } 1719 1720 /* Add the keyset typefield to the list of rule keyfields */ 1721 static int vcap_add_type_keyfield(struct vcap_rule *rule) 1722 { 1723 struct vcap_rule_internal *ri = to_intrule(rule); 1724 enum vcap_keyfield_set keyset = rule->keyset; 1725 enum vcap_type vt = ri->admin->vtype; 1726 const struct vcap_field *fields; 1727 const struct vcap_set *kset; 1728 int ret = -EINVAL; 1729 1730 kset = vcap_keyfieldset(ri->vctrl, vt, keyset); 1731 if (!kset) 1732 return ret; 1733 if (kset->type_id == (u8)-1) /* No type field is needed */ 1734 return 0; 1735 1736 fields = vcap_keyfields(ri->vctrl, vt, keyset); 1737 if (!fields) 1738 return -EINVAL; 1739 if (fields[VCAP_KF_TYPE].width > 1) { 1740 ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE, 1741 kset->type_id, 0xff); 1742 } else { 1743 if (kset->type_id) 1744 ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE, 1745 VCAP_BIT_1); 1746 else 1747 ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE, 1748 VCAP_BIT_0); 1749 } 1750 return 0; 1751 } 1752 1753 /* Add the actionset typefield to the list of rule actionfields */ 1754 static int vcap_add_type_actionfield(struct vcap_rule *rule) 1755 { 1756 enum vcap_actionfield_set actionset = rule->actionset; 1757 struct vcap_rule_internal *ri = to_intrule(rule); 1758 enum vcap_type vt = ri->admin->vtype; 1759 const struct vcap_field *fields; 1760 const struct vcap_set *aset; 1761 int ret = -EINVAL; 1762 1763 aset = vcap_actionfieldset(ri->vctrl, vt, actionset); 1764 if (!aset) 1765 return ret; 1766 if (aset->type_id == (u8)-1) /* No type field is needed */ 1767 return 0; 1768 1769 fields = vcap_actionfields(ri->vctrl, vt, actionset); 1770 if (!fields) 1771 return -EINVAL; 1772 if (fields[VCAP_AF_TYPE].width > 1) { 1773 ret = vcap_rule_add_action_u32(rule, VCAP_AF_TYPE, 1774 aset->type_id); 1775 } else { 1776 if (aset->type_id) 1777 ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE, 1778 VCAP_BIT_1); 1779 else 1780 ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE, 1781 VCAP_BIT_0); 1782 } 1783 return ret; 1784 } 1785 1786 /* Add a keyset to a keyset list */ 1787 bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist, 1788 enum vcap_keyfield_set keyset) 1789 { 1790 int idx; 1791 1792 if (keysetlist->cnt < keysetlist->max) { 1793 /* Avoid duplicates */ 1794 for (idx = 0; idx < keysetlist->cnt; ++idx) 1795 if (keysetlist->keysets[idx] == keyset) 1796 return keysetlist->cnt < keysetlist->max; 1797 keysetlist->keysets[keysetlist->cnt++] = keyset; 1798 } 1799 return keysetlist->cnt < keysetlist->max; 1800 } 1801 EXPORT_SYMBOL_GPL(vcap_keyset_list_add); 1802 1803 /* Add a actionset to a actionset list */ 1804 static bool vcap_actionset_list_add(struct vcap_actionset_list *actionsetlist, 1805 enum vcap_actionfield_set actionset) 1806 { 1807 int idx; 1808 1809 if (actionsetlist->cnt < actionsetlist->max) { 1810 /* Avoid duplicates */ 1811 for (idx = 0; idx < actionsetlist->cnt; ++idx) 1812 if (actionsetlist->actionsets[idx] == actionset) 1813 return actionsetlist->cnt < actionsetlist->max; 1814 actionsetlist->actionsets[actionsetlist->cnt++] = actionset; 1815 } 1816 return actionsetlist->cnt < actionsetlist->max; 1817 } 1818 1819 /* map keyset id to a string with the keyset name */ 1820 const char *vcap_keyset_name(struct vcap_control *vctrl, 1821 enum vcap_keyfield_set keyset) 1822 { 1823 return vctrl->stats->keyfield_set_names[keyset]; 1824 } 1825 EXPORT_SYMBOL_GPL(vcap_keyset_name); 1826 1827 /* map key field id to a string with the key name */ 1828 const char *vcap_keyfield_name(struct vcap_control *vctrl, 1829 enum vcap_key_field key) 1830 { 1831 return vctrl->stats->keyfield_names[key]; 1832 } 1833 EXPORT_SYMBOL_GPL(vcap_keyfield_name); 1834 1835 /* map actionset id to a string with the actionset name */ 1836 const char *vcap_actionset_name(struct vcap_control *vctrl, 1837 enum vcap_actionfield_set actionset) 1838 { 1839 return vctrl->stats->actionfield_set_names[actionset]; 1840 } 1841 1842 /* map action field id to a string with the action name */ 1843 const char *vcap_actionfield_name(struct vcap_control *vctrl, 1844 enum vcap_action_field action) 1845 { 1846 return vctrl->stats->actionfield_names[action]; 1847 } 1848 1849 /* Return the keyfield that matches a key in a keyset */ 1850 static const struct vcap_field * 1851 vcap_find_keyset_keyfield(struct vcap_control *vctrl, 1852 enum vcap_type vtype, 1853 enum vcap_keyfield_set keyset, 1854 enum vcap_key_field key) 1855 { 1856 const struct vcap_field *fields; 1857 int idx, count; 1858 1859 fields = vcap_keyfields(vctrl, vtype, keyset); 1860 if (!fields) 1861 return NULL; 1862 1863 /* Iterate the keyfields of the keyset */ 1864 count = vcap_keyfield_count(vctrl, vtype, keyset); 1865 for (idx = 0; idx < count; ++idx) { 1866 if (fields[idx].width == 0) 1867 continue; 1868 1869 if (key == idx) 1870 return &fields[idx]; 1871 } 1872 1873 return NULL; 1874 } 1875 1876 /* Match a list of keys against the keysets available in a vcap type */ 1877 static bool _vcap_rule_find_keysets(struct vcap_rule_internal *ri, 1878 struct vcap_keyset_list *matches) 1879 { 1880 const struct vcap_client_keyfield *ckf; 1881 int keyset, found, keycount, map_size; 1882 const struct vcap_field **map; 1883 enum vcap_type vtype; 1884 1885 vtype = ri->admin->vtype; 1886 map = ri->vctrl->vcaps[vtype].keyfield_set_map; 1887 map_size = ri->vctrl->vcaps[vtype].keyfield_set_size; 1888 1889 /* Get a count of the keyfields we want to match */ 1890 keycount = 0; 1891 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) 1892 ++keycount; 1893 1894 matches->cnt = 0; 1895 /* Iterate the keysets of the VCAP */ 1896 for (keyset = 0; keyset < map_size; ++keyset) { 1897 if (!map[keyset]) 1898 continue; 1899 1900 /* Iterate the keys in the rule */ 1901 found = 0; 1902 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) 1903 if (vcap_find_keyset_keyfield(ri->vctrl, vtype, 1904 keyset, ckf->ctrl.key)) 1905 ++found; 1906 1907 /* Save the keyset if all keyfields were found */ 1908 if (found == keycount) 1909 if (!vcap_keyset_list_add(matches, keyset)) 1910 /* bail out when the quota is filled */ 1911 break; 1912 } 1913 1914 return matches->cnt > 0; 1915 } 1916 1917 /* Match a list of keys against the keysets available in a vcap type */ 1918 bool vcap_rule_find_keysets(struct vcap_rule *rule, 1919 struct vcap_keyset_list *matches) 1920 { 1921 struct vcap_rule_internal *ri = to_intrule(rule); 1922 1923 return _vcap_rule_find_keysets(ri, matches); 1924 } 1925 EXPORT_SYMBOL_GPL(vcap_rule_find_keysets); 1926 1927 /* Return the actionfield that matches a action in a actionset */ 1928 static const struct vcap_field * 1929 vcap_find_actionset_actionfield(struct vcap_control *vctrl, 1930 enum vcap_type vtype, 1931 enum vcap_actionfield_set actionset, 1932 enum vcap_action_field action) 1933 { 1934 const struct vcap_field *fields; 1935 int idx, count; 1936 1937 fields = vcap_actionfields(vctrl, vtype, actionset); 1938 if (!fields) 1939 return NULL; 1940 1941 /* Iterate the actionfields of the actionset */ 1942 count = vcap_actionfield_count(vctrl, vtype, actionset); 1943 for (idx = 0; idx < count; ++idx) { 1944 if (fields[idx].width == 0) 1945 continue; 1946 1947 if (action == idx) 1948 return &fields[idx]; 1949 } 1950 1951 return NULL; 1952 } 1953 1954 /* Match a list of actions against the actionsets available in a vcap type */ 1955 static bool vcap_rule_find_actionsets(struct vcap_rule_internal *ri, 1956 struct vcap_actionset_list *matches) 1957 { 1958 int actionset, found, actioncount, map_size; 1959 const struct vcap_client_actionfield *ckf; 1960 const struct vcap_field **map; 1961 enum vcap_type vtype; 1962 1963 vtype = ri->admin->vtype; 1964 map = ri->vctrl->vcaps[vtype].actionfield_set_map; 1965 map_size = ri->vctrl->vcaps[vtype].actionfield_set_size; 1966 1967 /* Get a count of the actionfields we want to match */ 1968 actioncount = 0; 1969 list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list) 1970 ++actioncount; 1971 1972 matches->cnt = 0; 1973 /* Iterate the actionsets of the VCAP */ 1974 for (actionset = 0; actionset < map_size; ++actionset) { 1975 if (!map[actionset]) 1976 continue; 1977 1978 /* Iterate the actions in the rule */ 1979 found = 0; 1980 list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list) 1981 if (vcap_find_actionset_actionfield(ri->vctrl, vtype, 1982 actionset, 1983 ckf->ctrl.action)) 1984 ++found; 1985 1986 /* Save the actionset if all actionfields were found */ 1987 if (found == actioncount) 1988 if (!vcap_actionset_list_add(matches, actionset)) 1989 /* bail out when the quota is filled */ 1990 break; 1991 } 1992 1993 return matches->cnt > 0; 1994 } 1995 1996 /* Validate a rule with respect to available port keys */ 1997 int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto) 1998 { 1999 struct vcap_rule_internal *ri = to_intrule(rule); 2000 struct vcap_keyset_list matches = {}; 2001 enum vcap_keyfield_set keysets[10]; 2002 int ret; 2003 2004 ret = vcap_api_check(ri->vctrl); 2005 if (ret) 2006 return ret; 2007 if (!ri->admin) { 2008 ri->data.exterr = VCAP_ERR_NO_ADMIN; 2009 return -EINVAL; 2010 } 2011 if (!ri->ndev) { 2012 ri->data.exterr = VCAP_ERR_NO_NETDEV; 2013 return -EINVAL; 2014 } 2015 2016 matches.keysets = keysets; 2017 matches.max = ARRAY_SIZE(keysets); 2018 if (ri->data.keyset == VCAP_KFS_NO_VALUE) { 2019 /* Iterate over rule keyfields and select keysets that fits */ 2020 if (!_vcap_rule_find_keysets(ri, &matches)) { 2021 ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH; 2022 return -EINVAL; 2023 } 2024 } else { 2025 /* prepare for keyset validation */ 2026 keysets[0] = ri->data.keyset; 2027 matches.cnt = 1; 2028 } 2029 2030 /* Pick a keyset that is supported in the port lookups */ 2031 ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule, 2032 &matches, l3_proto); 2033 if (ret < 0) { 2034 pr_err("%s:%d: keyset validation failed: %d\n", 2035 __func__, __LINE__, ret); 2036 ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH; 2037 return ret; 2038 } 2039 /* use the keyset that is supported in the port lookups */ 2040 ret = vcap_set_rule_set_keyset(rule, ret); 2041 if (ret < 0) { 2042 pr_err("%s:%d: keyset was not updated: %d\n", 2043 __func__, __LINE__, ret); 2044 return ret; 2045 } 2046 if (ri->data.actionset == VCAP_AFS_NO_VALUE) { 2047 struct vcap_actionset_list matches = {}; 2048 enum vcap_actionfield_set actionsets[10]; 2049 2050 matches.actionsets = actionsets; 2051 matches.max = ARRAY_SIZE(actionsets); 2052 2053 /* Find an actionset that fits the rule actions */ 2054 if (!vcap_rule_find_actionsets(ri, &matches)) { 2055 ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH; 2056 return -EINVAL; 2057 } 2058 ret = vcap_set_rule_set_actionset(rule, actionsets[0]); 2059 if (ret < 0) { 2060 pr_err("%s:%d: actionset was not updated: %d\n", 2061 __func__, __LINE__, ret); 2062 return ret; 2063 } 2064 } 2065 vcap_add_type_keyfield(rule); 2066 vcap_add_type_actionfield(rule); 2067 /* Add default fields to this rule */ 2068 ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule); 2069 2070 /* Rule size is the maximum of the entry and action subword count */ 2071 ri->size = max(ri->keyset_sw, ri->actionset_sw); 2072 2073 /* Finally check if there is room for the rule in the VCAP */ 2074 return vcap_rule_space(ri->admin, ri->size); 2075 } 2076 EXPORT_SYMBOL_GPL(vcap_val_rule); 2077 2078 /* Entries are sorted with increasing values of sort_key. 2079 * I.e. Lowest numerical sort_key is first in list. 2080 * In order to locate largest keys first in list we negate the key size with 2081 * (max_size - size). 2082 */ 2083 static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio) 2084 { 2085 return ((max_size - size) << 24) | (user << 16) | prio; 2086 } 2087 2088 /* calculate the address of the next rule after this (lower address and prio) */ 2089 static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri) 2090 { 2091 return ((addr - ri->size) / ri->size) * ri->size; 2092 } 2093 2094 /* Assign a unique rule id and autogenerate one if id == 0 */ 2095 static u32 vcap_set_rule_id(struct vcap_rule_internal *ri) 2096 { 2097 if (ri->data.id != 0) 2098 return ri->data.id; 2099 2100 for (u32 next_id = 1; next_id < ~0; ++next_id) { 2101 if (!vcap_rule_exists(ri->vctrl, next_id)) { 2102 ri->data.id = next_id; 2103 break; 2104 } 2105 } 2106 return ri->data.id; 2107 } 2108 2109 static int vcap_insert_rule(struct vcap_rule_internal *ri, 2110 struct vcap_rule_move *move) 2111 { 2112 int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count; 2113 struct vcap_rule_internal *duprule, *iter, *elem = NULL; 2114 struct vcap_admin *admin = ri->admin; 2115 u32 addr; 2116 2117 ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user, 2118 ri->data.priority); 2119 2120 /* Insert the new rule in the list of rule based on the sort key 2121 * If the rule needs to be inserted between existing rules then move 2122 * these rules to make room for the new rule and update their start 2123 * address. 2124 */ 2125 list_for_each_entry(iter, &admin->rules, list) { 2126 if (ri->sort_key < iter->sort_key) { 2127 elem = iter; 2128 break; 2129 } 2130 } 2131 2132 if (!elem) { 2133 ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri); 2134 admin->last_used_addr = ri->addr; 2135 2136 /* Add a copy of the rule to the VCAP list */ 2137 duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED); 2138 if (IS_ERR(duprule)) 2139 return PTR_ERR(duprule); 2140 2141 list_add_tail(&duprule->list, &admin->rules); 2142 return 0; 2143 } 2144 2145 /* Reuse the space of the current rule */ 2146 addr = elem->addr + elem->size; 2147 ri->addr = vcap_next_rule_addr(addr, ri); 2148 addr = ri->addr; 2149 2150 /* Add a copy of the rule to the VCAP list */ 2151 duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED); 2152 if (IS_ERR(duprule)) 2153 return PTR_ERR(duprule); 2154 2155 /* Add before the current entry */ 2156 list_add_tail(&duprule->list, &elem->list); 2157 2158 /* Update the current rule */ 2159 elem->addr = vcap_next_rule_addr(addr, elem); 2160 addr = elem->addr; 2161 2162 /* Update the address in the remaining rules in the list */ 2163 list_for_each_entry_continue(elem, &admin->rules, list) { 2164 elem->addr = vcap_next_rule_addr(addr, elem); 2165 addr = elem->addr; 2166 } 2167 2168 /* Update the move info */ 2169 move->addr = admin->last_used_addr; 2170 move->count = ri->addr - addr; 2171 move->offset = admin->last_used_addr - addr; 2172 admin->last_used_addr = addr; 2173 return 0; 2174 } 2175 2176 static void vcap_move_rules(struct vcap_rule_internal *ri, 2177 struct vcap_rule_move *move) 2178 { 2179 ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr, 2180 move->offset, move->count); 2181 } 2182 2183 /* Check if the chain is already used to enable a VCAP lookup for this port */ 2184 static bool vcap_is_chain_used(struct vcap_control *vctrl, 2185 struct net_device *ndev, int src_cid) 2186 { 2187 struct vcap_enabled_port *eport; 2188 struct vcap_admin *admin; 2189 2190 list_for_each_entry(admin, &vctrl->list, list) 2191 list_for_each_entry(eport, &admin->enabled, list) 2192 if (eport->src_cid == src_cid && eport->ndev == ndev) 2193 return true; 2194 2195 return false; 2196 } 2197 2198 /* Fetch the next chain in the enabled list for the port */ 2199 static int vcap_get_next_chain(struct vcap_control *vctrl, 2200 struct net_device *ndev, 2201 int dst_cid) 2202 { 2203 struct vcap_enabled_port *eport; 2204 struct vcap_admin *admin; 2205 2206 list_for_each_entry(admin, &vctrl->list, list) { 2207 list_for_each_entry(eport, &admin->enabled, list) { 2208 if (eport->ndev != ndev) 2209 continue; 2210 if (eport->src_cid == dst_cid) 2211 return eport->dst_cid; 2212 } 2213 } 2214 2215 return 0; 2216 } 2217 2218 static bool vcap_path_exist(struct vcap_control *vctrl, struct net_device *ndev, 2219 int dst_cid) 2220 { 2221 int cid = rounddown(dst_cid, VCAP_CID_LOOKUP_SIZE); 2222 struct vcap_enabled_port *eport = NULL; 2223 struct vcap_enabled_port *elem; 2224 struct vcap_admin *admin; 2225 int tmp; 2226 2227 if (cid == 0) /* Chain zero is always available */ 2228 return true; 2229 2230 /* Find first entry that starts from chain 0*/ 2231 list_for_each_entry(admin, &vctrl->list, list) { 2232 list_for_each_entry(elem, &admin->enabled, list) { 2233 if (elem->src_cid == 0 && elem->ndev == ndev) { 2234 eport = elem; 2235 break; 2236 } 2237 } 2238 if (eport) 2239 break; 2240 } 2241 2242 if (!eport) 2243 return false; 2244 2245 tmp = eport->dst_cid; 2246 while (tmp != cid && tmp != 0) 2247 tmp = vcap_get_next_chain(vctrl, ndev, tmp); 2248 2249 return !!tmp; 2250 } 2251 2252 /* Internal clients can always store their rules in HW 2253 * External clients can store their rules if the chain is enabled all 2254 * the way from chain 0, otherwise the rule will be cached until 2255 * the chain is enabled. 2256 */ 2257 static void vcap_rule_set_state(struct vcap_rule_internal *ri) 2258 { 2259 if (ri->data.user <= VCAP_USER_QOS) 2260 ri->state = VCAP_RS_PERMANENT; 2261 else if (vcap_path_exist(ri->vctrl, ri->ndev, ri->data.vcap_chain_id)) 2262 ri->state = VCAP_RS_ENABLED; 2263 else 2264 ri->state = VCAP_RS_DISABLED; 2265 } 2266 2267 /* Encode and write a validated rule to the VCAP */ 2268 int vcap_add_rule(struct vcap_rule *rule) 2269 { 2270 struct vcap_rule_internal *ri = to_intrule(rule); 2271 struct vcap_rule_move move = {0}; 2272 struct vcap_counter ctr = {0}; 2273 int ret; 2274 2275 ret = vcap_api_check(ri->vctrl); 2276 if (ret) 2277 return ret; 2278 /* Insert the new rule in the list of vcap rules */ 2279 vcap_lock(ri->admin); 2280 2281 vcap_rule_set_state(ri); 2282 ret = vcap_insert_rule(ri, &move); 2283 if (ret < 0) { 2284 pr_err("%s:%d: could not insert rule in vcap list: %d\n", 2285 __func__, __LINE__, ret); 2286 goto out; 2287 } 2288 if (move.count > 0) 2289 vcap_move_rules(ri, &move); 2290 2291 /* Set the counter to zero */ 2292 ret = vcap_write_counter(ri, &ctr); 2293 if (ret) 2294 goto out; 2295 2296 if (ri->state == VCAP_RS_DISABLED) { 2297 /* Erase the rule area */ 2298 ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size); 2299 goto out; 2300 } 2301 2302 vcap_erase_cache(ri); 2303 ret = vcap_encode_rule(ri); 2304 if (ret) { 2305 pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret); 2306 goto out; 2307 } 2308 2309 ret = vcap_write_rule(ri); 2310 if (ret) { 2311 pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret); 2312 goto out; 2313 } 2314 out: 2315 vcap_unlock(ri->admin); 2316 return ret; 2317 } 2318 EXPORT_SYMBOL_GPL(vcap_add_rule); 2319 2320 /* Allocate a new rule with the provided arguments */ 2321 struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl, 2322 struct net_device *ndev, int vcap_chain_id, 2323 enum vcap_user user, u16 priority, 2324 u32 id) 2325 { 2326 struct vcap_rule_internal *ri; 2327 struct vcap_admin *admin; 2328 int err, maxsize; 2329 2330 err = vcap_api_check(vctrl); 2331 if (err) 2332 return ERR_PTR(err); 2333 if (!ndev) 2334 return ERR_PTR(-ENODEV); 2335 /* Get the VCAP instance */ 2336 admin = vcap_find_admin(vctrl, vcap_chain_id); 2337 if (!admin) 2338 return ERR_PTR(-ENOENT); 2339 /* Sanity check that this VCAP is supported on this platform */ 2340 if (vctrl->vcaps[admin->vtype].rows == 0) 2341 return ERR_PTR(-EINVAL); 2342 2343 vcap_lock(admin); 2344 /* Check if a rule with this id already exists */ 2345 if (vcap_rule_exists(vctrl, id)) { 2346 err = -EINVAL; 2347 goto out_unlock; 2348 } 2349 2350 /* Check if there is room for the rule in the block(s) of the VCAP */ 2351 maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */ 2352 if (vcap_rule_space(admin, maxsize)) { 2353 err = -ENOSPC; 2354 goto out_unlock; 2355 } 2356 2357 /* Create a container for the rule and return it */ 2358 ri = kzalloc_obj(*ri); 2359 if (!ri) { 2360 err = -ENOMEM; 2361 goto out_unlock; 2362 } 2363 2364 ri->data.vcap_chain_id = vcap_chain_id; 2365 ri->data.user = user; 2366 ri->data.priority = priority; 2367 ri->data.id = id; 2368 ri->data.keyset = VCAP_KFS_NO_VALUE; 2369 ri->data.actionset = VCAP_AFS_NO_VALUE; 2370 INIT_LIST_HEAD(&ri->list); 2371 INIT_LIST_HEAD(&ri->data.keyfields); 2372 INIT_LIST_HEAD(&ri->data.actionfields); 2373 ri->ndev = ndev; 2374 ri->admin = admin; /* refer to the vcap instance */ 2375 ri->vctrl = vctrl; /* refer to the client */ 2376 2377 if (vcap_set_rule_id(ri) == 0) { 2378 err = -EINVAL; 2379 goto out_free; 2380 } 2381 2382 vcap_unlock(admin); 2383 return (struct vcap_rule *)ri; 2384 2385 out_free: 2386 kfree(ri); 2387 out_unlock: 2388 vcap_unlock(admin); 2389 return ERR_PTR(err); 2390 2391 } 2392 EXPORT_SYMBOL_GPL(vcap_alloc_rule); 2393 2394 /* Free mem of a rule owned by client after the rule as been added to the VCAP */ 2395 void vcap_free_rule(struct vcap_rule *rule) 2396 { 2397 struct vcap_rule_internal *ri = to_intrule(rule); 2398 struct vcap_client_actionfield *caf, *next_caf; 2399 struct vcap_client_keyfield *ckf, *next_ckf; 2400 2401 /* Deallocate the list of keys and actions */ 2402 list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) { 2403 list_del(&ckf->ctrl.list); 2404 kfree(ckf); 2405 } 2406 list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) { 2407 list_del(&caf->ctrl.list); 2408 kfree(caf); 2409 } 2410 /* Deallocate the rule */ 2411 kfree(rule); 2412 } 2413 EXPORT_SYMBOL_GPL(vcap_free_rule); 2414 2415 /* Decode a rule from the VCAP cache and return a copy */ 2416 struct vcap_rule *vcap_decode_rule(struct vcap_rule_internal *elem) 2417 { 2418 struct vcap_rule_internal *ri; 2419 int err; 2420 2421 ri = vcap_dup_rule(elem, elem->state == VCAP_RS_DISABLED); 2422 if (IS_ERR(ri)) 2423 return ERR_CAST(ri); 2424 2425 if (ri->state == VCAP_RS_DISABLED) 2426 goto out; 2427 2428 err = vcap_read_rule(ri); 2429 if (err) 2430 return ERR_PTR(err); 2431 2432 err = vcap_decode_keyset(ri); 2433 if (err) 2434 return ERR_PTR(err); 2435 2436 err = vcap_decode_actionset(ri); 2437 if (err) 2438 return ERR_PTR(err); 2439 2440 out: 2441 return &ri->data; 2442 } 2443 2444 struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id) 2445 { 2446 struct vcap_rule_internal *elem; 2447 struct vcap_rule *rule; 2448 int err; 2449 2450 err = vcap_api_check(vctrl); 2451 if (err) 2452 return ERR_PTR(err); 2453 2454 elem = vcap_get_locked_rule(vctrl, id); 2455 if (!elem) 2456 return ERR_PTR(-ENOENT); 2457 2458 rule = vcap_decode_rule(elem); 2459 vcap_unlock(elem->admin); 2460 return rule; 2461 } 2462 EXPORT_SYMBOL_GPL(vcap_get_rule); 2463 2464 /* Update existing rule */ 2465 int vcap_mod_rule(struct vcap_rule *rule) 2466 { 2467 struct vcap_rule_internal *ri = to_intrule(rule); 2468 struct vcap_counter ctr; 2469 int err; 2470 2471 err = vcap_api_check(ri->vctrl); 2472 if (err) 2473 return err; 2474 2475 if (!vcap_get_locked_rule(ri->vctrl, ri->data.id)) 2476 return -ENOENT; 2477 2478 vcap_rule_set_state(ri); 2479 if (ri->state == VCAP_RS_DISABLED) 2480 goto out; 2481 2482 /* Encode the bitstreams to the VCAP cache */ 2483 vcap_erase_cache(ri); 2484 err = vcap_encode_rule(ri); 2485 if (err) 2486 goto out; 2487 2488 err = vcap_write_rule(ri); 2489 if (err) 2490 goto out; 2491 2492 memset(&ctr, 0, sizeof(ctr)); 2493 err = vcap_write_counter(ri, &ctr); 2494 2495 out: 2496 vcap_unlock(ri->admin); 2497 return err; 2498 } 2499 EXPORT_SYMBOL_GPL(vcap_mod_rule); 2500 2501 /* Return the alignment offset for a new rule address */ 2502 static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset) 2503 { 2504 return (el->addr + offset) % el->size; 2505 } 2506 2507 /* Update the rule address with an offset */ 2508 static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset) 2509 { 2510 el->addr += offset; 2511 } 2512 2513 /* Rules needs to be moved to fill the gap of the deleted rule */ 2514 static int vcap_fill_rule_gap(struct vcap_rule_internal *ri) 2515 { 2516 struct vcap_admin *admin = ri->admin; 2517 struct vcap_rule_internal *elem; 2518 struct vcap_rule_move move; 2519 int gap = 0, offset = 0; 2520 2521 /* If the first rule is deleted: Move other rules to the top */ 2522 if (list_is_first(&ri->list, &admin->rules)) 2523 offset = admin->last_valid_addr + 1 - ri->addr - ri->size; 2524 2525 /* Locate gaps between odd size rules and adjust the move */ 2526 elem = ri; 2527 list_for_each_entry_continue(elem, &admin->rules, list) 2528 gap += vcap_valid_rule_move(elem, ri->size); 2529 2530 /* Update the address in the remaining rules in the list */ 2531 elem = ri; 2532 list_for_each_entry_continue(elem, &admin->rules, list) 2533 vcap_adjust_rule_addr(elem, ri->size + gap + offset); 2534 2535 /* Update the move info */ 2536 move.addr = admin->last_used_addr; 2537 move.count = ri->addr - admin->last_used_addr - gap; 2538 move.offset = -(ri->size + gap + offset); 2539 2540 /* Do the actual move operation */ 2541 vcap_move_rules(ri, &move); 2542 2543 return gap + offset; 2544 } 2545 2546 /* Delete rule in a VCAP instance */ 2547 int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id) 2548 { 2549 struct vcap_rule_internal *ri, *elem; 2550 struct vcap_admin *admin; 2551 int gap = 0, err; 2552 2553 /* This will later also handle rule moving */ 2554 if (!ndev) 2555 return -ENODEV; 2556 err = vcap_api_check(vctrl); 2557 if (err) 2558 return err; 2559 /* Look for the rule id in all vcaps */ 2560 ri = vcap_get_locked_rule(vctrl, id); 2561 if (!ri) 2562 return -ENOENT; 2563 2564 admin = ri->admin; 2565 2566 if (ri->addr > admin->last_used_addr) 2567 gap = vcap_fill_rule_gap(ri); 2568 2569 /* Delete the rule from the list of rules and the cache */ 2570 list_del(&ri->list); 2571 vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap); 2572 vcap_free_rule(&ri->data); 2573 2574 /* Update the last used address, set to default when no rules */ 2575 if (list_empty(&admin->rules)) { 2576 admin->last_used_addr = admin->last_valid_addr + 1; 2577 } else { 2578 elem = list_last_entry(&admin->rules, struct vcap_rule_internal, 2579 list); 2580 admin->last_used_addr = elem->addr; 2581 } 2582 2583 vcap_unlock(admin); 2584 return err; 2585 } 2586 EXPORT_SYMBOL_GPL(vcap_del_rule); 2587 2588 /* Delete all rules in the VCAP instance */ 2589 int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin) 2590 { 2591 struct vcap_enabled_port *eport, *next_eport; 2592 struct vcap_rule_internal *ri, *next_ri; 2593 int ret = vcap_api_check(vctrl); 2594 2595 if (ret) 2596 return ret; 2597 2598 vcap_lock(admin); 2599 list_for_each_entry_safe(ri, next_ri, &admin->rules, list) { 2600 vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size); 2601 list_del(&ri->list); 2602 vcap_free_rule(&ri->data); 2603 } 2604 admin->last_used_addr = admin->last_valid_addr; 2605 2606 /* Remove list of enabled ports */ 2607 list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) { 2608 list_del(&eport->list); 2609 kfree(eport); 2610 } 2611 vcap_unlock(admin); 2612 2613 return 0; 2614 } 2615 EXPORT_SYMBOL_GPL(vcap_del_rules); 2616 2617 /* Find a client key field in a rule */ 2618 static struct vcap_client_keyfield * 2619 vcap_find_keyfield(struct vcap_rule *rule, enum vcap_key_field key) 2620 { 2621 struct vcap_rule_internal *ri = to_intrule(rule); 2622 struct vcap_client_keyfield *ckf; 2623 2624 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) 2625 if (ckf->ctrl.key == key) 2626 return ckf; 2627 return NULL; 2628 } 2629 2630 /* Find information on a key field in a rule */ 2631 const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule, 2632 enum vcap_key_field key) 2633 { 2634 struct vcap_rule_internal *ri = to_intrule(rule); 2635 enum vcap_keyfield_set keyset = rule->keyset; 2636 enum vcap_type vt = ri->admin->vtype; 2637 const struct vcap_field *fields; 2638 2639 if (keyset == VCAP_KFS_NO_VALUE) 2640 return NULL; 2641 fields = vcap_keyfields(ri->vctrl, vt, keyset); 2642 if (!fields) 2643 return NULL; 2644 return &fields[key]; 2645 } 2646 EXPORT_SYMBOL_GPL(vcap_lookup_keyfield); 2647 2648 /* Check if the keyfield is already in the rule */ 2649 static bool vcap_keyfield_unique(struct vcap_rule *rule, 2650 enum vcap_key_field key) 2651 { 2652 struct vcap_rule_internal *ri = to_intrule(rule); 2653 const struct vcap_client_keyfield *ckf; 2654 2655 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) 2656 if (ckf->ctrl.key == key) 2657 return false; 2658 return true; 2659 } 2660 2661 /* Check if the keyfield is in the keyset */ 2662 static bool vcap_keyfield_match_keyset(struct vcap_rule *rule, 2663 enum vcap_key_field key) 2664 { 2665 struct vcap_rule_internal *ri = to_intrule(rule); 2666 enum vcap_keyfield_set keyset = rule->keyset; 2667 enum vcap_type vt = ri->admin->vtype; 2668 const struct vcap_field *fields; 2669 2670 /* the field is accepted if the rule has no keyset yet */ 2671 if (keyset == VCAP_KFS_NO_VALUE) 2672 return true; 2673 fields = vcap_keyfields(ri->vctrl, vt, keyset); 2674 if (!fields) 2675 return false; 2676 /* if there is a width there is a way */ 2677 return fields[key].width > 0; 2678 } 2679 2680 static int vcap_rule_add_key(struct vcap_rule *rule, 2681 enum vcap_key_field key, 2682 enum vcap_field_type ftype, 2683 struct vcap_client_keyfield_data *data) 2684 { 2685 struct vcap_rule_internal *ri = to_intrule(rule); 2686 struct vcap_client_keyfield *field; 2687 2688 if (!vcap_keyfield_unique(rule, key)) { 2689 pr_warn("%s:%d: keyfield %s is already in the rule\n", 2690 __func__, __LINE__, 2691 vcap_keyfield_name(ri->vctrl, key)); 2692 return -EINVAL; 2693 } 2694 2695 if (!vcap_keyfield_match_keyset(rule, key)) { 2696 pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n", 2697 __func__, __LINE__, 2698 vcap_keyfield_name(ri->vctrl, key)); 2699 return -EINVAL; 2700 } 2701 2702 field = kzalloc_obj(*field); 2703 if (!field) 2704 return -ENOMEM; 2705 memcpy(&field->data, data, sizeof(field->data)); 2706 field->ctrl.key = key; 2707 field->ctrl.type = ftype; 2708 list_add_tail(&field->ctrl.list, &rule->keyfields); 2709 return 0; 2710 } 2711 2712 static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val) 2713 { 2714 switch (val) { 2715 case VCAP_BIT_0: 2716 u1->value = 0; 2717 u1->mask = 1; 2718 break; 2719 case VCAP_BIT_1: 2720 u1->value = 1; 2721 u1->mask = 1; 2722 break; 2723 case VCAP_BIT_ANY: 2724 u1->value = 0; 2725 u1->mask = 0; 2726 break; 2727 } 2728 } 2729 2730 /* Add a bit key with value and mask to the rule */ 2731 int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key, 2732 enum vcap_bit val) 2733 { 2734 struct vcap_client_keyfield_data data; 2735 2736 vcap_rule_set_key_bitsize(&data.u1, val); 2737 return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data); 2738 } 2739 EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit); 2740 2741 /* Add a 32 bit key field with value and mask to the rule */ 2742 int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key, 2743 u32 value, u32 mask) 2744 { 2745 struct vcap_client_keyfield_data data; 2746 2747 data.u32.value = value; 2748 data.u32.mask = mask; 2749 return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data); 2750 } 2751 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32); 2752 2753 /* Add a 48 bit key with value and mask to the rule */ 2754 int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key, 2755 struct vcap_u48_key *fieldval) 2756 { 2757 struct vcap_client_keyfield_data data; 2758 2759 memcpy(&data.u48, fieldval, sizeof(data.u48)); 2760 return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data); 2761 } 2762 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48); 2763 2764 /* Add a 72 bit key with value and mask to the rule */ 2765 int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key, 2766 struct vcap_u72_key *fieldval) 2767 { 2768 struct vcap_client_keyfield_data data; 2769 2770 memcpy(&data.u72, fieldval, sizeof(data.u72)); 2771 return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data); 2772 } 2773 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72); 2774 2775 /* Add a 128 bit key with value and mask to the rule */ 2776 int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key, 2777 struct vcap_u128_key *fieldval) 2778 { 2779 struct vcap_client_keyfield_data data; 2780 2781 memcpy(&data.u128, fieldval, sizeof(data.u128)); 2782 return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data); 2783 } 2784 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128); 2785 2786 int vcap_rule_get_key_u32(struct vcap_rule *rule, enum vcap_key_field key, 2787 u32 *value, u32 *mask) 2788 { 2789 struct vcap_client_keyfield *ckf; 2790 2791 ckf = vcap_find_keyfield(rule, key); 2792 if (!ckf) 2793 return -ENOENT; 2794 2795 *value = ckf->data.u32.value; 2796 *mask = ckf->data.u32.mask; 2797 2798 return 0; 2799 } 2800 EXPORT_SYMBOL_GPL(vcap_rule_get_key_u32); 2801 2802 /* Find a client action field in a rule */ 2803 struct vcap_client_actionfield * 2804 vcap_find_actionfield(struct vcap_rule *rule, enum vcap_action_field act) 2805 { 2806 struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule; 2807 struct vcap_client_actionfield *caf; 2808 2809 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) 2810 if (caf->ctrl.action == act) 2811 return caf; 2812 return NULL; 2813 } 2814 EXPORT_SYMBOL_GPL(vcap_find_actionfield); 2815 2816 /* Check if the actionfield is already in the rule */ 2817 static bool vcap_actionfield_unique(struct vcap_rule *rule, 2818 enum vcap_action_field act) 2819 { 2820 struct vcap_rule_internal *ri = to_intrule(rule); 2821 const struct vcap_client_actionfield *caf; 2822 2823 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) 2824 if (caf->ctrl.action == act) 2825 return false; 2826 return true; 2827 } 2828 2829 /* Check if the actionfield is in the actionset */ 2830 static bool vcap_actionfield_match_actionset(struct vcap_rule *rule, 2831 enum vcap_action_field action) 2832 { 2833 enum vcap_actionfield_set actionset = rule->actionset; 2834 struct vcap_rule_internal *ri = to_intrule(rule); 2835 enum vcap_type vt = ri->admin->vtype; 2836 const struct vcap_field *fields; 2837 2838 /* the field is accepted if the rule has no actionset yet */ 2839 if (actionset == VCAP_AFS_NO_VALUE) 2840 return true; 2841 fields = vcap_actionfields(ri->vctrl, vt, actionset); 2842 if (!fields) 2843 return false; 2844 /* if there is a width there is a way */ 2845 return fields[action].width > 0; 2846 } 2847 2848 static int vcap_rule_add_action(struct vcap_rule *rule, 2849 enum vcap_action_field action, 2850 enum vcap_field_type ftype, 2851 struct vcap_client_actionfield_data *data) 2852 { 2853 struct vcap_rule_internal *ri = to_intrule(rule); 2854 struct vcap_client_actionfield *field; 2855 2856 if (!vcap_actionfield_unique(rule, action)) { 2857 pr_warn("%s:%d: actionfield %s is already in the rule\n", 2858 __func__, __LINE__, 2859 vcap_actionfield_name(ri->vctrl, action)); 2860 return -EINVAL; 2861 } 2862 2863 if (!vcap_actionfield_match_actionset(rule, action)) { 2864 pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n", 2865 __func__, __LINE__, 2866 vcap_actionfield_name(ri->vctrl, action)); 2867 return -EINVAL; 2868 } 2869 2870 field = kzalloc_obj(*field); 2871 if (!field) 2872 return -ENOMEM; 2873 memcpy(&field->data, data, sizeof(field->data)); 2874 field->ctrl.action = action; 2875 field->ctrl.type = ftype; 2876 list_add_tail(&field->ctrl.list, &rule->actionfields); 2877 return 0; 2878 } 2879 2880 static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1, 2881 enum vcap_bit val) 2882 { 2883 switch (val) { 2884 case VCAP_BIT_0: 2885 u1->value = 0; 2886 break; 2887 case VCAP_BIT_1: 2888 u1->value = 1; 2889 break; 2890 case VCAP_BIT_ANY: 2891 u1->value = 0; 2892 break; 2893 } 2894 } 2895 2896 /* Add a bit action with value to the rule */ 2897 int vcap_rule_add_action_bit(struct vcap_rule *rule, 2898 enum vcap_action_field action, 2899 enum vcap_bit val) 2900 { 2901 struct vcap_client_actionfield_data data; 2902 2903 vcap_rule_set_action_bitsize(&data.u1, val); 2904 return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data); 2905 } 2906 EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit); 2907 2908 /* Add a 32 bit action field with value to the rule */ 2909 int vcap_rule_add_action_u32(struct vcap_rule *rule, 2910 enum vcap_action_field action, 2911 u32 value) 2912 { 2913 struct vcap_client_actionfield_data data; 2914 2915 data.u32.value = value; 2916 return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data); 2917 } 2918 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32); 2919 2920 /* Add a 72 bit action field with value to the rule */ 2921 int vcap_rule_add_action_u72(struct vcap_rule *rule, 2922 enum vcap_action_field action, 2923 struct vcap_u72_action *fieldval) 2924 { 2925 struct vcap_client_actionfield_data data; 2926 2927 memcpy(&data.u72, fieldval, sizeof(data.u72)); 2928 return vcap_rule_add_action(rule, action, VCAP_FIELD_U72, &data); 2929 } 2930 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u72); 2931 2932 static int vcap_read_counter(struct vcap_rule_internal *ri, 2933 struct vcap_counter *ctr) 2934 { 2935 struct vcap_admin *admin = ri->admin; 2936 2937 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER, 2938 ri->addr); 2939 ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER, 2940 ri->counter_id, 0); 2941 ctr->value = admin->cache.counter; 2942 ctr->sticky = admin->cache.sticky; 2943 return 0; 2944 } 2945 2946 /* Copy to host byte order */ 2947 void vcap_netbytes_copy(u8 *dst, u8 *src, int count) 2948 { 2949 int idx; 2950 2951 for (idx = 0; idx < count; ++idx, ++dst) 2952 *dst = src[count - idx - 1]; 2953 } 2954 EXPORT_SYMBOL_GPL(vcap_netbytes_copy); 2955 2956 /* Convert validation error code into tc extack error message */ 2957 void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule) 2958 { 2959 switch (vrule->exterr) { 2960 case VCAP_ERR_NONE: 2961 break; 2962 case VCAP_ERR_NO_ADMIN: 2963 NL_SET_ERR_MSG_MOD(fco->common.extack, 2964 "Missing VCAP instance"); 2965 break; 2966 case VCAP_ERR_NO_NETDEV: 2967 NL_SET_ERR_MSG_MOD(fco->common.extack, 2968 "Missing network interface"); 2969 break; 2970 case VCAP_ERR_NO_KEYSET_MATCH: 2971 NL_SET_ERR_MSG_MOD(fco->common.extack, 2972 "No keyset matched the filter keys"); 2973 break; 2974 case VCAP_ERR_NO_ACTIONSET_MATCH: 2975 NL_SET_ERR_MSG_MOD(fco->common.extack, 2976 "No actionset matched the filter actions"); 2977 break; 2978 case VCAP_ERR_NO_PORT_KEYSET_MATCH: 2979 NL_SET_ERR_MSG_MOD(fco->common.extack, 2980 "No port keyset matched the filter keys"); 2981 break; 2982 } 2983 } 2984 EXPORT_SYMBOL_GPL(vcap_set_tc_exterr); 2985 2986 /* Write a rule to VCAP HW to enable it */ 2987 static int vcap_enable_rule(struct vcap_rule_internal *ri) 2988 { 2989 struct vcap_client_actionfield *af, *naf; 2990 struct vcap_client_keyfield *kf, *nkf; 2991 int err; 2992 2993 vcap_erase_cache(ri); 2994 err = vcap_encode_rule(ri); 2995 if (err) 2996 goto out; 2997 err = vcap_write_rule(ri); 2998 if (err) 2999 goto out; 3000 3001 /* Deallocate the list of keys and actions */ 3002 list_for_each_entry_safe(kf, nkf, &ri->data.keyfields, ctrl.list) { 3003 list_del(&kf->ctrl.list); 3004 kfree(kf); 3005 } 3006 list_for_each_entry_safe(af, naf, &ri->data.actionfields, ctrl.list) { 3007 list_del(&af->ctrl.list); 3008 kfree(af); 3009 } 3010 ri->state = VCAP_RS_ENABLED; 3011 out: 3012 return err; 3013 } 3014 3015 /* Enable all disabled rules for a specific chain/port in the VCAP HW */ 3016 static int vcap_enable_rules(struct vcap_control *vctrl, 3017 struct net_device *ndev, int chain) 3018 { 3019 int next_chain = chain + VCAP_CID_LOOKUP_SIZE; 3020 struct vcap_rule_internal *ri; 3021 struct vcap_admin *admin; 3022 int err = 0; 3023 3024 list_for_each_entry(admin, &vctrl->list, list) { 3025 if (!(chain >= admin->first_cid && chain <= admin->last_cid)) 3026 continue; 3027 3028 /* Found the admin, now find the offloadable rules */ 3029 vcap_lock(admin); 3030 list_for_each_entry(ri, &admin->rules, list) { 3031 /* Is the rule in the lookup defined by the chain */ 3032 if (!(ri->data.vcap_chain_id >= chain && 3033 ri->data.vcap_chain_id < next_chain)) { 3034 continue; 3035 } 3036 3037 if (ri->ndev != ndev) 3038 continue; 3039 3040 if (ri->state != VCAP_RS_DISABLED) 3041 continue; 3042 3043 err = vcap_enable_rule(ri); 3044 if (err) 3045 break; 3046 } 3047 vcap_unlock(admin); 3048 if (err) 3049 break; 3050 } 3051 return err; 3052 } 3053 3054 /* Read and erase a rule from VCAP HW to disable it */ 3055 static int vcap_disable_rule(struct vcap_rule_internal *ri) 3056 { 3057 int err; 3058 3059 err = vcap_read_rule(ri); 3060 if (err) 3061 return err; 3062 err = vcap_decode_keyset(ri); 3063 if (err) 3064 return err; 3065 err = vcap_decode_actionset(ri); 3066 if (err) 3067 return err; 3068 3069 ri->state = VCAP_RS_DISABLED; 3070 ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size); 3071 return 0; 3072 } 3073 3074 /* Disable all enabled rules for a specific chain/port in the VCAP HW */ 3075 static int vcap_disable_rules(struct vcap_control *vctrl, 3076 struct net_device *ndev, int chain) 3077 { 3078 struct vcap_rule_internal *ri; 3079 struct vcap_admin *admin; 3080 int err = 0; 3081 3082 list_for_each_entry(admin, &vctrl->list, list) { 3083 if (!(chain >= admin->first_cid && chain <= admin->last_cid)) 3084 continue; 3085 3086 /* Found the admin, now find the rules on the chain */ 3087 vcap_lock(admin); 3088 list_for_each_entry(ri, &admin->rules, list) { 3089 if (ri->data.vcap_chain_id != chain) 3090 continue; 3091 3092 if (ri->ndev != ndev) 3093 continue; 3094 3095 if (ri->state != VCAP_RS_ENABLED) 3096 continue; 3097 3098 err = vcap_disable_rule(ri); 3099 if (err) 3100 break; 3101 } 3102 vcap_unlock(admin); 3103 if (err) 3104 break; 3105 } 3106 return err; 3107 } 3108 3109 /* Check if this port is already enabled for this VCAP instance */ 3110 static bool vcap_is_enabled(struct vcap_control *vctrl, struct net_device *ndev, 3111 int dst_cid) 3112 { 3113 struct vcap_enabled_port *eport; 3114 struct vcap_admin *admin; 3115 3116 list_for_each_entry(admin, &vctrl->list, list) 3117 list_for_each_entry(eport, &admin->enabled, list) 3118 if (eport->dst_cid == dst_cid && eport->ndev == ndev) 3119 return true; 3120 3121 return false; 3122 } 3123 3124 /* Enable this port and chain id in a VCAP instance */ 3125 static int vcap_enable(struct vcap_control *vctrl, struct net_device *ndev, 3126 unsigned long cookie, int src_cid, int dst_cid) 3127 { 3128 struct vcap_enabled_port *eport; 3129 struct vcap_admin *admin; 3130 3131 if (src_cid >= dst_cid) 3132 return -EFAULT; 3133 3134 admin = vcap_find_admin(vctrl, dst_cid); 3135 if (!admin) 3136 return -ENOENT; 3137 3138 eport = kzalloc_obj(*eport); 3139 if (!eport) 3140 return -ENOMEM; 3141 3142 eport->ndev = ndev; 3143 eport->cookie = cookie; 3144 eport->src_cid = src_cid; 3145 eport->dst_cid = dst_cid; 3146 vcap_lock(admin); 3147 list_add_tail(&eport->list, &admin->enabled); 3148 vcap_unlock(admin); 3149 3150 if (vcap_path_exist(vctrl, ndev, src_cid)) { 3151 /* Enable chained lookups */ 3152 while (dst_cid) { 3153 admin = vcap_find_admin(vctrl, dst_cid); 3154 if (!admin) 3155 return -ENOENT; 3156 3157 vcap_enable_rules(vctrl, ndev, dst_cid); 3158 dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid); 3159 } 3160 } 3161 return 0; 3162 } 3163 3164 /* Disable this port and chain id for a VCAP instance */ 3165 static int vcap_disable(struct vcap_control *vctrl, struct net_device *ndev, 3166 unsigned long cookie) 3167 { 3168 struct vcap_enabled_port *elem, *eport = NULL; 3169 struct vcap_admin *found = NULL, *admin; 3170 int dst_cid; 3171 3172 list_for_each_entry(admin, &vctrl->list, list) { 3173 list_for_each_entry(elem, &admin->enabled, list) { 3174 if (elem->cookie == cookie && elem->ndev == ndev) { 3175 eport = elem; 3176 found = admin; 3177 break; 3178 } 3179 } 3180 if (eport) 3181 break; 3182 } 3183 3184 if (!eport) 3185 return -ENOENT; 3186 3187 /* Disable chained lookups */ 3188 dst_cid = eport->dst_cid; 3189 while (dst_cid) { 3190 admin = vcap_find_admin(vctrl, dst_cid); 3191 if (!admin) 3192 return -ENOENT; 3193 3194 vcap_disable_rules(vctrl, ndev, dst_cid); 3195 dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid); 3196 } 3197 3198 vcap_lock(found); 3199 list_del(&eport->list); 3200 vcap_unlock(found); 3201 kfree(eport); 3202 return 0; 3203 } 3204 3205 /* Enable/Disable the VCAP instance lookups */ 3206 int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev, 3207 int src_cid, int dst_cid, unsigned long cookie, 3208 bool enable) 3209 { 3210 int err; 3211 3212 err = vcap_api_check(vctrl); 3213 if (err) 3214 return err; 3215 3216 if (!ndev) 3217 return -ENODEV; 3218 3219 /* Source and destination must be the first chain in a lookup */ 3220 if (src_cid % VCAP_CID_LOOKUP_SIZE) 3221 return -EFAULT; 3222 if (dst_cid % VCAP_CID_LOOKUP_SIZE) 3223 return -EFAULT; 3224 3225 if (enable) { 3226 if (vcap_is_enabled(vctrl, ndev, dst_cid)) 3227 return -EADDRINUSE; 3228 if (vcap_is_chain_used(vctrl, ndev, src_cid)) 3229 return -EADDRNOTAVAIL; 3230 err = vcap_enable(vctrl, ndev, cookie, src_cid, dst_cid); 3231 } else { 3232 err = vcap_disable(vctrl, ndev, cookie); 3233 } 3234 3235 return err; 3236 } 3237 EXPORT_SYMBOL_GPL(vcap_enable_lookups); 3238 3239 /* Is this chain id the last lookup of all VCAPs */ 3240 bool vcap_is_last_chain(struct vcap_control *vctrl, int cid, bool ingress) 3241 { 3242 struct vcap_admin *admin; 3243 int lookup; 3244 3245 if (vcap_api_check(vctrl)) 3246 return false; 3247 3248 admin = vcap_find_admin(vctrl, cid); 3249 if (!admin) 3250 return false; 3251 3252 if (!vcap_admin_is_last(vctrl, admin, ingress)) 3253 return false; 3254 3255 /* This must be the last lookup in this VCAP type */ 3256 lookup = vcap_chain_id_to_lookup(admin, cid); 3257 return lookup == admin->lookups - 1; 3258 } 3259 EXPORT_SYMBOL_GPL(vcap_is_last_chain); 3260 3261 /* Set a rule counter id (for certain vcaps only) */ 3262 void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id) 3263 { 3264 struct vcap_rule_internal *ri = to_intrule(rule); 3265 3266 ri->counter_id = counter_id; 3267 } 3268 EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id); 3269 3270 int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr) 3271 { 3272 struct vcap_rule_internal *ri = to_intrule(rule); 3273 int err; 3274 3275 err = vcap_api_check(ri->vctrl); 3276 if (err) 3277 return err; 3278 if (!ctr) { 3279 pr_err("%s:%d: counter is missing\n", __func__, __LINE__); 3280 return -EINVAL; 3281 } 3282 3283 vcap_lock(ri->admin); 3284 err = vcap_write_counter(ri, ctr); 3285 vcap_unlock(ri->admin); 3286 3287 return err; 3288 } 3289 EXPORT_SYMBOL_GPL(vcap_rule_set_counter); 3290 3291 int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr) 3292 { 3293 struct vcap_rule_internal *ri = to_intrule(rule); 3294 int err; 3295 3296 err = vcap_api_check(ri->vctrl); 3297 if (err) 3298 return err; 3299 if (!ctr) { 3300 pr_err("%s:%d: counter is missing\n", __func__, __LINE__); 3301 return -EINVAL; 3302 } 3303 3304 vcap_lock(ri->admin); 3305 err = vcap_read_counter(ri, ctr); 3306 vcap_unlock(ri->admin); 3307 3308 return err; 3309 } 3310 EXPORT_SYMBOL_GPL(vcap_rule_get_counter); 3311 3312 /* Get a copy of a client key field */ 3313 static int vcap_rule_get_key(struct vcap_rule *rule, 3314 enum vcap_key_field key, 3315 struct vcap_client_keyfield *ckf) 3316 { 3317 struct vcap_client_keyfield *field; 3318 3319 field = vcap_find_keyfield(rule, key); 3320 if (!field) 3321 return -EINVAL; 3322 memcpy(ckf, field, sizeof(*ckf)); 3323 INIT_LIST_HEAD(&ckf->ctrl.list); 3324 return 0; 3325 } 3326 3327 /* Find a keyset having the same size as the provided rule, where the keyset 3328 * does not have a type id. 3329 */ 3330 static int vcap_rule_get_untyped_keyset(struct vcap_rule_internal *ri, 3331 struct vcap_keyset_list *matches) 3332 { 3333 struct vcap_control *vctrl = ri->vctrl; 3334 enum vcap_type vt = ri->admin->vtype; 3335 const struct vcap_set *keyfield_set; 3336 int idx; 3337 3338 keyfield_set = vctrl->vcaps[vt].keyfield_set; 3339 for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) { 3340 if (keyfield_set[idx].sw_per_item == ri->keyset_sw && 3341 keyfield_set[idx].type_id == (u8)-1) { 3342 vcap_keyset_list_add(matches, idx); 3343 return 0; 3344 } 3345 } 3346 return -EINVAL; 3347 } 3348 3349 /* Get the keysets that matches the rule key type/mask */ 3350 int vcap_rule_get_keysets(struct vcap_rule_internal *ri, 3351 struct vcap_keyset_list *matches) 3352 { 3353 struct vcap_control *vctrl = ri->vctrl; 3354 enum vcap_type vt = ri->admin->vtype; 3355 const struct vcap_set *keyfield_set; 3356 struct vcap_client_keyfield kf = {}; 3357 u32 value, mask; 3358 int err, idx; 3359 3360 err = vcap_rule_get_key(&ri->data, VCAP_KF_TYPE, &kf); 3361 if (err) 3362 return vcap_rule_get_untyped_keyset(ri, matches); 3363 3364 if (kf.ctrl.type == VCAP_FIELD_BIT) { 3365 value = kf.data.u1.value; 3366 mask = kf.data.u1.mask; 3367 } else if (kf.ctrl.type == VCAP_FIELD_U32) { 3368 value = kf.data.u32.value; 3369 mask = kf.data.u32.mask; 3370 } else { 3371 return -EINVAL; 3372 } 3373 3374 keyfield_set = vctrl->vcaps[vt].keyfield_set; 3375 for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) { 3376 if (keyfield_set[idx].sw_per_item != ri->keyset_sw) 3377 continue; 3378 3379 if (keyfield_set[idx].type_id == (u8)-1) { 3380 vcap_keyset_list_add(matches, idx); 3381 continue; 3382 } 3383 3384 if ((keyfield_set[idx].type_id & mask) == value) 3385 vcap_keyset_list_add(matches, idx); 3386 } 3387 if (matches->cnt > 0) 3388 return 0; 3389 3390 return -EINVAL; 3391 } 3392 3393 /* Collect packet counts from all rules with the same cookie */ 3394 int vcap_get_rule_count_by_cookie(struct vcap_control *vctrl, 3395 struct vcap_counter *ctr, u64 cookie) 3396 { 3397 struct vcap_rule_internal *ri; 3398 struct vcap_counter temp = {}; 3399 struct vcap_admin *admin; 3400 int err; 3401 3402 err = vcap_api_check(vctrl); 3403 if (err) 3404 return err; 3405 3406 /* Iterate all rules in each VCAP instance */ 3407 list_for_each_entry(admin, &vctrl->list, list) { 3408 vcap_lock(admin); 3409 list_for_each_entry(ri, &admin->rules, list) { 3410 if (ri->data.cookie != cookie) 3411 continue; 3412 3413 err = vcap_read_counter(ri, &temp); 3414 if (err) 3415 goto unlock; 3416 ctr->value += temp.value; 3417 3418 /* Reset the rule counter */ 3419 temp.value = 0; 3420 temp.sticky = 0; 3421 err = vcap_write_counter(ri, &temp); 3422 if (err) 3423 goto unlock; 3424 } 3425 vcap_unlock(admin); 3426 } 3427 return err; 3428 3429 unlock: 3430 vcap_unlock(admin); 3431 return err; 3432 } 3433 EXPORT_SYMBOL_GPL(vcap_get_rule_count_by_cookie); 3434 3435 static int vcap_rule_mod_key(struct vcap_rule *rule, 3436 enum vcap_key_field key, 3437 enum vcap_field_type ftype, 3438 struct vcap_client_keyfield_data *data) 3439 { 3440 struct vcap_client_keyfield *field; 3441 3442 field = vcap_find_keyfield(rule, key); 3443 if (!field) 3444 return vcap_rule_add_key(rule, key, ftype, data); 3445 memcpy(&field->data, data, sizeof(field->data)); 3446 return 0; 3447 } 3448 3449 /* Modify a 32 bit key field with value and mask in the rule */ 3450 int vcap_rule_mod_key_u32(struct vcap_rule *rule, enum vcap_key_field key, 3451 u32 value, u32 mask) 3452 { 3453 struct vcap_client_keyfield_data data; 3454 3455 data.u32.value = value; 3456 data.u32.mask = mask; 3457 return vcap_rule_mod_key(rule, key, VCAP_FIELD_U32, &data); 3458 } 3459 EXPORT_SYMBOL_GPL(vcap_rule_mod_key_u32); 3460 3461 /* Remove a key field with value and mask in the rule */ 3462 int vcap_rule_rem_key(struct vcap_rule *rule, enum vcap_key_field key) 3463 { 3464 struct vcap_rule_internal *ri = to_intrule(rule); 3465 struct vcap_client_keyfield *field; 3466 3467 field = vcap_find_keyfield(rule, key); 3468 if (!field) { 3469 pr_err("%s:%d: key %s is not in the rule\n", 3470 __func__, __LINE__, vcap_keyfield_name(ri->vctrl, key)); 3471 return -EINVAL; 3472 } 3473 /* Deallocate the key field */ 3474 list_del(&field->ctrl.list); 3475 kfree(field); 3476 return 0; 3477 } 3478 EXPORT_SYMBOL_GPL(vcap_rule_rem_key); 3479 3480 static int vcap_rule_mod_action(struct vcap_rule *rule, 3481 enum vcap_action_field action, 3482 enum vcap_field_type ftype, 3483 struct vcap_client_actionfield_data *data) 3484 { 3485 struct vcap_client_actionfield *field; 3486 3487 field = vcap_find_actionfield(rule, action); 3488 if (!field) 3489 return vcap_rule_add_action(rule, action, ftype, data); 3490 memcpy(&field->data, data, sizeof(field->data)); 3491 return 0; 3492 } 3493 3494 /* Modify a 32 bit action field with value in the rule */ 3495 int vcap_rule_mod_action_u32(struct vcap_rule *rule, 3496 enum vcap_action_field action, 3497 u32 value) 3498 { 3499 struct vcap_client_actionfield_data data; 3500 3501 data.u32.value = value; 3502 return vcap_rule_mod_action(rule, action, VCAP_FIELD_U32, &data); 3503 } 3504 EXPORT_SYMBOL_GPL(vcap_rule_mod_action_u32); 3505 3506 /* Drop keys in a keylist and any keys that are not supported by the keyset */ 3507 int vcap_filter_rule_keys(struct vcap_rule *rule, 3508 enum vcap_key_field keylist[], int length, 3509 bool drop_unsupported) 3510 { 3511 struct vcap_rule_internal *ri = to_intrule(rule); 3512 struct vcap_client_keyfield *ckf, *next_ckf; 3513 const struct vcap_field *fields; 3514 enum vcap_key_field key; 3515 int err = 0; 3516 int idx; 3517 3518 if (length > 0) { 3519 err = -EEXIST; 3520 list_for_each_entry_safe(ckf, next_ckf, 3521 &ri->data.keyfields, ctrl.list) { 3522 key = ckf->ctrl.key; 3523 for (idx = 0; idx < length; ++idx) 3524 if (key == keylist[idx]) { 3525 list_del(&ckf->ctrl.list); 3526 kfree(ckf); 3527 idx++; 3528 err = 0; 3529 } 3530 } 3531 } 3532 if (drop_unsupported) { 3533 err = -EEXIST; 3534 fields = vcap_keyfields(ri->vctrl, ri->admin->vtype, 3535 rule->keyset); 3536 if (!fields) 3537 return err; 3538 list_for_each_entry_safe(ckf, next_ckf, 3539 &ri->data.keyfields, ctrl.list) { 3540 key = ckf->ctrl.key; 3541 if (fields[key].width == 0) { 3542 list_del(&ckf->ctrl.list); 3543 kfree(ckf); 3544 err = 0; 3545 } 3546 } 3547 } 3548 return err; 3549 } 3550 EXPORT_SYMBOL_GPL(vcap_filter_rule_keys); 3551 3552 /* Select the keyset from the list that results in the smallest rule size */ 3553 enum vcap_keyfield_set 3554 vcap_select_min_rule_keyset(struct vcap_control *vctrl, 3555 enum vcap_type vtype, 3556 struct vcap_keyset_list *kslist) 3557 { 3558 enum vcap_keyfield_set ret = VCAP_KFS_NO_VALUE; 3559 const struct vcap_set *kset; 3560 int max = 100, idx; 3561 3562 for (idx = 0; idx < kslist->cnt; ++idx) { 3563 kset = vcap_keyfieldset(vctrl, vtype, kslist->keysets[idx]); 3564 if (!kset) 3565 continue; 3566 if (kset->sw_per_item >= max) 3567 continue; 3568 max = kset->sw_per_item; 3569 ret = kslist->keysets[idx]; 3570 } 3571 return ret; 3572 } 3573 EXPORT_SYMBOL_GPL(vcap_select_min_rule_keyset); 3574 3575 /* Make a full copy of an existing rule with a new rule id */ 3576 struct vcap_rule *vcap_copy_rule(struct vcap_rule *erule) 3577 { 3578 struct vcap_rule_internal *ri = to_intrule(erule); 3579 struct vcap_client_actionfield *caf; 3580 struct vcap_client_keyfield *ckf; 3581 struct vcap_rule *rule; 3582 int err; 3583 3584 err = vcap_api_check(ri->vctrl); 3585 if (err) 3586 return ERR_PTR(err); 3587 3588 rule = vcap_alloc_rule(ri->vctrl, ri->ndev, ri->data.vcap_chain_id, 3589 ri->data.user, ri->data.priority, 0); 3590 if (IS_ERR(rule)) 3591 return rule; 3592 3593 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) { 3594 /* Add a key duplicate in the new rule */ 3595 err = vcap_rule_add_key(rule, 3596 ckf->ctrl.key, 3597 ckf->ctrl.type, 3598 &ckf->data); 3599 if (err) 3600 goto err; 3601 } 3602 3603 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) { 3604 /* Add a action duplicate in the new rule */ 3605 err = vcap_rule_add_action(rule, 3606 caf->ctrl.action, 3607 caf->ctrl.type, 3608 &caf->data); 3609 if (err) 3610 goto err; 3611 } 3612 return rule; 3613 err: 3614 vcap_free_rule(rule); 3615 return ERR_PTR(err); 3616 } 3617 EXPORT_SYMBOL_GPL(vcap_copy_rule); 3618 3619 #ifdef CONFIG_VCAP_KUNIT_TEST 3620 #include "vcap_api_kunit.c" 3621 #endif 3622