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