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 /* Moving a rule in the VCAP address space */ 12 struct vcap_rule_move { 13 int addr; /* address to move */ 14 int offset; /* change in address */ 15 int count; /* blocksize of addresses to move */ 16 }; 17 18 /* Stores the filter cookie that enabled the port */ 19 struct vcap_enabled_port { 20 struct list_head list; /* for insertion in enabled ports list */ 21 struct net_device *ndev; /* the enabled port */ 22 unsigned long cookie; /* filter that enabled the port */ 23 }; 24 25 void vcap_iter_set(struct vcap_stream_iter *itr, int sw_width, 26 const struct vcap_typegroup *tg, u32 offset) 27 { 28 memset(itr, 0, sizeof(*itr)); 29 itr->offset = offset; 30 itr->sw_width = sw_width; 31 itr->regs_per_sw = DIV_ROUND_UP(sw_width, 32); 32 itr->tg = tg; 33 } 34 35 static void vcap_iter_skip_tg(struct vcap_stream_iter *itr) 36 { 37 /* Compensate the field offset for preceding typegroups. 38 * A typegroup table ends with an all-zero terminator. 39 */ 40 while (itr->tg->width && itr->offset >= itr->tg->offset) { 41 itr->offset += itr->tg->width; 42 itr->tg++; /* next typegroup */ 43 } 44 } 45 46 void vcap_iter_update(struct vcap_stream_iter *itr) 47 { 48 int sw_idx, sw_bitpos; 49 50 /* Calculate the subword index and bitposition for current bit */ 51 sw_idx = itr->offset / itr->sw_width; 52 sw_bitpos = itr->offset % itr->sw_width; 53 /* Calculate the register index and bitposition for current bit */ 54 itr->reg_idx = (sw_idx * itr->regs_per_sw) + (sw_bitpos / 32); 55 itr->reg_bitpos = sw_bitpos % 32; 56 } 57 58 void vcap_iter_init(struct vcap_stream_iter *itr, int sw_width, 59 const struct vcap_typegroup *tg, u32 offset) 60 { 61 vcap_iter_set(itr, sw_width, tg, offset); 62 vcap_iter_skip_tg(itr); 63 vcap_iter_update(itr); 64 } 65 66 void vcap_iter_next(struct vcap_stream_iter *itr) 67 { 68 itr->offset++; 69 vcap_iter_skip_tg(itr); 70 vcap_iter_update(itr); 71 } 72 73 static void vcap_set_bit(u32 *stream, struct vcap_stream_iter *itr, bool value) 74 { 75 u32 mask = BIT(itr->reg_bitpos); 76 u32 *p = &stream[itr->reg_idx]; 77 78 if (value) 79 *p |= mask; 80 else 81 *p &= ~mask; 82 } 83 84 static void vcap_encode_bit(u32 *stream, struct vcap_stream_iter *itr, bool val) 85 { 86 /* When intersected by a type group field, stream the type group bits 87 * before continuing with the value bit 88 */ 89 while (itr->tg->width && 90 itr->offset >= itr->tg->offset && 91 itr->offset < itr->tg->offset + itr->tg->width) { 92 int tg_bitpos = itr->tg->offset - itr->offset; 93 94 vcap_set_bit(stream, itr, (itr->tg->value >> tg_bitpos) & 0x1); 95 itr->offset++; 96 vcap_iter_update(itr); 97 } 98 vcap_set_bit(stream, itr, val); 99 } 100 101 static void vcap_encode_field(u32 *stream, struct vcap_stream_iter *itr, 102 int width, const u8 *value) 103 { 104 int idx; 105 106 /* Loop over the field value bits and add the value bits one by one to 107 * the output stream. 108 */ 109 for (idx = 0; idx < width; idx++) { 110 u8 bidx = idx & GENMASK(2, 0); 111 112 /* Encode one field value bit */ 113 vcap_encode_bit(stream, itr, (value[idx / 8] >> bidx) & 0x1); 114 vcap_iter_next(itr); 115 } 116 } 117 118 static void vcap_encode_typegroups(u32 *stream, int sw_width, 119 const struct vcap_typegroup *tg, 120 bool mask) 121 { 122 struct vcap_stream_iter iter; 123 int idx; 124 125 /* Mask bits must be set to zeros (inverted later when writing to the 126 * mask cache register), so that the mask typegroup bits consist of 127 * match-1 or match-0, or both 128 */ 129 vcap_iter_set(&iter, sw_width, tg, 0); 130 while (iter.tg->width) { 131 /* Set position to current typegroup bit */ 132 iter.offset = iter.tg->offset; 133 vcap_iter_update(&iter); 134 for (idx = 0; idx < iter.tg->width; idx++) { 135 /* Iterate over current typegroup bits. Mask typegroup 136 * bits are always set 137 */ 138 if (mask) 139 vcap_set_bit(stream, &iter, 0x1); 140 else 141 vcap_set_bit(stream, &iter, 142 (iter.tg->value >> idx) & 0x1); 143 iter.offset++; 144 vcap_iter_update(&iter); 145 } 146 iter.tg++; /* next typegroup */ 147 } 148 } 149 150 /* Return the list of keyfields for the keyset */ 151 const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl, 152 enum vcap_type vt, 153 enum vcap_keyfield_set keyset) 154 { 155 /* Check that the keyset exists in the vcap keyset list */ 156 if (keyset >= vctrl->vcaps[vt].keyfield_set_size) 157 return NULL; 158 return vctrl->vcaps[vt].keyfield_set_map[keyset]; 159 } 160 161 /* Return the keyset information for the keyset */ 162 const struct vcap_set *vcap_keyfieldset(struct vcap_control *vctrl, 163 enum vcap_type vt, 164 enum vcap_keyfield_set keyset) 165 { 166 const struct vcap_set *kset; 167 168 /* Check that the keyset exists in the vcap keyset list */ 169 if (keyset >= vctrl->vcaps[vt].keyfield_set_size) 170 return NULL; 171 kset = &vctrl->vcaps[vt].keyfield_set[keyset]; 172 if (kset->sw_per_item == 0 || kset->sw_per_item > vctrl->vcaps[vt].sw_count) 173 return NULL; 174 return kset; 175 } 176 177 /* Return the typegroup table for the matching keyset (using subword size) */ 178 const struct vcap_typegroup * 179 vcap_keyfield_typegroup(struct vcap_control *vctrl, 180 enum vcap_type vt, enum vcap_keyfield_set keyset) 181 { 182 const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset); 183 184 /* Check that the keyset is valid */ 185 if (!kset) 186 return NULL; 187 return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item]; 188 } 189 190 /* Return the number of keyfields in the keyset */ 191 int vcap_keyfield_count(struct vcap_control *vctrl, 192 enum vcap_type vt, enum vcap_keyfield_set keyset) 193 { 194 /* Check that the keyset exists in the vcap keyset list */ 195 if (keyset >= vctrl->vcaps[vt].keyfield_set_size) 196 return 0; 197 return vctrl->vcaps[vt].keyfield_set_map_size[keyset]; 198 } 199 200 static void vcap_encode_keyfield(struct vcap_rule_internal *ri, 201 const struct vcap_client_keyfield *kf, 202 const struct vcap_field *rf, 203 const struct vcap_typegroup *tgt) 204 { 205 int sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width; 206 struct vcap_cache_data *cache = &ri->admin->cache; 207 struct vcap_stream_iter iter; 208 const u8 *value, *mask; 209 210 /* Encode the fields for the key and the mask in their respective 211 * streams, respecting the subword width. 212 */ 213 switch (kf->ctrl.type) { 214 case VCAP_FIELD_BIT: 215 value = &kf->data.u1.value; 216 mask = &kf->data.u1.mask; 217 break; 218 case VCAP_FIELD_U32: 219 value = (const u8 *)&kf->data.u32.value; 220 mask = (const u8 *)&kf->data.u32.mask; 221 break; 222 case VCAP_FIELD_U48: 223 value = kf->data.u48.value; 224 mask = kf->data.u48.mask; 225 break; 226 case VCAP_FIELD_U56: 227 value = kf->data.u56.value; 228 mask = kf->data.u56.mask; 229 break; 230 case VCAP_FIELD_U64: 231 value = kf->data.u64.value; 232 mask = kf->data.u64.mask; 233 break; 234 case VCAP_FIELD_U72: 235 value = kf->data.u72.value; 236 mask = kf->data.u72.mask; 237 break; 238 case VCAP_FIELD_U112: 239 value = kf->data.u112.value; 240 mask = kf->data.u112.mask; 241 break; 242 case VCAP_FIELD_U128: 243 value = kf->data.u128.value; 244 mask = kf->data.u128.mask; 245 break; 246 } 247 vcap_iter_init(&iter, sw_width, tgt, rf->offset); 248 vcap_encode_field(cache->keystream, &iter, rf->width, value); 249 vcap_iter_init(&iter, sw_width, tgt, rf->offset); 250 vcap_encode_field(cache->maskstream, &iter, rf->width, mask); 251 } 252 253 static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl, 254 struct vcap_rule_internal *ri, 255 const struct vcap_typegroup *tgt) 256 { 257 int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width; 258 struct vcap_cache_data *cache = &ri->admin->cache; 259 260 /* Encode the typegroup bits for the key and the mask in their streams, 261 * respecting the subword width. 262 */ 263 vcap_encode_typegroups(cache->keystream, sw_width, tgt, false); 264 vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true); 265 } 266 267 static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri) 268 { 269 const struct vcap_client_keyfield *ckf; 270 const struct vcap_typegroup *tg_table; 271 const struct vcap_field *kf_table; 272 int keyset_size; 273 274 /* Get a valid set of fields for the specific keyset */ 275 kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset); 276 if (!kf_table) { 277 pr_err("%s:%d: no fields available for this keyset: %d\n", 278 __func__, __LINE__, ri->data.keyset); 279 return -EINVAL; 280 } 281 /* Get a valid typegroup for the specific keyset */ 282 tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype, 283 ri->data.keyset); 284 if (!tg_table) { 285 pr_err("%s:%d: no typegroups available for this keyset: %d\n", 286 __func__, __LINE__, ri->data.keyset); 287 return -EINVAL; 288 } 289 /* Get a valid size for the specific keyset */ 290 keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype, 291 ri->data.keyset); 292 if (keyset_size == 0) { 293 pr_err("%s:%d: zero field count for this keyset: %d\n", 294 __func__, __LINE__, ri->data.keyset); 295 return -EINVAL; 296 } 297 /* Iterate over the keyfields (key, mask) in the rule 298 * and encode these bits 299 */ 300 if (list_empty(&ri->data.keyfields)) { 301 pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__); 302 return -EINVAL; 303 } 304 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) { 305 /* Check that the client entry exists in the keyset */ 306 if (ckf->ctrl.key >= keyset_size) { 307 pr_err("%s:%d: key %d is not in vcap\n", 308 __func__, __LINE__, ckf->ctrl.key); 309 return -EINVAL; 310 } 311 vcap_encode_keyfield(ri, ckf, &kf_table[ckf->ctrl.key], tg_table); 312 } 313 /* Add typegroup bits to the key/mask bitstreams */ 314 vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table); 315 return 0; 316 } 317 318 /* Return the list of actionfields for the actionset */ 319 const struct vcap_field * 320 vcap_actionfields(struct vcap_control *vctrl, 321 enum vcap_type vt, enum vcap_actionfield_set actionset) 322 { 323 /* Check that the actionset exists in the vcap actionset list */ 324 if (actionset >= vctrl->vcaps[vt].actionfield_set_size) 325 return NULL; 326 return vctrl->vcaps[vt].actionfield_set_map[actionset]; 327 } 328 329 const struct vcap_set * 330 vcap_actionfieldset(struct vcap_control *vctrl, 331 enum vcap_type vt, enum vcap_actionfield_set actionset) 332 { 333 const struct vcap_set *aset; 334 335 /* Check that the actionset exists in the vcap actionset list */ 336 if (actionset >= vctrl->vcaps[vt].actionfield_set_size) 337 return NULL; 338 aset = &vctrl->vcaps[vt].actionfield_set[actionset]; 339 if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count) 340 return NULL; 341 return aset; 342 } 343 344 /* Return the typegroup table for the matching actionset (using subword size) */ 345 const struct vcap_typegroup * 346 vcap_actionfield_typegroup(struct vcap_control *vctrl, 347 enum vcap_type vt, enum vcap_actionfield_set actionset) 348 { 349 const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset); 350 351 /* Check that the actionset is valid */ 352 if (!aset) 353 return NULL; 354 return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item]; 355 } 356 357 /* Return the number of actionfields in the actionset */ 358 int vcap_actionfield_count(struct vcap_control *vctrl, 359 enum vcap_type vt, 360 enum vcap_actionfield_set actionset) 361 { 362 /* Check that the actionset exists in the vcap actionset list */ 363 if (actionset >= vctrl->vcaps[vt].actionfield_set_size) 364 return 0; 365 return vctrl->vcaps[vt].actionfield_set_map_size[actionset]; 366 } 367 368 static void vcap_encode_actionfield(struct vcap_rule_internal *ri, 369 const struct vcap_client_actionfield *af, 370 const struct vcap_field *rf, 371 const struct vcap_typegroup *tgt) 372 { 373 int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width; 374 375 struct vcap_cache_data *cache = &ri->admin->cache; 376 struct vcap_stream_iter iter; 377 const u8 *value; 378 379 /* Encode the action field in the stream, respecting the subword width */ 380 switch (af->ctrl.type) { 381 case VCAP_FIELD_BIT: 382 value = &af->data.u1.value; 383 break; 384 case VCAP_FIELD_U32: 385 value = (const u8 *)&af->data.u32.value; 386 break; 387 case VCAP_FIELD_U48: 388 value = af->data.u48.value; 389 break; 390 case VCAP_FIELD_U56: 391 value = af->data.u56.value; 392 break; 393 case VCAP_FIELD_U64: 394 value = af->data.u64.value; 395 break; 396 case VCAP_FIELD_U72: 397 value = af->data.u72.value; 398 break; 399 case VCAP_FIELD_U112: 400 value = af->data.u112.value; 401 break; 402 case VCAP_FIELD_U128: 403 value = af->data.u128.value; 404 break; 405 } 406 vcap_iter_init(&iter, act_width, tgt, rf->offset); 407 vcap_encode_field(cache->actionstream, &iter, rf->width, value); 408 } 409 410 static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri, 411 const struct vcap_typegroup *tgt) 412 { 413 int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width; 414 struct vcap_cache_data *cache = &ri->admin->cache; 415 416 /* Encode the typegroup bits for the actionstream respecting the subword 417 * width. 418 */ 419 vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false); 420 } 421 422 static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri) 423 { 424 const struct vcap_client_actionfield *caf; 425 const struct vcap_typegroup *tg_table; 426 const struct vcap_field *af_table; 427 int actionset_size; 428 429 /* Get a valid set of actionset fields for the specific actionset */ 430 af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype, 431 ri->data.actionset); 432 if (!af_table) { 433 pr_err("%s:%d: no fields available for this actionset: %d\n", 434 __func__, __LINE__, ri->data.actionset); 435 return -EINVAL; 436 } 437 /* Get a valid typegroup for the specific actionset */ 438 tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype, 439 ri->data.actionset); 440 if (!tg_table) { 441 pr_err("%s:%d: no typegroups available for this actionset: %d\n", 442 __func__, __LINE__, ri->data.actionset); 443 return -EINVAL; 444 } 445 /* Get a valid actionset size for the specific actionset */ 446 actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype, 447 ri->data.actionset); 448 if (actionset_size == 0) { 449 pr_err("%s:%d: zero field count for this actionset: %d\n", 450 __func__, __LINE__, ri->data.actionset); 451 return -EINVAL; 452 } 453 /* Iterate over the actionfields in the rule 454 * and encode these bits 455 */ 456 if (list_empty(&ri->data.actionfields)) 457 pr_warn("%s:%d: no actionfields in the rule\n", 458 __func__, __LINE__); 459 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) { 460 /* Check that the client action exists in the actionset */ 461 if (caf->ctrl.action >= actionset_size) { 462 pr_err("%s:%d: action %d is not in vcap\n", 463 __func__, __LINE__, caf->ctrl.action); 464 return -EINVAL; 465 } 466 vcap_encode_actionfield(ri, caf, &af_table[caf->ctrl.action], 467 tg_table); 468 } 469 /* Add typegroup bits to the entry bitstreams */ 470 vcap_encode_actionfield_typegroups(ri, tg_table); 471 return 0; 472 } 473 474 static int vcap_encode_rule(struct vcap_rule_internal *ri) 475 { 476 int err; 477 478 err = vcap_encode_rule_keyset(ri); 479 if (err) 480 return err; 481 err = vcap_encode_rule_actionset(ri); 482 if (err) 483 return err; 484 return 0; 485 } 486 487 int vcap_api_check(struct vcap_control *ctrl) 488 { 489 if (!ctrl) { 490 pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__); 491 return -EINVAL; 492 } 493 if (!ctrl->ops || !ctrl->ops->validate_keyset || 494 !ctrl->ops->add_default_fields || !ctrl->ops->cache_erase || 495 !ctrl->ops->cache_write || !ctrl->ops->cache_read || 496 !ctrl->ops->init || !ctrl->ops->update || !ctrl->ops->move || 497 !ctrl->ops->port_info || !ctrl->ops->enable) { 498 pr_err("%s:%d: client operations are missing\n", 499 __func__, __LINE__); 500 return -ENOENT; 501 } 502 return 0; 503 } 504 505 void vcap_erase_cache(struct vcap_rule_internal *ri) 506 { 507 ri->vctrl->ops->cache_erase(ri->admin); 508 } 509 510 /* Update the keyset for the rule */ 511 int vcap_set_rule_set_keyset(struct vcap_rule *rule, 512 enum vcap_keyfield_set keyset) 513 { 514 struct vcap_rule_internal *ri = to_intrule(rule); 515 const struct vcap_set *kset; 516 int sw_width; 517 518 kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset); 519 /* Check that the keyset is valid */ 520 if (!kset) 521 return -EINVAL; 522 ri->keyset_sw = kset->sw_per_item; 523 sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width; 524 ri->keyset_sw_regs = DIV_ROUND_UP(sw_width, 32); 525 ri->data.keyset = keyset; 526 return 0; 527 } 528 EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset); 529 530 /* Update the actionset for the rule */ 531 int vcap_set_rule_set_actionset(struct vcap_rule *rule, 532 enum vcap_actionfield_set actionset) 533 { 534 struct vcap_rule_internal *ri = to_intrule(rule); 535 const struct vcap_set *aset; 536 int act_width; 537 538 aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset); 539 /* Check that the actionset is valid */ 540 if (!aset) 541 return -EINVAL; 542 ri->actionset_sw = aset->sw_per_item; 543 act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width; 544 ri->actionset_sw_regs = DIV_ROUND_UP(act_width, 32); 545 ri->data.actionset = actionset; 546 return 0; 547 } 548 EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset); 549 550 /* Find a rule with a provided rule id */ 551 static struct vcap_rule_internal *vcap_lookup_rule(struct vcap_control *vctrl, 552 u32 id) 553 { 554 struct vcap_rule_internal *ri; 555 struct vcap_admin *admin; 556 557 /* Look for the rule id in all vcaps */ 558 list_for_each_entry(admin, &vctrl->list, list) 559 list_for_each_entry(ri, &admin->rules, list) 560 if (ri->data.id == id) 561 return ri; 562 return NULL; 563 } 564 565 /* Find a rule id with a provided cookie */ 566 int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie) 567 { 568 struct vcap_rule_internal *ri; 569 struct vcap_admin *admin; 570 571 /* Look for the rule id in all vcaps */ 572 list_for_each_entry(admin, &vctrl->list, list) 573 list_for_each_entry(ri, &admin->rules, list) 574 if (ri->data.cookie == cookie) 575 return ri->data.id; 576 return -ENOENT; 577 } 578 EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie); 579 580 /* Make a shallow copy of the rule without the fields */ 581 struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri) 582 { 583 struct vcap_rule_internal *duprule; 584 585 /* Allocate the client part */ 586 duprule = kzalloc(sizeof(*duprule), GFP_KERNEL); 587 if (!duprule) 588 return ERR_PTR(-ENOMEM); 589 *duprule = *ri; 590 /* Not inserted in the VCAP */ 591 INIT_LIST_HEAD(&duprule->list); 592 /* No elements in these lists */ 593 INIT_LIST_HEAD(&duprule->data.keyfields); 594 INIT_LIST_HEAD(&duprule->data.actionfields); 595 return duprule; 596 } 597 598 /* Write VCAP cache content to the VCAP HW instance */ 599 static int vcap_write_rule(struct vcap_rule_internal *ri) 600 { 601 struct vcap_admin *admin = ri->admin; 602 int sw_idx, ent_idx = 0, act_idx = 0; 603 u32 addr = ri->addr; 604 605 if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) { 606 pr_err("%s:%d: rule is empty\n", __func__, __LINE__); 607 return -EINVAL; 608 } 609 /* Use the values in the streams to write the VCAP cache */ 610 for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) { 611 ri->vctrl->ops->cache_write(ri->ndev, admin, 612 VCAP_SEL_ENTRY, ent_idx, 613 ri->keyset_sw_regs); 614 ri->vctrl->ops->cache_write(ri->ndev, admin, 615 VCAP_SEL_ACTION, act_idx, 616 ri->actionset_sw_regs); 617 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE, 618 VCAP_SEL_ALL, addr); 619 ent_idx += ri->keyset_sw_regs; 620 act_idx += ri->actionset_sw_regs; 621 } 622 return 0; 623 } 624 625 static int vcap_write_counter(struct vcap_rule_internal *ri, 626 struct vcap_counter *ctr) 627 { 628 struct vcap_admin *admin = ri->admin; 629 630 admin->cache.counter = ctr->value; 631 admin->cache.sticky = ctr->sticky; 632 ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER, 633 ri->counter_id, 0); 634 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE, 635 VCAP_SEL_COUNTER, ri->addr); 636 return 0; 637 } 638 639 /* Convert a chain id to a VCAP lookup index */ 640 int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid) 641 { 642 int lookup_first = admin->vinst * admin->lookups_per_instance; 643 int lookup_last = lookup_first + admin->lookups_per_instance; 644 int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE; 645 int cid = admin->first_cid; 646 int lookup; 647 648 for (lookup = lookup_first; lookup < lookup_last; ++lookup, 649 cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE) 650 if (cur_cid >= cid && cur_cid < cid_next) 651 return lookup; 652 return 0; 653 } 654 EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup); 655 656 /* Lookup a vcap instance using chain id */ 657 struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid) 658 { 659 struct vcap_admin *admin; 660 661 if (vcap_api_check(vctrl)) 662 return NULL; 663 664 list_for_each_entry(admin, &vctrl->list, list) { 665 if (cid >= admin->first_cid && cid <= admin->last_cid) 666 return admin; 667 } 668 return NULL; 669 } 670 EXPORT_SYMBOL_GPL(vcap_find_admin); 671 672 /* Is the next chain id in the following lookup, possible in another VCAP */ 673 bool vcap_is_next_lookup(struct vcap_control *vctrl, int cur_cid, int next_cid) 674 { 675 struct vcap_admin *admin, *next_admin; 676 int lookup, next_lookup; 677 678 /* The offset must be at least one lookup */ 679 if (next_cid < cur_cid + VCAP_CID_LOOKUP_SIZE) 680 return false; 681 682 if (vcap_api_check(vctrl)) 683 return false; 684 685 admin = vcap_find_admin(vctrl, cur_cid); 686 if (!admin) 687 return false; 688 689 /* If no VCAP contains the next chain, the next chain must be beyond 690 * the last chain in the current VCAP 691 */ 692 next_admin = vcap_find_admin(vctrl, next_cid); 693 if (!next_admin) 694 return next_cid > admin->last_cid; 695 696 lookup = vcap_chain_id_to_lookup(admin, cur_cid); 697 next_lookup = vcap_chain_id_to_lookup(next_admin, next_cid); 698 699 /* Next lookup must be the following lookup */ 700 if (admin == next_admin || admin->vtype == next_admin->vtype) 701 return next_lookup == lookup + 1; 702 703 /* Must be the first lookup in the next VCAP instance */ 704 return next_lookup == 0; 705 } 706 EXPORT_SYMBOL_GPL(vcap_is_next_lookup); 707 708 /* Check if there is room for a new rule */ 709 static int vcap_rule_space(struct vcap_admin *admin, int size) 710 { 711 if (admin->last_used_addr - size < admin->first_valid_addr) { 712 pr_err("%s:%d: No room for rule size: %u, %u\n", 713 __func__, __LINE__, size, admin->first_valid_addr); 714 return -ENOSPC; 715 } 716 return 0; 717 } 718 719 /* Add the keyset typefield to the list of rule keyfields */ 720 static int vcap_add_type_keyfield(struct vcap_rule *rule) 721 { 722 struct vcap_rule_internal *ri = to_intrule(rule); 723 enum vcap_keyfield_set keyset = rule->keyset; 724 enum vcap_type vt = ri->admin->vtype; 725 const struct vcap_field *fields; 726 const struct vcap_set *kset; 727 int ret = -EINVAL; 728 729 kset = vcap_keyfieldset(ri->vctrl, vt, keyset); 730 if (!kset) 731 return ret; 732 if (kset->type_id == (u8)-1) /* No type field is needed */ 733 return 0; 734 735 fields = vcap_keyfields(ri->vctrl, vt, keyset); 736 if (!fields) 737 return -EINVAL; 738 if (fields[VCAP_KF_TYPE].width > 1) { 739 ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE, 740 kset->type_id, 0xff); 741 } else { 742 if (kset->type_id) 743 ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE, 744 VCAP_BIT_1); 745 else 746 ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE, 747 VCAP_BIT_0); 748 } 749 return 0; 750 } 751 752 /* Add a keyset to a keyset list */ 753 bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist, 754 enum vcap_keyfield_set keyset) 755 { 756 int idx; 757 758 if (keysetlist->cnt < keysetlist->max) { 759 /* Avoid duplicates */ 760 for (idx = 0; idx < keysetlist->cnt; ++idx) 761 if (keysetlist->keysets[idx] == keyset) 762 return keysetlist->cnt < keysetlist->max; 763 keysetlist->keysets[keysetlist->cnt++] = keyset; 764 } 765 return keysetlist->cnt < keysetlist->max; 766 } 767 EXPORT_SYMBOL_GPL(vcap_keyset_list_add); 768 769 /* map keyset id to a string with the keyset name */ 770 const char *vcap_keyset_name(struct vcap_control *vctrl, 771 enum vcap_keyfield_set keyset) 772 { 773 return vctrl->stats->keyfield_set_names[keyset]; 774 } 775 EXPORT_SYMBOL_GPL(vcap_keyset_name); 776 777 /* map key field id to a string with the key name */ 778 const char *vcap_keyfield_name(struct vcap_control *vctrl, 779 enum vcap_key_field key) 780 { 781 return vctrl->stats->keyfield_names[key]; 782 } 783 EXPORT_SYMBOL_GPL(vcap_keyfield_name); 784 785 /* map actionset id to a string with the actionset name */ 786 const char *vcap_actionset_name(struct vcap_control *vctrl, 787 enum vcap_actionfield_set actionset) 788 { 789 return vctrl->stats->actionfield_set_names[actionset]; 790 } 791 792 /* map action field id to a string with the action name */ 793 const char *vcap_actionfield_name(struct vcap_control *vctrl, 794 enum vcap_action_field action) 795 { 796 return vctrl->stats->actionfield_names[action]; 797 } 798 799 /* Return the keyfield that matches a key in a keyset */ 800 static const struct vcap_field * 801 vcap_find_keyset_keyfield(struct vcap_control *vctrl, 802 enum vcap_type vtype, 803 enum vcap_keyfield_set keyset, 804 enum vcap_key_field key) 805 { 806 const struct vcap_field *fields; 807 int idx, count; 808 809 fields = vcap_keyfields(vctrl, vtype, keyset); 810 if (!fields) 811 return NULL; 812 813 /* Iterate the keyfields of the keyset */ 814 count = vcap_keyfield_count(vctrl, vtype, keyset); 815 for (idx = 0; idx < count; ++idx) { 816 if (fields[idx].width == 0) 817 continue; 818 819 if (key == idx) 820 return &fields[idx]; 821 } 822 823 return NULL; 824 } 825 826 /* Match a list of keys against the keysets available in a vcap type */ 827 static bool vcap_rule_find_keysets(struct vcap_rule_internal *ri, 828 struct vcap_keyset_list *matches) 829 { 830 const struct vcap_client_keyfield *ckf; 831 int keyset, found, keycount, map_size; 832 const struct vcap_field **map; 833 enum vcap_type vtype; 834 835 vtype = ri->admin->vtype; 836 map = ri->vctrl->vcaps[vtype].keyfield_set_map; 837 map_size = ri->vctrl->vcaps[vtype].keyfield_set_size; 838 839 /* Get a count of the keyfields we want to match */ 840 keycount = 0; 841 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) 842 ++keycount; 843 844 matches->cnt = 0; 845 /* Iterate the keysets of the VCAP */ 846 for (keyset = 0; keyset < map_size; ++keyset) { 847 if (!map[keyset]) 848 continue; 849 850 /* Iterate the keys in the rule */ 851 found = 0; 852 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) 853 if (vcap_find_keyset_keyfield(ri->vctrl, vtype, 854 keyset, ckf->ctrl.key)) 855 ++found; 856 857 /* Save the keyset if all keyfields were found */ 858 if (found == keycount) 859 if (!vcap_keyset_list_add(matches, keyset)) 860 /* bail out when the quota is filled */ 861 break; 862 } 863 864 return matches->cnt > 0; 865 } 866 867 /* Validate a rule with respect to available port keys */ 868 int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto) 869 { 870 struct vcap_rule_internal *ri = to_intrule(rule); 871 struct vcap_keyset_list matches = {}; 872 enum vcap_keyfield_set keysets[10]; 873 int ret; 874 875 ret = vcap_api_check(ri->vctrl); 876 if (ret) 877 return ret; 878 if (!ri->admin) { 879 ri->data.exterr = VCAP_ERR_NO_ADMIN; 880 return -EINVAL; 881 } 882 if (!ri->ndev) { 883 ri->data.exterr = VCAP_ERR_NO_NETDEV; 884 return -EINVAL; 885 } 886 887 matches.keysets = keysets; 888 matches.max = ARRAY_SIZE(keysets); 889 if (ri->data.keyset == VCAP_KFS_NO_VALUE) { 890 /* Iterate over rule keyfields and select keysets that fits */ 891 if (!vcap_rule_find_keysets(ri, &matches)) { 892 ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH; 893 return -EINVAL; 894 } 895 } else { 896 /* prepare for keyset validation */ 897 keysets[0] = ri->data.keyset; 898 matches.cnt = 1; 899 } 900 901 /* Pick a keyset that is supported in the port lookups */ 902 ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule, 903 &matches, l3_proto); 904 if (ret < 0) { 905 pr_err("%s:%d: keyset validation failed: %d\n", 906 __func__, __LINE__, ret); 907 ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH; 908 return ret; 909 } 910 /* use the keyset that is supported in the port lookups */ 911 ret = vcap_set_rule_set_keyset(rule, ret); 912 if (ret < 0) { 913 pr_err("%s:%d: keyset was not updated: %d\n", 914 __func__, __LINE__, ret); 915 return ret; 916 } 917 if (ri->data.actionset == VCAP_AFS_NO_VALUE) { 918 /* Later also actionsets will be matched against actions in 919 * the rule, and the type will be set accordingly 920 */ 921 ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH; 922 return -EINVAL; 923 } 924 vcap_add_type_keyfield(rule); 925 /* Add default fields to this rule */ 926 ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule); 927 928 /* Rule size is the maximum of the entry and action subword count */ 929 ri->size = max(ri->keyset_sw, ri->actionset_sw); 930 931 /* Finally check if there is room for the rule in the VCAP */ 932 return vcap_rule_space(ri->admin, ri->size); 933 } 934 EXPORT_SYMBOL_GPL(vcap_val_rule); 935 936 /* Entries are sorted with increasing values of sort_key. 937 * I.e. Lowest numerical sort_key is first in list. 938 * In order to locate largest keys first in list we negate the key size with 939 * (max_size - size). 940 */ 941 static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio) 942 { 943 return ((max_size - size) << 24) | (user << 16) | prio; 944 } 945 946 /* calculate the address of the next rule after this (lower address and prio) */ 947 static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri) 948 { 949 return ((addr - ri->size) / ri->size) * ri->size; 950 } 951 952 /* Assign a unique rule id and autogenerate one if id == 0 */ 953 static u32 vcap_set_rule_id(struct vcap_rule_internal *ri) 954 { 955 u32 next_id; 956 957 if (ri->data.id != 0) 958 return ri->data.id; 959 960 next_id = ri->vctrl->rule_id + 1; 961 962 for (next_id = ri->vctrl->rule_id + 1; next_id < ~0; ++next_id) { 963 if (!vcap_lookup_rule(ri->vctrl, next_id)) { 964 ri->data.id = next_id; 965 ri->vctrl->rule_id = next_id; 966 break; 967 } 968 } 969 return ri->data.id; 970 } 971 972 static int vcap_insert_rule(struct vcap_rule_internal *ri, 973 struct vcap_rule_move *move) 974 { 975 int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count; 976 struct vcap_rule_internal *duprule, *iter, *elem = NULL; 977 struct vcap_admin *admin = ri->admin; 978 u32 addr; 979 980 ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user, 981 ri->data.priority); 982 983 /* Insert the new rule in the list of rule based on the sort key 984 * If the rule needs to be inserted between existing rules then move 985 * these rules to make room for the new rule and update their start 986 * address. 987 */ 988 list_for_each_entry(iter, &admin->rules, list) { 989 if (ri->sort_key < iter->sort_key) { 990 elem = iter; 991 break; 992 } 993 } 994 995 if (!elem) { 996 ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri); 997 admin->last_used_addr = ri->addr; 998 999 /* Add a shallow copy of the rule to the VCAP list */ 1000 duprule = vcap_dup_rule(ri); 1001 if (IS_ERR(duprule)) 1002 return PTR_ERR(duprule); 1003 1004 list_add_tail(&duprule->list, &admin->rules); 1005 return 0; 1006 } 1007 1008 /* Reuse the space of the current rule */ 1009 addr = elem->addr + elem->size; 1010 ri->addr = vcap_next_rule_addr(addr, ri); 1011 addr = ri->addr; 1012 1013 /* Add a shallow copy of the rule to the VCAP list */ 1014 duprule = vcap_dup_rule(ri); 1015 if (IS_ERR(duprule)) 1016 return PTR_ERR(duprule); 1017 1018 /* Add before the current entry */ 1019 list_add_tail(&duprule->list, &elem->list); 1020 1021 /* Update the current rule */ 1022 elem->addr = vcap_next_rule_addr(addr, elem); 1023 addr = elem->addr; 1024 1025 /* Update the address in the remaining rules in the list */ 1026 list_for_each_entry_continue(elem, &admin->rules, list) { 1027 elem->addr = vcap_next_rule_addr(addr, elem); 1028 addr = elem->addr; 1029 } 1030 1031 /* Update the move info */ 1032 move->addr = admin->last_used_addr; 1033 move->count = ri->addr - addr; 1034 move->offset = admin->last_used_addr - addr; 1035 admin->last_used_addr = addr; 1036 return 0; 1037 } 1038 1039 static void vcap_move_rules(struct vcap_rule_internal *ri, 1040 struct vcap_rule_move *move) 1041 { 1042 ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr, 1043 move->offset, move->count); 1044 } 1045 1046 /* Encode and write a validated rule to the VCAP */ 1047 int vcap_add_rule(struct vcap_rule *rule) 1048 { 1049 struct vcap_rule_internal *ri = to_intrule(rule); 1050 struct vcap_rule_move move = {0}; 1051 int ret; 1052 1053 ret = vcap_api_check(ri->vctrl); 1054 if (ret) 1055 return ret; 1056 /* Insert the new rule in the list of vcap rules */ 1057 mutex_lock(&ri->admin->lock); 1058 ret = vcap_insert_rule(ri, &move); 1059 if (ret < 0) { 1060 pr_err("%s:%d: could not insert rule in vcap list: %d\n", 1061 __func__, __LINE__, ret); 1062 goto out; 1063 } 1064 if (move.count > 0) 1065 vcap_move_rules(ri, &move); 1066 ret = vcap_encode_rule(ri); 1067 if (ret) { 1068 pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret); 1069 goto out; 1070 } 1071 1072 ret = vcap_write_rule(ri); 1073 if (ret) 1074 pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret); 1075 out: 1076 mutex_unlock(&ri->admin->lock); 1077 return ret; 1078 } 1079 EXPORT_SYMBOL_GPL(vcap_add_rule); 1080 1081 /* Allocate a new rule with the provided arguments */ 1082 struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl, 1083 struct net_device *ndev, int vcap_chain_id, 1084 enum vcap_user user, u16 priority, 1085 u32 id) 1086 { 1087 struct vcap_rule_internal *ri; 1088 struct vcap_admin *admin; 1089 int err, maxsize; 1090 1091 err = vcap_api_check(vctrl); 1092 if (err) 1093 return ERR_PTR(err); 1094 if (!ndev) 1095 return ERR_PTR(-ENODEV); 1096 /* Get the VCAP instance */ 1097 admin = vcap_find_admin(vctrl, vcap_chain_id); 1098 if (!admin) 1099 return ERR_PTR(-ENOENT); 1100 /* Sanity check that this VCAP is supported on this platform */ 1101 if (vctrl->vcaps[admin->vtype].rows == 0) 1102 return ERR_PTR(-EINVAL); 1103 /* Check if a rule with this id already exists */ 1104 if (vcap_lookup_rule(vctrl, id)) 1105 return ERR_PTR(-EEXIST); 1106 /* Check if there is room for the rule in the block(s) of the VCAP */ 1107 maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */ 1108 if (vcap_rule_space(admin, maxsize)) 1109 return ERR_PTR(-ENOSPC); 1110 /* Create a container for the rule and return it */ 1111 ri = kzalloc(sizeof(*ri), GFP_KERNEL); 1112 if (!ri) 1113 return ERR_PTR(-ENOMEM); 1114 ri->data.vcap_chain_id = vcap_chain_id; 1115 ri->data.user = user; 1116 ri->data.priority = priority; 1117 ri->data.id = id; 1118 ri->data.keyset = VCAP_KFS_NO_VALUE; 1119 ri->data.actionset = VCAP_AFS_NO_VALUE; 1120 INIT_LIST_HEAD(&ri->list); 1121 INIT_LIST_HEAD(&ri->data.keyfields); 1122 INIT_LIST_HEAD(&ri->data.actionfields); 1123 ri->ndev = ndev; 1124 ri->admin = admin; /* refer to the vcap instance */ 1125 ri->vctrl = vctrl; /* refer to the client */ 1126 if (vcap_set_rule_id(ri) == 0) 1127 goto out_free; 1128 vcap_erase_cache(ri); 1129 return (struct vcap_rule *)ri; 1130 1131 out_free: 1132 kfree(ri); 1133 return ERR_PTR(-EINVAL); 1134 } 1135 EXPORT_SYMBOL_GPL(vcap_alloc_rule); 1136 1137 /* Free mem of a rule owned by client after the rule as been added to the VCAP */ 1138 void vcap_free_rule(struct vcap_rule *rule) 1139 { 1140 struct vcap_rule_internal *ri = to_intrule(rule); 1141 struct vcap_client_actionfield *caf, *next_caf; 1142 struct vcap_client_keyfield *ckf, *next_ckf; 1143 1144 /* Deallocate the list of keys and actions */ 1145 list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) { 1146 list_del(&ckf->ctrl.list); 1147 kfree(ckf); 1148 } 1149 list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) { 1150 list_del(&caf->ctrl.list); 1151 kfree(caf); 1152 } 1153 /* Deallocate the rule */ 1154 kfree(rule); 1155 } 1156 EXPORT_SYMBOL_GPL(vcap_free_rule); 1157 1158 /* Return the alignment offset for a new rule address */ 1159 static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset) 1160 { 1161 return (el->addr + offset) % el->size; 1162 } 1163 1164 /* Update the rule address with an offset */ 1165 static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset) 1166 { 1167 el->addr += offset; 1168 } 1169 1170 /* Rules needs to be moved to fill the gap of the deleted rule */ 1171 static int vcap_fill_rule_gap(struct vcap_rule_internal *ri) 1172 { 1173 struct vcap_admin *admin = ri->admin; 1174 struct vcap_rule_internal *elem; 1175 struct vcap_rule_move move; 1176 int gap = 0, offset = 0; 1177 1178 /* If the first rule is deleted: Move other rules to the top */ 1179 if (list_is_first(&ri->list, &admin->rules)) 1180 offset = admin->last_valid_addr + 1 - ri->addr - ri->size; 1181 1182 /* Locate gaps between odd size rules and adjust the move */ 1183 elem = ri; 1184 list_for_each_entry_continue(elem, &admin->rules, list) 1185 gap += vcap_valid_rule_move(elem, ri->size); 1186 1187 /* Update the address in the remaining rules in the list */ 1188 elem = ri; 1189 list_for_each_entry_continue(elem, &admin->rules, list) 1190 vcap_adjust_rule_addr(elem, ri->size + gap + offset); 1191 1192 /* Update the move info */ 1193 move.addr = admin->last_used_addr; 1194 move.count = ri->addr - admin->last_used_addr - gap; 1195 move.offset = -(ri->size + gap + offset); 1196 1197 /* Do the actual move operation */ 1198 vcap_move_rules(ri, &move); 1199 1200 return gap + offset; 1201 } 1202 1203 /* Delete rule in a VCAP instance */ 1204 int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id) 1205 { 1206 struct vcap_rule_internal *ri, *elem; 1207 struct vcap_admin *admin; 1208 int gap = 0, err; 1209 1210 /* This will later also handle rule moving */ 1211 if (!ndev) 1212 return -ENODEV; 1213 err = vcap_api_check(vctrl); 1214 if (err) 1215 return err; 1216 /* Look for the rule id in all vcaps */ 1217 ri = vcap_lookup_rule(vctrl, id); 1218 if (!ri) 1219 return -EINVAL; 1220 admin = ri->admin; 1221 1222 if (ri->addr > admin->last_used_addr) 1223 gap = vcap_fill_rule_gap(ri); 1224 1225 /* Delete the rule from the list of rules and the cache */ 1226 mutex_lock(&admin->lock); 1227 list_del(&ri->list); 1228 vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap); 1229 kfree(ri); 1230 mutex_unlock(&admin->lock); 1231 1232 /* Update the last used address, set to default when no rules */ 1233 if (list_empty(&admin->rules)) { 1234 admin->last_used_addr = admin->last_valid_addr + 1; 1235 } else { 1236 elem = list_last_entry(&admin->rules, struct vcap_rule_internal, 1237 list); 1238 admin->last_used_addr = elem->addr; 1239 } 1240 return 0; 1241 } 1242 EXPORT_SYMBOL_GPL(vcap_del_rule); 1243 1244 /* Delete all rules in the VCAP instance */ 1245 int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin) 1246 { 1247 struct vcap_enabled_port *eport, *next_eport; 1248 struct vcap_rule_internal *ri, *next_ri; 1249 int ret = vcap_api_check(vctrl); 1250 1251 if (ret) 1252 return ret; 1253 1254 mutex_lock(&admin->lock); 1255 list_for_each_entry_safe(ri, next_ri, &admin->rules, list) { 1256 vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size); 1257 list_del(&ri->list); 1258 kfree(ri); 1259 } 1260 admin->last_used_addr = admin->last_valid_addr; 1261 1262 /* Remove list of enabled ports */ 1263 list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) { 1264 list_del(&eport->list); 1265 kfree(eport); 1266 } 1267 mutex_unlock(&admin->lock); 1268 1269 return 0; 1270 } 1271 EXPORT_SYMBOL_GPL(vcap_del_rules); 1272 1273 /* Find information on a key field in a rule */ 1274 const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule, 1275 enum vcap_key_field key) 1276 { 1277 struct vcap_rule_internal *ri = to_intrule(rule); 1278 enum vcap_keyfield_set keyset = rule->keyset; 1279 enum vcap_type vt = ri->admin->vtype; 1280 const struct vcap_field *fields; 1281 1282 if (keyset == VCAP_KFS_NO_VALUE) 1283 return NULL; 1284 fields = vcap_keyfields(ri->vctrl, vt, keyset); 1285 if (!fields) 1286 return NULL; 1287 return &fields[key]; 1288 } 1289 EXPORT_SYMBOL_GPL(vcap_lookup_keyfield); 1290 1291 static void vcap_copy_from_client_keyfield(struct vcap_rule *rule, 1292 struct vcap_client_keyfield *field, 1293 struct vcap_client_keyfield_data *data) 1294 { 1295 /* This will be expanded later to handle different vcap memory layouts */ 1296 memcpy(&field->data, data, sizeof(field->data)); 1297 } 1298 1299 /* Check if the keyfield is already in the rule */ 1300 static bool vcap_keyfield_unique(struct vcap_rule *rule, 1301 enum vcap_key_field key) 1302 { 1303 struct vcap_rule_internal *ri = to_intrule(rule); 1304 const struct vcap_client_keyfield *ckf; 1305 1306 list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) 1307 if (ckf->ctrl.key == key) 1308 return false; 1309 return true; 1310 } 1311 1312 /* Check if the keyfield is in the keyset */ 1313 static bool vcap_keyfield_match_keyset(struct vcap_rule *rule, 1314 enum vcap_key_field key) 1315 { 1316 struct vcap_rule_internal *ri = to_intrule(rule); 1317 enum vcap_keyfield_set keyset = rule->keyset; 1318 enum vcap_type vt = ri->admin->vtype; 1319 const struct vcap_field *fields; 1320 1321 /* the field is accepted if the rule has no keyset yet */ 1322 if (keyset == VCAP_KFS_NO_VALUE) 1323 return true; 1324 fields = vcap_keyfields(ri->vctrl, vt, keyset); 1325 if (!fields) 1326 return false; 1327 /* if there is a width there is a way */ 1328 return fields[key].width > 0; 1329 } 1330 1331 static int vcap_rule_add_key(struct vcap_rule *rule, 1332 enum vcap_key_field key, 1333 enum vcap_field_type ftype, 1334 struct vcap_client_keyfield_data *data) 1335 { 1336 struct vcap_rule_internal *ri = to_intrule(rule); 1337 struct vcap_client_keyfield *field; 1338 1339 if (!vcap_keyfield_unique(rule, key)) { 1340 pr_warn("%s:%d: keyfield %s is already in the rule\n", 1341 __func__, __LINE__, 1342 vcap_keyfield_name(ri->vctrl, key)); 1343 return -EINVAL; 1344 } 1345 1346 if (!vcap_keyfield_match_keyset(rule, key)) { 1347 pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n", 1348 __func__, __LINE__, 1349 vcap_keyfield_name(ri->vctrl, key)); 1350 return -EINVAL; 1351 } 1352 1353 field = kzalloc(sizeof(*field), GFP_KERNEL); 1354 if (!field) 1355 return -ENOMEM; 1356 field->ctrl.key = key; 1357 field->ctrl.type = ftype; 1358 vcap_copy_from_client_keyfield(rule, field, data); 1359 list_add_tail(&field->ctrl.list, &rule->keyfields); 1360 return 0; 1361 } 1362 1363 static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val) 1364 { 1365 switch (val) { 1366 case VCAP_BIT_0: 1367 u1->value = 0; 1368 u1->mask = 1; 1369 break; 1370 case VCAP_BIT_1: 1371 u1->value = 1; 1372 u1->mask = 1; 1373 break; 1374 case VCAP_BIT_ANY: 1375 u1->value = 0; 1376 u1->mask = 0; 1377 break; 1378 } 1379 } 1380 1381 /* Add a bit key with value and mask to the rule */ 1382 int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key, 1383 enum vcap_bit val) 1384 { 1385 struct vcap_client_keyfield_data data; 1386 1387 vcap_rule_set_key_bitsize(&data.u1, val); 1388 return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data); 1389 } 1390 EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit); 1391 1392 /* Add a 32 bit key field with value and mask to the rule */ 1393 int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key, 1394 u32 value, u32 mask) 1395 { 1396 struct vcap_client_keyfield_data data; 1397 1398 data.u32.value = value; 1399 data.u32.mask = mask; 1400 return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data); 1401 } 1402 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32); 1403 1404 /* Add a 48 bit key with value and mask to the rule */ 1405 int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key, 1406 struct vcap_u48_key *fieldval) 1407 { 1408 struct vcap_client_keyfield_data data; 1409 1410 memcpy(&data.u48, fieldval, sizeof(data.u48)); 1411 return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data); 1412 } 1413 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48); 1414 1415 /* Add a 72 bit key with value and mask to the rule */ 1416 int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key, 1417 struct vcap_u72_key *fieldval) 1418 { 1419 struct vcap_client_keyfield_data data; 1420 1421 memcpy(&data.u72, fieldval, sizeof(data.u72)); 1422 return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data); 1423 } 1424 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72); 1425 1426 /* Add a 128 bit key with value and mask to the rule */ 1427 int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key, 1428 struct vcap_u128_key *fieldval) 1429 { 1430 struct vcap_client_keyfield_data data; 1431 1432 memcpy(&data.u128, fieldval, sizeof(data.u128)); 1433 return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data); 1434 } 1435 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128); 1436 1437 static void vcap_copy_from_client_actionfield(struct vcap_rule *rule, 1438 struct vcap_client_actionfield *field, 1439 struct vcap_client_actionfield_data *data) 1440 { 1441 /* This will be expanded later to handle different vcap memory layouts */ 1442 memcpy(&field->data, data, sizeof(field->data)); 1443 } 1444 1445 /* Check if the actionfield is already in the rule */ 1446 static bool vcap_actionfield_unique(struct vcap_rule *rule, 1447 enum vcap_action_field act) 1448 { 1449 struct vcap_rule_internal *ri = to_intrule(rule); 1450 const struct vcap_client_actionfield *caf; 1451 1452 list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) 1453 if (caf->ctrl.action == act) 1454 return false; 1455 return true; 1456 } 1457 1458 /* Check if the actionfield is in the actionset */ 1459 static bool vcap_actionfield_match_actionset(struct vcap_rule *rule, 1460 enum vcap_action_field action) 1461 { 1462 enum vcap_actionfield_set actionset = rule->actionset; 1463 struct vcap_rule_internal *ri = to_intrule(rule); 1464 enum vcap_type vt = ri->admin->vtype; 1465 const struct vcap_field *fields; 1466 1467 /* the field is accepted if the rule has no actionset yet */ 1468 if (actionset == VCAP_AFS_NO_VALUE) 1469 return true; 1470 fields = vcap_actionfields(ri->vctrl, vt, actionset); 1471 if (!fields) 1472 return false; 1473 /* if there is a width there is a way */ 1474 return fields[action].width > 0; 1475 } 1476 1477 static int vcap_rule_add_action(struct vcap_rule *rule, 1478 enum vcap_action_field action, 1479 enum vcap_field_type ftype, 1480 struct vcap_client_actionfield_data *data) 1481 { 1482 struct vcap_rule_internal *ri = to_intrule(rule); 1483 struct vcap_client_actionfield *field; 1484 1485 if (!vcap_actionfield_unique(rule, action)) { 1486 pr_warn("%s:%d: actionfield %s is already in the rule\n", 1487 __func__, __LINE__, 1488 vcap_actionfield_name(ri->vctrl, action)); 1489 return -EINVAL; 1490 } 1491 1492 if (!vcap_actionfield_match_actionset(rule, action)) { 1493 pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n", 1494 __func__, __LINE__, 1495 vcap_actionfield_name(ri->vctrl, action)); 1496 return -EINVAL; 1497 } 1498 1499 field = kzalloc(sizeof(*field), GFP_KERNEL); 1500 if (!field) 1501 return -ENOMEM; 1502 field->ctrl.action = action; 1503 field->ctrl.type = ftype; 1504 vcap_copy_from_client_actionfield(rule, field, data); 1505 list_add_tail(&field->ctrl.list, &rule->actionfields); 1506 return 0; 1507 } 1508 1509 static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1, 1510 enum vcap_bit val) 1511 { 1512 switch (val) { 1513 case VCAP_BIT_0: 1514 u1->value = 0; 1515 break; 1516 case VCAP_BIT_1: 1517 u1->value = 1; 1518 break; 1519 case VCAP_BIT_ANY: 1520 u1->value = 0; 1521 break; 1522 } 1523 } 1524 1525 /* Add a bit action with value to the rule */ 1526 int vcap_rule_add_action_bit(struct vcap_rule *rule, 1527 enum vcap_action_field action, 1528 enum vcap_bit val) 1529 { 1530 struct vcap_client_actionfield_data data; 1531 1532 vcap_rule_set_action_bitsize(&data.u1, val); 1533 return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data); 1534 } 1535 EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit); 1536 1537 /* Add a 32 bit action field with value to the rule */ 1538 int vcap_rule_add_action_u32(struct vcap_rule *rule, 1539 enum vcap_action_field action, 1540 u32 value) 1541 { 1542 struct vcap_client_actionfield_data data; 1543 1544 data.u32.value = value; 1545 return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data); 1546 } 1547 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32); 1548 1549 static int vcap_read_counter(struct vcap_rule_internal *ri, 1550 struct vcap_counter *ctr) 1551 { 1552 struct vcap_admin *admin = ri->admin; 1553 1554 ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER, 1555 ri->addr); 1556 ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER, 1557 ri->counter_id, 0); 1558 ctr->value = admin->cache.counter; 1559 ctr->sticky = admin->cache.sticky; 1560 return 0; 1561 } 1562 1563 /* Copy to host byte order */ 1564 void vcap_netbytes_copy(u8 *dst, u8 *src, int count) 1565 { 1566 int idx; 1567 1568 for (idx = 0; idx < count; ++idx, ++dst) 1569 *dst = src[count - idx - 1]; 1570 } 1571 EXPORT_SYMBOL_GPL(vcap_netbytes_copy); 1572 1573 /* Convert validation error code into tc extact error message */ 1574 void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule) 1575 { 1576 switch (vrule->exterr) { 1577 case VCAP_ERR_NONE: 1578 break; 1579 case VCAP_ERR_NO_ADMIN: 1580 NL_SET_ERR_MSG_MOD(fco->common.extack, 1581 "Missing VCAP instance"); 1582 break; 1583 case VCAP_ERR_NO_NETDEV: 1584 NL_SET_ERR_MSG_MOD(fco->common.extack, 1585 "Missing network interface"); 1586 break; 1587 case VCAP_ERR_NO_KEYSET_MATCH: 1588 NL_SET_ERR_MSG_MOD(fco->common.extack, 1589 "No keyset matched the filter keys"); 1590 break; 1591 case VCAP_ERR_NO_ACTIONSET_MATCH: 1592 NL_SET_ERR_MSG_MOD(fco->common.extack, 1593 "No actionset matched the filter actions"); 1594 break; 1595 case VCAP_ERR_NO_PORT_KEYSET_MATCH: 1596 NL_SET_ERR_MSG_MOD(fco->common.extack, 1597 "No port keyset matched the filter keys"); 1598 break; 1599 } 1600 } 1601 EXPORT_SYMBOL_GPL(vcap_set_tc_exterr); 1602 1603 /* Check if this port is already enabled for this VCAP instance */ 1604 static bool vcap_is_enabled(struct vcap_admin *admin, struct net_device *ndev, 1605 unsigned long cookie) 1606 { 1607 struct vcap_enabled_port *eport; 1608 1609 list_for_each_entry(eport, &admin->enabled, list) 1610 if (eport->cookie == cookie || eport->ndev == ndev) 1611 return true; 1612 1613 return false; 1614 } 1615 1616 /* Enable this port for this VCAP instance */ 1617 static int vcap_enable(struct vcap_admin *admin, struct net_device *ndev, 1618 unsigned long cookie) 1619 { 1620 struct vcap_enabled_port *eport; 1621 1622 eport = kzalloc(sizeof(*eport), GFP_KERNEL); 1623 if (!eport) 1624 return -ENOMEM; 1625 1626 eport->ndev = ndev; 1627 eport->cookie = cookie; 1628 list_add_tail(&eport->list, &admin->enabled); 1629 1630 return 0; 1631 } 1632 1633 /* Disable this port for this VCAP instance */ 1634 static int vcap_disable(struct vcap_admin *admin, struct net_device *ndev, 1635 unsigned long cookie) 1636 { 1637 struct vcap_enabled_port *eport; 1638 1639 list_for_each_entry(eport, &admin->enabled, list) { 1640 if (eport->cookie == cookie && eport->ndev == ndev) { 1641 list_del(&eport->list); 1642 kfree(eport); 1643 return 0; 1644 } 1645 } 1646 1647 return -ENOENT; 1648 } 1649 1650 /* Find the VCAP instance that enabled the port using a specific filter */ 1651 static struct vcap_admin *vcap_find_admin_by_cookie(struct vcap_control *vctrl, 1652 unsigned long cookie) 1653 { 1654 struct vcap_enabled_port *eport; 1655 struct vcap_admin *admin; 1656 1657 list_for_each_entry(admin, &vctrl->list, list) 1658 list_for_each_entry(eport, &admin->enabled, list) 1659 if (eport->cookie == cookie) 1660 return admin; 1661 1662 return NULL; 1663 } 1664 1665 /* Enable/Disable the VCAP instance lookups. Chain id 0 means disable */ 1666 int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev, 1667 int chain_id, unsigned long cookie, bool enable) 1668 { 1669 struct vcap_admin *admin; 1670 int err; 1671 1672 err = vcap_api_check(vctrl); 1673 if (err) 1674 return err; 1675 1676 if (!ndev) 1677 return -ENODEV; 1678 1679 if (chain_id) 1680 admin = vcap_find_admin(vctrl, chain_id); 1681 else 1682 admin = vcap_find_admin_by_cookie(vctrl, cookie); 1683 if (!admin) 1684 return -ENOENT; 1685 1686 /* first instance and first chain */ 1687 if (admin->vinst || chain_id > admin->first_cid) 1688 return -EFAULT; 1689 1690 err = vctrl->ops->enable(ndev, admin, enable); 1691 if (err) 1692 return err; 1693 1694 if (chain_id) { 1695 if (vcap_is_enabled(admin, ndev, cookie)) 1696 return -EADDRINUSE; 1697 mutex_lock(&admin->lock); 1698 vcap_enable(admin, ndev, cookie); 1699 } else { 1700 mutex_lock(&admin->lock); 1701 vcap_disable(admin, ndev, cookie); 1702 } 1703 mutex_unlock(&admin->lock); 1704 1705 return 0; 1706 } 1707 EXPORT_SYMBOL_GPL(vcap_enable_lookups); 1708 1709 /* Set a rule counter id (for certain vcaps only) */ 1710 void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id) 1711 { 1712 struct vcap_rule_internal *ri = to_intrule(rule); 1713 1714 ri->counter_id = counter_id; 1715 } 1716 EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id); 1717 1718 /* Provide all rules via a callback interface */ 1719 int vcap_rule_iter(struct vcap_control *vctrl, 1720 int (*callback)(void *, struct vcap_rule *), void *arg) 1721 { 1722 struct vcap_rule_internal *ri; 1723 struct vcap_admin *admin; 1724 int ret; 1725 1726 ret = vcap_api_check(vctrl); 1727 if (ret) 1728 return ret; 1729 1730 /* Iterate all rules in each VCAP instance */ 1731 list_for_each_entry(admin, &vctrl->list, list) { 1732 list_for_each_entry(ri, &admin->rules, list) { 1733 ret = callback(arg, &ri->data); 1734 if (ret) 1735 return ret; 1736 } 1737 } 1738 1739 return 0; 1740 } 1741 EXPORT_SYMBOL_GPL(vcap_rule_iter); 1742 1743 int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr) 1744 { 1745 struct vcap_rule_internal *ri = to_intrule(rule); 1746 int err; 1747 1748 err = vcap_api_check(ri->vctrl); 1749 if (err) 1750 return err; 1751 if (!ctr) { 1752 pr_err("%s:%d: counter is missing\n", __func__, __LINE__); 1753 return -EINVAL; 1754 } 1755 return vcap_write_counter(ri, ctr); 1756 } 1757 EXPORT_SYMBOL_GPL(vcap_rule_set_counter); 1758 1759 int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr) 1760 { 1761 struct vcap_rule_internal *ri = to_intrule(rule); 1762 int err; 1763 1764 err = vcap_api_check(ri->vctrl); 1765 if (err) 1766 return err; 1767 if (!ctr) { 1768 pr_err("%s:%d: counter is missing\n", __func__, __LINE__); 1769 return -EINVAL; 1770 } 1771 return vcap_read_counter(ri, ctr); 1772 } 1773 EXPORT_SYMBOL_GPL(vcap_rule_get_counter); 1774 1775 #ifdef CONFIG_VCAP_KUNIT_TEST 1776 #include "vcap_api_kunit.c" 1777 #endif 1778