debugfs.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Authors:
  5. * (C) 2015 Pengutronix, Alexander Aring <aar@pengutronix.de>
  6. * Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  7. */
  8. #include <net/6lowpan.h>
  9. #include "6lowpan_i.h"
  10. #define LOWPAN_DEBUGFS_CTX_PFX_NUM_ARGS 8
  11. static struct dentry *lowpan_debugfs;
  12. static int lowpan_ctx_flag_active_set(void *data, u64 val)
  13. {
  14. struct lowpan_iphc_ctx *ctx = data;
  15. if (val != 0 && val != 1)
  16. return -EINVAL;
  17. if (val)
  18. set_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE, &ctx->flags);
  19. else
  20. clear_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE, &ctx->flags);
  21. return 0;
  22. }
  23. static int lowpan_ctx_flag_active_get(void *data, u64 *val)
  24. {
  25. *val = lowpan_iphc_ctx_is_active(data);
  26. return 0;
  27. }
  28. DEFINE_DEBUGFS_ATTRIBUTE(lowpan_ctx_flag_active_fops,
  29. lowpan_ctx_flag_active_get,
  30. lowpan_ctx_flag_active_set, "%llu\n");
  31. static int lowpan_ctx_flag_c_set(void *data, u64 val)
  32. {
  33. struct lowpan_iphc_ctx *ctx = data;
  34. if (val != 0 && val != 1)
  35. return -EINVAL;
  36. if (val)
  37. set_bit(LOWPAN_IPHC_CTX_FLAG_COMPRESSION, &ctx->flags);
  38. else
  39. clear_bit(LOWPAN_IPHC_CTX_FLAG_COMPRESSION, &ctx->flags);
  40. return 0;
  41. }
  42. static int lowpan_ctx_flag_c_get(void *data, u64 *val)
  43. {
  44. *val = lowpan_iphc_ctx_is_compression(data);
  45. return 0;
  46. }
  47. DEFINE_DEBUGFS_ATTRIBUTE(lowpan_ctx_flag_c_fops, lowpan_ctx_flag_c_get,
  48. lowpan_ctx_flag_c_set, "%llu\n");
  49. static int lowpan_ctx_plen_set(void *data, u64 val)
  50. {
  51. struct lowpan_iphc_ctx *ctx = data;
  52. struct lowpan_iphc_ctx_table *t =
  53. container_of(ctx, struct lowpan_iphc_ctx_table, table[ctx->id]);
  54. if (val > 128)
  55. return -EINVAL;
  56. spin_lock_bh(&t->lock);
  57. ctx->plen = val;
  58. spin_unlock_bh(&t->lock);
  59. return 0;
  60. }
  61. static int lowpan_ctx_plen_get(void *data, u64 *val)
  62. {
  63. struct lowpan_iphc_ctx *ctx = data;
  64. struct lowpan_iphc_ctx_table *t =
  65. container_of(ctx, struct lowpan_iphc_ctx_table, table[ctx->id]);
  66. spin_lock_bh(&t->lock);
  67. *val = ctx->plen;
  68. spin_unlock_bh(&t->lock);
  69. return 0;
  70. }
  71. DEFINE_DEBUGFS_ATTRIBUTE(lowpan_ctx_plen_fops, lowpan_ctx_plen_get,
  72. lowpan_ctx_plen_set, "%llu\n");
  73. static int lowpan_ctx_pfx_show(struct seq_file *file, void *offset)
  74. {
  75. struct lowpan_iphc_ctx *ctx = file->private;
  76. struct lowpan_iphc_ctx_table *t =
  77. container_of(ctx, struct lowpan_iphc_ctx_table, table[ctx->id]);
  78. spin_lock_bh(&t->lock);
  79. seq_printf(file, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  80. be16_to_cpu(ctx->pfx.s6_addr16[0]),
  81. be16_to_cpu(ctx->pfx.s6_addr16[1]),
  82. be16_to_cpu(ctx->pfx.s6_addr16[2]),
  83. be16_to_cpu(ctx->pfx.s6_addr16[3]),
  84. be16_to_cpu(ctx->pfx.s6_addr16[4]),
  85. be16_to_cpu(ctx->pfx.s6_addr16[5]),
  86. be16_to_cpu(ctx->pfx.s6_addr16[6]),
  87. be16_to_cpu(ctx->pfx.s6_addr16[7]));
  88. spin_unlock_bh(&t->lock);
  89. return 0;
  90. }
  91. static int lowpan_ctx_pfx_open(struct inode *inode, struct file *file)
  92. {
  93. return single_open(file, lowpan_ctx_pfx_show, inode->i_private);
  94. }
  95. static ssize_t lowpan_ctx_pfx_write(struct file *fp,
  96. const char __user *user_buf, size_t count,
  97. loff_t *ppos)
  98. {
  99. char buf[128] = {};
  100. struct seq_file *file = fp->private_data;
  101. struct lowpan_iphc_ctx *ctx = file->private;
  102. struct lowpan_iphc_ctx_table *t =
  103. container_of(ctx, struct lowpan_iphc_ctx_table, table[ctx->id]);
  104. int status = count, n, i;
  105. unsigned int addr[8];
  106. if (copy_from_user(&buf, user_buf, min_t(size_t, sizeof(buf) - 1,
  107. count))) {
  108. status = -EFAULT;
  109. goto out;
  110. }
  111. n = sscanf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
  112. &addr[0], &addr[1], &addr[2], &addr[3], &addr[4],
  113. &addr[5], &addr[6], &addr[7]);
  114. if (n != LOWPAN_DEBUGFS_CTX_PFX_NUM_ARGS) {
  115. status = -EINVAL;
  116. goto out;
  117. }
  118. spin_lock_bh(&t->lock);
  119. for (i = 0; i < 8; i++)
  120. ctx->pfx.s6_addr16[i] = cpu_to_be16(addr[i] & 0xffff);
  121. spin_unlock_bh(&t->lock);
  122. out:
  123. return status;
  124. }
  125. static const struct file_operations lowpan_ctx_pfx_fops = {
  126. .open = lowpan_ctx_pfx_open,
  127. .read = seq_read,
  128. .write = lowpan_ctx_pfx_write,
  129. .llseek = seq_lseek,
  130. .release = single_release,
  131. };
  132. static void lowpan_dev_debugfs_ctx_init(struct net_device *dev,
  133. struct dentry *ctx, u8 id)
  134. {
  135. struct lowpan_dev *ldev = lowpan_dev(dev);
  136. struct dentry *root;
  137. char buf[32];
  138. if (WARN_ON_ONCE(id >= LOWPAN_IPHC_CTX_TABLE_SIZE))
  139. return;
  140. sprintf(buf, "%d", id);
  141. root = debugfs_create_dir(buf, ctx);
  142. debugfs_create_file("active", 0644, root, &ldev->ctx.table[id],
  143. &lowpan_ctx_flag_active_fops);
  144. debugfs_create_file("compression", 0644, root, &ldev->ctx.table[id],
  145. &lowpan_ctx_flag_c_fops);
  146. debugfs_create_file("prefix", 0644, root, &ldev->ctx.table[id],
  147. &lowpan_ctx_pfx_fops);
  148. debugfs_create_file("prefix_len", 0644, root, &ldev->ctx.table[id],
  149. &lowpan_ctx_plen_fops);
  150. }
  151. static int lowpan_context_show(struct seq_file *file, void *offset)
  152. {
  153. struct lowpan_iphc_ctx_table *t = file->private;
  154. int i;
  155. seq_printf(file, "%3s|%-43s|%c\n", "cid", "prefix", 'C');
  156. seq_puts(file, "-------------------------------------------------\n");
  157. spin_lock_bh(&t->lock);
  158. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) {
  159. if (!lowpan_iphc_ctx_is_active(&t->table[i]))
  160. continue;
  161. seq_printf(file, "%3d|%39pI6c/%-3d|%d\n", t->table[i].id,
  162. &t->table[i].pfx, t->table[i].plen,
  163. lowpan_iphc_ctx_is_compression(&t->table[i]));
  164. }
  165. spin_unlock_bh(&t->lock);
  166. return 0;
  167. }
  168. DEFINE_SHOW_ATTRIBUTE(lowpan_context);
  169. static int lowpan_short_addr_get(void *data, u64 *val)
  170. {
  171. struct wpan_dev *wdev = data;
  172. rtnl_lock();
  173. *val = le16_to_cpu(wdev->short_addr);
  174. rtnl_unlock();
  175. return 0;
  176. }
  177. DEFINE_DEBUGFS_ATTRIBUTE(lowpan_short_addr_fops, lowpan_short_addr_get, NULL,
  178. "0x%04llx\n");
  179. static void lowpan_dev_debugfs_802154_init(const struct net_device *dev,
  180. struct lowpan_dev *ldev)
  181. {
  182. struct dentry *root;
  183. if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154))
  184. return;
  185. root = debugfs_create_dir("ieee802154", ldev->iface_debugfs);
  186. debugfs_create_file("short_addr", 0444, root,
  187. lowpan_802154_dev(dev)->wdev->ieee802154_ptr,
  188. &lowpan_short_addr_fops);
  189. }
  190. void lowpan_dev_debugfs_init(struct net_device *dev)
  191. {
  192. struct lowpan_dev *ldev = lowpan_dev(dev);
  193. struct dentry *contexts;
  194. int i;
  195. /* creating the root */
  196. ldev->iface_debugfs = debugfs_create_dir(dev->name, lowpan_debugfs);
  197. contexts = debugfs_create_dir("contexts", ldev->iface_debugfs);
  198. debugfs_create_file("show", 0644, contexts, &lowpan_dev(dev)->ctx,
  199. &lowpan_context_fops);
  200. for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
  201. lowpan_dev_debugfs_ctx_init(dev, contexts, i);
  202. lowpan_dev_debugfs_802154_init(dev, ldev);
  203. }
  204. void lowpan_dev_debugfs_exit(struct net_device *dev)
  205. {
  206. debugfs_remove_recursive(lowpan_dev(dev)->iface_debugfs);
  207. }
  208. void __init lowpan_debugfs_init(void)
  209. {
  210. lowpan_debugfs = debugfs_create_dir("6lowpan", NULL);
  211. }
  212. void lowpan_debugfs_exit(void)
  213. {
  214. debugfs_remove_recursive(lowpan_debugfs);
  215. }