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