vcap_api_debugfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Microchip VCAP API debug file system support
  3. *
  4. * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
  5. *
  6. */
  7. #include "vcap_api_private.h"
  8. #include "vcap_api_debugfs.h"
  9. struct vcap_admin_debugfs_info {
  10. struct vcap_control *vctrl;
  11. struct vcap_admin *admin;
  12. };
  13. struct vcap_port_debugfs_info {
  14. struct vcap_control *vctrl;
  15. struct net_device *ndev;
  16. };
  17. /* Dump the keyfields value and mask values */
  18. static void vcap_debugfs_show_rule_keyfield(struct vcap_control *vctrl,
  19. struct vcap_output_print *out,
  20. enum vcap_key_field key,
  21. const struct vcap_field *keyfield,
  22. struct vcap_client_keyfield_data *data)
  23. {
  24. bool hex = false;
  25. u8 *value, *mask;
  26. int idx, bytes;
  27. out->prf(out->dst, " %s: W%d: ", vcap_keyfield_name(vctrl, key),
  28. keyfield[key].width);
  29. switch (keyfield[key].type) {
  30. case VCAP_FIELD_BIT:
  31. out->prf(out->dst, "%d/%d", data->u1.value, data->u1.mask);
  32. break;
  33. case VCAP_FIELD_U32:
  34. value = (u8 *)(&data->u32.value);
  35. mask = (u8 *)(&data->u32.mask);
  36. if (key == VCAP_KF_L3_IP4_SIP || key == VCAP_KF_L3_IP4_DIP) {
  37. out->prf(out->dst, "%pI4h/%pI4h", &data->u32.value,
  38. &data->u32.mask);
  39. } else if (key == VCAP_KF_ETYPE ||
  40. key == VCAP_KF_IF_IGR_PORT_MASK ||
  41. key == VCAP_KF_IF_EGR_PORT_MASK) {
  42. hex = true;
  43. } else {
  44. u32 fmsk = (1 << keyfield[key].width) - 1;
  45. if (keyfield[key].width == 32)
  46. fmsk = ~0;
  47. out->prf(out->dst, "%u/%u", data->u32.value & fmsk,
  48. data->u32.mask & fmsk);
  49. }
  50. break;
  51. case VCAP_FIELD_U48:
  52. value = data->u48.value;
  53. mask = data->u48.mask;
  54. if (key == VCAP_KF_L2_SMAC || key == VCAP_KF_L2_DMAC)
  55. out->prf(out->dst, "%pMR/%pMR", data->u48.value,
  56. data->u48.mask);
  57. else
  58. hex = true;
  59. break;
  60. case VCAP_FIELD_U56:
  61. value = data->u56.value;
  62. mask = data->u56.mask;
  63. hex = true;
  64. break;
  65. case VCAP_FIELD_U64:
  66. value = data->u64.value;
  67. mask = data->u64.mask;
  68. hex = true;
  69. break;
  70. case VCAP_FIELD_U72:
  71. value = data->u72.value;
  72. mask = data->u72.mask;
  73. hex = true;
  74. break;
  75. case VCAP_FIELD_U112:
  76. value = data->u112.value;
  77. mask = data->u112.mask;
  78. hex = true;
  79. break;
  80. case VCAP_FIELD_U128:
  81. value = data->u128.value;
  82. mask = data->u128.mask;
  83. if (key == VCAP_KF_L3_IP6_SIP || key == VCAP_KF_L3_IP6_DIP) {
  84. u8 nvalue[16], nmask[16];
  85. vcap_netbytes_copy(nvalue, data->u128.value,
  86. sizeof(nvalue));
  87. vcap_netbytes_copy(nmask, data->u128.mask,
  88. sizeof(nmask));
  89. out->prf(out->dst, "%pI6/%pI6", nvalue, nmask);
  90. } else {
  91. hex = true;
  92. }
  93. break;
  94. }
  95. if (hex) {
  96. bytes = DIV_ROUND_UP(keyfield[key].width, BITS_PER_BYTE);
  97. out->prf(out->dst, "0x");
  98. for (idx = 0; idx < bytes; ++idx)
  99. out->prf(out->dst, "%02x", value[bytes - idx - 1]);
  100. out->prf(out->dst, "/0x");
  101. for (idx = 0; idx < bytes; ++idx)
  102. out->prf(out->dst, "%02x", mask[bytes - idx - 1]);
  103. }
  104. out->prf(out->dst, "\n");
  105. }
  106. static void
  107. vcap_debugfs_show_rule_actionfield(struct vcap_control *vctrl,
  108. struct vcap_output_print *out,
  109. enum vcap_action_field action,
  110. const struct vcap_field *actionfield,
  111. u8 *value)
  112. {
  113. bool hex = false;
  114. int idx, bytes;
  115. u32 fmsk, val;
  116. out->prf(out->dst, " %s: W%d: ",
  117. vcap_actionfield_name(vctrl, action),
  118. actionfield[action].width);
  119. switch (actionfield[action].type) {
  120. case VCAP_FIELD_BIT:
  121. out->prf(out->dst, "%d", value[0]);
  122. break;
  123. case VCAP_FIELD_U32:
  124. fmsk = (1 << actionfield[action].width) - 1;
  125. val = *(u32 *)value;
  126. out->prf(out->dst, "%u", val & fmsk);
  127. break;
  128. case VCAP_FIELD_U48:
  129. case VCAP_FIELD_U56:
  130. case VCAP_FIELD_U64:
  131. case VCAP_FIELD_U72:
  132. case VCAP_FIELD_U112:
  133. case VCAP_FIELD_U128:
  134. hex = true;
  135. break;
  136. }
  137. if (hex) {
  138. bytes = DIV_ROUND_UP(actionfield[action].width, BITS_PER_BYTE);
  139. out->prf(out->dst, "0x");
  140. for (idx = 0; idx < bytes; ++idx)
  141. out->prf(out->dst, "%02x", value[bytes - idx - 1]);
  142. }
  143. out->prf(out->dst, "\n");
  144. }
  145. static int vcap_debugfs_show_keysets(struct vcap_rule_internal *ri,
  146. struct vcap_output_print *out)
  147. {
  148. struct vcap_admin *admin = ri->admin;
  149. enum vcap_keyfield_set keysets[10];
  150. struct vcap_keyset_list matches;
  151. int err;
  152. matches.keysets = keysets;
  153. matches.cnt = 0;
  154. matches.max = ARRAY_SIZE(keysets);
  155. if (ri->state == VCAP_RS_DISABLED)
  156. err = vcap_rule_get_keysets(ri, &matches);
  157. else
  158. err = vcap_find_keystream_keysets(ri->vctrl, admin->vtype,
  159. admin->cache.keystream,
  160. admin->cache.maskstream,
  161. false, 0, &matches);
  162. if (err) {
  163. pr_err("%s:%d: could not find valid keysets: %d\n",
  164. __func__, __LINE__, err);
  165. return err;
  166. }
  167. out->prf(out->dst, " keysets:");
  168. for (int idx = 0; idx < matches.cnt; ++idx)
  169. out->prf(out->dst, " %s",
  170. vcap_keyset_name(ri->vctrl, matches.keysets[idx]));
  171. out->prf(out->dst, "\n");
  172. return 0;
  173. }
  174. static int vcap_debugfs_show_rule_keyset(struct vcap_rule_internal *ri,
  175. struct vcap_output_print *out)
  176. {
  177. struct vcap_control *vctrl = ri->vctrl;
  178. struct vcap_admin *admin = ri->admin;
  179. const struct vcap_field *keyfield;
  180. struct vcap_client_keyfield *ckf;
  181. vcap_debugfs_show_keysets(ri, out);
  182. out->prf(out->dst, " keyset_sw: %d\n", ri->keyset_sw);
  183. out->prf(out->dst, " keyset_sw_regs: %d\n", ri->keyset_sw_regs);
  184. list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
  185. keyfield = vcap_keyfields(vctrl, admin->vtype, ri->data.keyset);
  186. vcap_debugfs_show_rule_keyfield(vctrl, out, ckf->ctrl.key,
  187. keyfield, &ckf->data);
  188. }
  189. return 0;
  190. }
  191. static int vcap_debugfs_show_rule_actionset(struct vcap_rule_internal *ri,
  192. struct vcap_output_print *out)
  193. {
  194. struct vcap_control *vctrl = ri->vctrl;
  195. struct vcap_admin *admin = ri->admin;
  196. const struct vcap_field *actionfield;
  197. struct vcap_client_actionfield *caf;
  198. out->prf(out->dst, " actionset: %s\n",
  199. vcap_actionset_name(vctrl, ri->data.actionset));
  200. out->prf(out->dst, " actionset_sw: %d\n", ri->actionset_sw);
  201. out->prf(out->dst, " actionset_sw_regs: %d\n", ri->actionset_sw_regs);
  202. list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
  203. actionfield = vcap_actionfields(vctrl, admin->vtype,
  204. ri->data.actionset);
  205. vcap_debugfs_show_rule_actionfield(vctrl, out, caf->ctrl.action,
  206. actionfield,
  207. &caf->data.u1.value);
  208. }
  209. return 0;
  210. }
  211. static void vcap_show_admin_rule(struct vcap_control *vctrl,
  212. struct vcap_admin *admin,
  213. struct vcap_output_print *out,
  214. struct vcap_rule_internal *ri)
  215. {
  216. ri->counter.value = admin->cache.counter;
  217. ri->counter.sticky = admin->cache.sticky;
  218. out->prf(out->dst,
  219. "rule: %u, addr: [%d,%d], X%d, ctr[%d]: %d, hit: %d\n",
  220. ri->data.id, ri->addr, ri->addr + ri->size - 1, ri->size,
  221. ri->counter_id, ri->counter.value, ri->counter.sticky);
  222. out->prf(out->dst, " chain_id: %d\n", ri->data.vcap_chain_id);
  223. out->prf(out->dst, " user: %d\n", ri->data.user);
  224. out->prf(out->dst, " priority: %d\n", ri->data.priority);
  225. out->prf(out->dst, " state: ");
  226. switch (ri->state) {
  227. case VCAP_RS_PERMANENT:
  228. out->prf(out->dst, "permanent\n");
  229. break;
  230. case VCAP_RS_DISABLED:
  231. out->prf(out->dst, "disabled\n");
  232. break;
  233. case VCAP_RS_ENABLED:
  234. out->prf(out->dst, "enabled\n");
  235. break;
  236. }
  237. vcap_debugfs_show_rule_keyset(ri, out);
  238. vcap_debugfs_show_rule_actionset(ri, out);
  239. }
  240. static void vcap_show_admin_info(struct vcap_control *vctrl,
  241. struct vcap_admin *admin,
  242. struct vcap_output_print *out)
  243. {
  244. const struct vcap_info *vcap = &vctrl->vcaps[admin->vtype];
  245. out->prf(out->dst, "name: %s\n", vcap->name);
  246. out->prf(out->dst, "rows: %d\n", vcap->rows);
  247. out->prf(out->dst, "sw_count: %d\n", vcap->sw_count);
  248. out->prf(out->dst, "sw_width: %d\n", vcap->sw_width);
  249. out->prf(out->dst, "sticky_width: %d\n", vcap->sticky_width);
  250. out->prf(out->dst, "act_width: %d\n", vcap->act_width);
  251. out->prf(out->dst, "default_cnt: %d\n", vcap->default_cnt);
  252. out->prf(out->dst, "require_cnt_dis: %d\n", vcap->require_cnt_dis);
  253. out->prf(out->dst, "version: %d\n", vcap->version);
  254. out->prf(out->dst, "vtype: %d\n", admin->vtype);
  255. out->prf(out->dst, "vinst: %d\n", admin->vinst);
  256. out->prf(out->dst, "ingress: %d\n", admin->ingress);
  257. out->prf(out->dst, "first_cid: %d\n", admin->first_cid);
  258. out->prf(out->dst, "last_cid: %d\n", admin->last_cid);
  259. out->prf(out->dst, "lookups: %d\n", admin->lookups);
  260. out->prf(out->dst, "first_valid_addr: %d\n", admin->first_valid_addr);
  261. out->prf(out->dst, "last_valid_addr: %d\n", admin->last_valid_addr);
  262. out->prf(out->dst, "last_used_addr: %d\n", admin->last_used_addr);
  263. }
  264. static int vcap_show_admin(struct vcap_control *vctrl,
  265. struct vcap_admin *admin,
  266. struct vcap_output_print *out)
  267. {
  268. struct vcap_rule_internal *elem;
  269. struct vcap_rule *vrule;
  270. int ret = 0;
  271. vcap_show_admin_info(vctrl, admin, out);
  272. list_for_each_entry(elem, &admin->rules, list) {
  273. vrule = vcap_decode_rule(elem);
  274. if (IS_ERR(vrule)) {
  275. ret = PTR_ERR(vrule);
  276. break;
  277. }
  278. out->prf(out->dst, "\n");
  279. vcap_show_admin_rule(vctrl, admin, out, to_intrule(vrule));
  280. vcap_free_rule(vrule);
  281. }
  282. return ret;
  283. }
  284. static int vcap_show_admin_raw(struct vcap_control *vctrl,
  285. struct vcap_admin *admin,
  286. struct vcap_output_print *out)
  287. {
  288. enum vcap_keyfield_set keysets[10];
  289. enum vcap_type vt = admin->vtype;
  290. struct vcap_keyset_list kslist;
  291. struct vcap_rule_internal *ri;
  292. const struct vcap_set *info;
  293. int addr, idx;
  294. int ret;
  295. if (list_empty(&admin->rules))
  296. return 0;
  297. ret = vcap_api_check(vctrl);
  298. if (ret)
  299. return ret;
  300. ri = list_first_entry(&admin->rules, struct vcap_rule_internal, list);
  301. /* Go from higher to lower addresses searching for a keyset */
  302. kslist.keysets = keysets;
  303. kslist.max = ARRAY_SIZE(keysets);
  304. for (addr = admin->last_valid_addr; addr >= admin->first_valid_addr;
  305. --addr) {
  306. kslist.cnt = 0;
  307. ret = vcap_addr_keysets(vctrl, ri->ndev, admin, addr, &kslist);
  308. if (ret < 0)
  309. continue;
  310. info = vcap_keyfieldset(vctrl, vt, kslist.keysets[0]);
  311. if (!info)
  312. continue;
  313. if (addr % info->sw_per_item) {
  314. pr_info("addr: %d X%d error rule, keyset: %s\n",
  315. addr,
  316. info->sw_per_item,
  317. vcap_keyset_name(vctrl, kslist.keysets[0]));
  318. } else {
  319. out->prf(out->dst, " addr: %d, X%d rule, keysets:",
  320. addr,
  321. info->sw_per_item);
  322. for (idx = 0; idx < kslist.cnt; ++idx)
  323. out->prf(out->dst, " %s",
  324. vcap_keyset_name(vctrl,
  325. kslist.keysets[idx]));
  326. out->prf(out->dst, "\n");
  327. }
  328. }
  329. return 0;
  330. }
  331. /* Show the port configuration and status */
  332. static int vcap_port_debugfs_show(struct seq_file *m, void *unused)
  333. {
  334. struct vcap_port_debugfs_info *info = m->private;
  335. struct vcap_admin *admin;
  336. struct vcap_output_print out = {
  337. .prf = (void *)seq_printf,
  338. .dst = m,
  339. };
  340. list_for_each_entry(admin, &info->vctrl->list, list) {
  341. if (admin->vinst)
  342. continue;
  343. info->vctrl->ops->port_info(info->ndev, admin, &out);
  344. }
  345. return 0;
  346. }
  347. DEFINE_SHOW_ATTRIBUTE(vcap_port_debugfs);
  348. void vcap_port_debugfs(struct device *dev, struct dentry *parent,
  349. struct vcap_control *vctrl,
  350. struct net_device *ndev)
  351. {
  352. struct vcap_port_debugfs_info *info;
  353. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  354. if (!info)
  355. return;
  356. info->vctrl = vctrl;
  357. info->ndev = ndev;
  358. debugfs_create_file(netdev_name(ndev), 0444, parent, info,
  359. &vcap_port_debugfs_fops);
  360. }
  361. EXPORT_SYMBOL_GPL(vcap_port_debugfs);
  362. /* Show the full VCAP instance data (rules with all fields) */
  363. static int vcap_debugfs_show(struct seq_file *m, void *unused)
  364. {
  365. struct vcap_admin_debugfs_info *info = m->private;
  366. struct vcap_output_print out = {
  367. .prf = (void *)seq_printf,
  368. .dst = m,
  369. };
  370. int ret;
  371. mutex_lock(&info->admin->lock);
  372. ret = vcap_show_admin(info->vctrl, info->admin, &out);
  373. mutex_unlock(&info->admin->lock);
  374. return ret;
  375. }
  376. DEFINE_SHOW_ATTRIBUTE(vcap_debugfs);
  377. /* Show the raw VCAP instance data (rules with address info) */
  378. static int vcap_raw_debugfs_show(struct seq_file *m, void *unused)
  379. {
  380. struct vcap_admin_debugfs_info *info = m->private;
  381. struct vcap_output_print out = {
  382. .prf = (void *)seq_printf,
  383. .dst = m,
  384. };
  385. int ret;
  386. mutex_lock(&info->admin->lock);
  387. ret = vcap_show_admin_raw(info->vctrl, info->admin, &out);
  388. mutex_unlock(&info->admin->lock);
  389. return ret;
  390. }
  391. DEFINE_SHOW_ATTRIBUTE(vcap_raw_debugfs);
  392. struct dentry *vcap_debugfs(struct device *dev, struct dentry *parent,
  393. struct vcap_control *vctrl)
  394. {
  395. struct vcap_admin_debugfs_info *info;
  396. struct vcap_admin *admin;
  397. struct dentry *dir;
  398. char name[50];
  399. dir = debugfs_create_dir("vcaps", parent);
  400. if (PTR_ERR_OR_ZERO(dir))
  401. return NULL;
  402. list_for_each_entry(admin, &vctrl->list, list) {
  403. sprintf(name, "raw_%s_%d", vctrl->vcaps[admin->vtype].name,
  404. admin->vinst);
  405. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  406. if (!info)
  407. return NULL;
  408. info->vctrl = vctrl;
  409. info->admin = admin;
  410. debugfs_create_file(name, 0444, dir, info,
  411. &vcap_raw_debugfs_fops);
  412. sprintf(name, "%s_%d", vctrl->vcaps[admin->vtype].name,
  413. admin->vinst);
  414. debugfs_create_file(name, 0444, dir, info, &vcap_debugfs_fops);
  415. }
  416. return dir;
  417. }
  418. EXPORT_SYMBOL_GPL(vcap_debugfs);
  419. #ifdef CONFIG_VCAP_KUNIT_TEST
  420. #include "vcap_api_debugfs_kunit.c"
  421. #endif