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