xref: /openbmc/linux/net/6lowpan/debugfs.c (revision 1802d0beecafe581ad584634ba92f8a471d8a63a)
1*1802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2*1802d0beSThomas Gleixner /*
3b1815fd9SAlexander Aring  *
4b1815fd9SAlexander Aring  * Authors:
5b1815fd9SAlexander Aring  * (C) 2015 Pengutronix, Alexander Aring <aar@pengutronix.de>
6b1815fd9SAlexander Aring  * Copyright (c)  2015 Nordic Semiconductor. All Rights Reserved.
7b1815fd9SAlexander Aring  */
8b1815fd9SAlexander Aring 
9b1815fd9SAlexander Aring #include <net/6lowpan.h>
10b1815fd9SAlexander Aring 
11b1815fd9SAlexander Aring #include "6lowpan_i.h"
12b1815fd9SAlexander Aring 
135609c185SAlexander Aring #define LOWPAN_DEBUGFS_CTX_PFX_NUM_ARGS	8
145609c185SAlexander Aring 
15b1815fd9SAlexander Aring static struct dentry *lowpan_debugfs;
16b1815fd9SAlexander Aring 
175609c185SAlexander Aring static int lowpan_ctx_flag_active_set(void *data, u64 val)
185609c185SAlexander Aring {
195609c185SAlexander Aring 	struct lowpan_iphc_ctx *ctx = data;
205609c185SAlexander Aring 
215609c185SAlexander Aring 	if (val != 0 && val != 1)
225609c185SAlexander Aring 		return -EINVAL;
235609c185SAlexander Aring 
245609c185SAlexander Aring 	if (val)
255609c185SAlexander Aring 		set_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE, &ctx->flags);
265609c185SAlexander Aring 	else
275609c185SAlexander Aring 		clear_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE, &ctx->flags);
285609c185SAlexander Aring 
295609c185SAlexander Aring 	return 0;
305609c185SAlexander Aring }
315609c185SAlexander Aring 
325609c185SAlexander Aring static int lowpan_ctx_flag_active_get(void *data, u64 *val)
335609c185SAlexander Aring {
345609c185SAlexander Aring 	*val = lowpan_iphc_ctx_is_active(data);
355609c185SAlexander Aring 	return 0;
365609c185SAlexander Aring }
375609c185SAlexander Aring 
385e053534SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(lowpan_ctx_flag_active_fops,
395609c185SAlexander Aring 			 lowpan_ctx_flag_active_get,
405609c185SAlexander Aring 			 lowpan_ctx_flag_active_set, "%llu\n");
415609c185SAlexander Aring 
425609c185SAlexander Aring static int lowpan_ctx_flag_c_set(void *data, u64 val)
435609c185SAlexander Aring {
445609c185SAlexander Aring 	struct lowpan_iphc_ctx *ctx = data;
455609c185SAlexander Aring 
465609c185SAlexander Aring 	if (val != 0 && val != 1)
475609c185SAlexander Aring 		return -EINVAL;
485609c185SAlexander Aring 
495609c185SAlexander Aring 	if (val)
505609c185SAlexander Aring 		set_bit(LOWPAN_IPHC_CTX_FLAG_COMPRESSION, &ctx->flags);
515609c185SAlexander Aring 	else
525609c185SAlexander Aring 		clear_bit(LOWPAN_IPHC_CTX_FLAG_COMPRESSION, &ctx->flags);
535609c185SAlexander Aring 
545609c185SAlexander Aring 	return 0;
555609c185SAlexander Aring }
565609c185SAlexander Aring 
575609c185SAlexander Aring static int lowpan_ctx_flag_c_get(void *data, u64 *val)
585609c185SAlexander Aring {
595609c185SAlexander Aring 	*val = lowpan_iphc_ctx_is_compression(data);
605609c185SAlexander Aring 	return 0;
615609c185SAlexander Aring }
625609c185SAlexander Aring 
635e053534SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(lowpan_ctx_flag_c_fops, lowpan_ctx_flag_c_get,
645609c185SAlexander Aring 			 lowpan_ctx_flag_c_set, "%llu\n");
655609c185SAlexander Aring 
665609c185SAlexander Aring static int lowpan_ctx_plen_set(void *data, u64 val)
675609c185SAlexander Aring {
685609c185SAlexander Aring 	struct lowpan_iphc_ctx *ctx = data;
695609c185SAlexander Aring 	struct lowpan_iphc_ctx_table *t =
705609c185SAlexander Aring 		container_of(ctx, struct lowpan_iphc_ctx_table, table[ctx->id]);
715609c185SAlexander Aring 
725609c185SAlexander Aring 	if (val > 128)
735609c185SAlexander Aring 		return -EINVAL;
745609c185SAlexander Aring 
755609c185SAlexander Aring 	spin_lock_bh(&t->lock);
765609c185SAlexander Aring 	ctx->plen = val;
775609c185SAlexander Aring 	spin_unlock_bh(&t->lock);
785609c185SAlexander Aring 
795609c185SAlexander Aring 	return 0;
805609c185SAlexander Aring }
815609c185SAlexander Aring 
825609c185SAlexander Aring static int lowpan_ctx_plen_get(void *data, u64 *val)
835609c185SAlexander Aring {
845609c185SAlexander Aring 	struct lowpan_iphc_ctx *ctx = data;
855609c185SAlexander Aring 	struct lowpan_iphc_ctx_table *t =
865609c185SAlexander Aring 		container_of(ctx, struct lowpan_iphc_ctx_table, table[ctx->id]);
875609c185SAlexander Aring 
885609c185SAlexander Aring 	spin_lock_bh(&t->lock);
895609c185SAlexander Aring 	*val = ctx->plen;
905609c185SAlexander Aring 	spin_unlock_bh(&t->lock);
915609c185SAlexander Aring 	return 0;
925609c185SAlexander Aring }
935609c185SAlexander Aring 
945e053534SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(lowpan_ctx_plen_fops, lowpan_ctx_plen_get,
955609c185SAlexander Aring 			 lowpan_ctx_plen_set, "%llu\n");
965609c185SAlexander Aring 
975609c185SAlexander Aring static int lowpan_ctx_pfx_show(struct seq_file *file, void *offset)
985609c185SAlexander Aring {
995609c185SAlexander Aring 	struct lowpan_iphc_ctx *ctx = file->private;
1005609c185SAlexander Aring 	struct lowpan_iphc_ctx_table *t =
1015609c185SAlexander Aring 		container_of(ctx, struct lowpan_iphc_ctx_table, table[ctx->id]);
1025609c185SAlexander Aring 
1035609c185SAlexander Aring 	spin_lock_bh(&t->lock);
1045609c185SAlexander Aring 	seq_printf(file, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1055609c185SAlexander Aring 		   be16_to_cpu(ctx->pfx.s6_addr16[0]),
1065609c185SAlexander Aring 		   be16_to_cpu(ctx->pfx.s6_addr16[1]),
1075609c185SAlexander Aring 		   be16_to_cpu(ctx->pfx.s6_addr16[2]),
1085609c185SAlexander Aring 		   be16_to_cpu(ctx->pfx.s6_addr16[3]),
1095609c185SAlexander Aring 		   be16_to_cpu(ctx->pfx.s6_addr16[4]),
1105609c185SAlexander Aring 		   be16_to_cpu(ctx->pfx.s6_addr16[5]),
1115609c185SAlexander Aring 		   be16_to_cpu(ctx->pfx.s6_addr16[6]),
1125609c185SAlexander Aring 		   be16_to_cpu(ctx->pfx.s6_addr16[7]));
1135609c185SAlexander Aring 	spin_unlock_bh(&t->lock);
1145609c185SAlexander Aring 
1155609c185SAlexander Aring 	return 0;
1165609c185SAlexander Aring }
1175609c185SAlexander Aring 
1185609c185SAlexander Aring static int lowpan_ctx_pfx_open(struct inode *inode, struct file *file)
1195609c185SAlexander Aring {
1205609c185SAlexander Aring 	return single_open(file, lowpan_ctx_pfx_show, inode->i_private);
1215609c185SAlexander Aring }
1225609c185SAlexander Aring 
1235609c185SAlexander Aring static ssize_t lowpan_ctx_pfx_write(struct file *fp,
1245609c185SAlexander Aring 				    const char __user *user_buf, size_t count,
1255609c185SAlexander Aring 				    loff_t *ppos)
1265609c185SAlexander Aring {
1275609c185SAlexander Aring 	char buf[128] = {};
1285609c185SAlexander Aring 	struct seq_file *file = fp->private_data;
1295609c185SAlexander Aring 	struct lowpan_iphc_ctx *ctx = file->private;
1305609c185SAlexander Aring 	struct lowpan_iphc_ctx_table *t =
1315609c185SAlexander Aring 		container_of(ctx, struct lowpan_iphc_ctx_table, table[ctx->id]);
1325609c185SAlexander Aring 	int status = count, n, i;
1335609c185SAlexander Aring 	unsigned int addr[8];
1345609c185SAlexander Aring 
1355609c185SAlexander Aring 	if (copy_from_user(&buf, user_buf, min_t(size_t, sizeof(buf) - 1,
1365609c185SAlexander Aring 						 count))) {
1375609c185SAlexander Aring 		status = -EFAULT;
1385609c185SAlexander Aring 		goto out;
1395609c185SAlexander Aring 	}
1405609c185SAlexander Aring 
1415609c185SAlexander Aring 	n = sscanf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
1425609c185SAlexander Aring 		   &addr[0], &addr[1], &addr[2], &addr[3], &addr[4],
1435609c185SAlexander Aring 		   &addr[5], &addr[6], &addr[7]);
1445609c185SAlexander Aring 	if (n != LOWPAN_DEBUGFS_CTX_PFX_NUM_ARGS) {
1455609c185SAlexander Aring 		status = -EINVAL;
1465609c185SAlexander Aring 		goto out;
1475609c185SAlexander Aring 	}
1485609c185SAlexander Aring 
1495609c185SAlexander Aring 	spin_lock_bh(&t->lock);
1505609c185SAlexander Aring 	for (i = 0; i < 8; i++)
1515609c185SAlexander Aring 		ctx->pfx.s6_addr16[i] = cpu_to_be16(addr[i] & 0xffff);
1525609c185SAlexander Aring 	spin_unlock_bh(&t->lock);
1535609c185SAlexander Aring 
1545609c185SAlexander Aring out:
1555609c185SAlexander Aring 	return status;
1565609c185SAlexander Aring }
1575609c185SAlexander Aring 
1586aaf37b4SAlexander Aring static const struct file_operations lowpan_ctx_pfx_fops = {
1595609c185SAlexander Aring 	.open		= lowpan_ctx_pfx_open,
1605609c185SAlexander Aring 	.read		= seq_read,
1615609c185SAlexander Aring 	.write		= lowpan_ctx_pfx_write,
1625609c185SAlexander Aring 	.llseek		= seq_lseek,
1635609c185SAlexander Aring 	.release	= single_release,
1645609c185SAlexander Aring };
1655609c185SAlexander Aring 
1665609c185SAlexander Aring static int lowpan_dev_debugfs_ctx_init(struct net_device *dev,
1675609c185SAlexander Aring 				       struct dentry *ctx, u8 id)
1685609c185SAlexander Aring {
1692e4d60cbSAlexander Aring 	struct lowpan_dev *ldev = lowpan_dev(dev);
1705609c185SAlexander Aring 	struct dentry *dentry, *root;
1715609c185SAlexander Aring 	char buf[32];
1725609c185SAlexander Aring 
1735609c185SAlexander Aring 	WARN_ON_ONCE(id > LOWPAN_IPHC_CTX_TABLE_SIZE);
1745609c185SAlexander Aring 
1755609c185SAlexander Aring 	sprintf(buf, "%d", id);
1765609c185SAlexander Aring 
1775609c185SAlexander Aring 	root = debugfs_create_dir(buf, ctx);
1785609c185SAlexander Aring 	if (!root)
1795609c185SAlexander Aring 		return -EINVAL;
1805609c185SAlexander Aring 
1815e053534SYueHaibing 	dentry = debugfs_create_file_unsafe("active", 0644, root,
1822e4d60cbSAlexander Aring 					    &ldev->ctx.table[id],
1835609c185SAlexander Aring 					    &lowpan_ctx_flag_active_fops);
1845609c185SAlexander Aring 	if (!dentry)
1855609c185SAlexander Aring 		return -EINVAL;
1865609c185SAlexander Aring 
1875e053534SYueHaibing 	dentry = debugfs_create_file_unsafe("compression", 0644, root,
1882e4d60cbSAlexander Aring 					    &ldev->ctx.table[id],
1895609c185SAlexander Aring 					    &lowpan_ctx_flag_c_fops);
1905609c185SAlexander Aring 	if (!dentry)
1915609c185SAlexander Aring 		return -EINVAL;
1925609c185SAlexander Aring 
1935609c185SAlexander Aring 	dentry = debugfs_create_file("prefix", 0644, root,
1942e4d60cbSAlexander Aring 				     &ldev->ctx.table[id],
1955609c185SAlexander Aring 				     &lowpan_ctx_pfx_fops);
1965609c185SAlexander Aring 	if (!dentry)
1975609c185SAlexander Aring 		return -EINVAL;
1985609c185SAlexander Aring 
1995e053534SYueHaibing 	dentry = debugfs_create_file_unsafe("prefix_len", 0644, root,
2002e4d60cbSAlexander Aring 					    &ldev->ctx.table[id],
2015609c185SAlexander Aring 					    &lowpan_ctx_plen_fops);
2025609c185SAlexander Aring 	if (!dentry)
2035609c185SAlexander Aring 		return -EINVAL;
2045609c185SAlexander Aring 
2055609c185SAlexander Aring 	return 0;
2065609c185SAlexander Aring }
2075609c185SAlexander Aring 
2085609c185SAlexander Aring static int lowpan_context_show(struct seq_file *file, void *offset)
2095609c185SAlexander Aring {
2105609c185SAlexander Aring 	struct lowpan_iphc_ctx_table *t = file->private;
2115609c185SAlexander Aring 	int i;
2125609c185SAlexander Aring 
2135609c185SAlexander Aring 	seq_printf(file, "%3s|%-43s|%c\n", "cid", "prefix", 'C');
2145609c185SAlexander Aring 	seq_puts(file, "-------------------------------------------------\n");
2155609c185SAlexander Aring 
2165609c185SAlexander Aring 	spin_lock_bh(&t->lock);
2175609c185SAlexander Aring 	for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) {
2185609c185SAlexander Aring 		if (!lowpan_iphc_ctx_is_active(&t->table[i]))
2195609c185SAlexander Aring 			continue;
2205609c185SAlexander Aring 
2215609c185SAlexander Aring 		seq_printf(file, "%3d|%39pI6c/%-3d|%d\n", t->table[i].id,
2225609c185SAlexander Aring 			   &t->table[i].pfx, t->table[i].plen,
2235609c185SAlexander Aring 			   lowpan_iphc_ctx_is_compression(&t->table[i]));
2245609c185SAlexander Aring 	}
2255609c185SAlexander Aring 	spin_unlock_bh(&t->lock);
2265609c185SAlexander Aring 
2275609c185SAlexander Aring 	return 0;
2285609c185SAlexander Aring }
229f79ba430SYangtao Li DEFINE_SHOW_ATTRIBUTE(lowpan_context);
2305609c185SAlexander Aring 
231cfce9465SAlexander Aring static int lowpan_short_addr_get(void *data, u64 *val)
232cfce9465SAlexander Aring {
233cfce9465SAlexander Aring 	struct wpan_dev *wdev = data;
234cfce9465SAlexander Aring 
235cfce9465SAlexander Aring 	rtnl_lock();
236cfce9465SAlexander Aring 	*val = le16_to_cpu(wdev->short_addr);
237cfce9465SAlexander Aring 	rtnl_unlock();
238cfce9465SAlexander Aring 
239cfce9465SAlexander Aring 	return 0;
240cfce9465SAlexander Aring }
241cfce9465SAlexander Aring 
2425e053534SYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(lowpan_short_addr_fops, lowpan_short_addr_get, NULL,
2435e053534SYueHaibing 			 "0x%04llx\n");
244cfce9465SAlexander Aring 
245cfce9465SAlexander Aring static int lowpan_dev_debugfs_802154_init(const struct net_device *dev,
246cfce9465SAlexander Aring 					  struct lowpan_dev *ldev)
247cfce9465SAlexander Aring {
248cfce9465SAlexander Aring 	struct dentry *dentry, *root;
249cfce9465SAlexander Aring 
250cfce9465SAlexander Aring 	if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154))
251cfce9465SAlexander Aring 		return 0;
252cfce9465SAlexander Aring 
253cfce9465SAlexander Aring 	root = debugfs_create_dir("ieee802154", ldev->iface_debugfs);
254cfce9465SAlexander Aring 	if (!root)
255cfce9465SAlexander Aring 		return -EINVAL;
256cfce9465SAlexander Aring 
2575e053534SYueHaibing 	dentry = debugfs_create_file_unsafe("short_addr", 0444, root,
258cfce9465SAlexander Aring 					    lowpan_802154_dev(dev)->wdev->ieee802154_ptr,
259cfce9465SAlexander Aring 					    &lowpan_short_addr_fops);
260cfce9465SAlexander Aring 	if (!dentry)
261cfce9465SAlexander Aring 		return -EINVAL;
262cfce9465SAlexander Aring 
263cfce9465SAlexander Aring 	return 0;
264cfce9465SAlexander Aring }
265cfce9465SAlexander Aring 
266b1815fd9SAlexander Aring int lowpan_dev_debugfs_init(struct net_device *dev)
267b1815fd9SAlexander Aring {
2682e4d60cbSAlexander Aring 	struct lowpan_dev *ldev = lowpan_dev(dev);
2695609c185SAlexander Aring 	struct dentry *contexts, *dentry;
2705609c185SAlexander Aring 	int ret, i;
271b1815fd9SAlexander Aring 
272b1815fd9SAlexander Aring 	/* creating the root */
2732e4d60cbSAlexander Aring 	ldev->iface_debugfs = debugfs_create_dir(dev->name, lowpan_debugfs);
2742e4d60cbSAlexander Aring 	if (!ldev->iface_debugfs)
275b1815fd9SAlexander Aring 		goto fail;
276b1815fd9SAlexander Aring 
2772e4d60cbSAlexander Aring 	contexts = debugfs_create_dir("contexts", ldev->iface_debugfs);
2785609c185SAlexander Aring 	if (!contexts)
2795609c185SAlexander Aring 		goto remove_root;
2805609c185SAlexander Aring 
2815609c185SAlexander Aring 	dentry = debugfs_create_file("show", 0644, contexts,
2822e4d60cbSAlexander Aring 				     &lowpan_dev(dev)->ctx,
2835609c185SAlexander Aring 				     &lowpan_context_fops);
2845609c185SAlexander Aring 	if (!dentry)
2855609c185SAlexander Aring 		goto remove_root;
2865609c185SAlexander Aring 
2875609c185SAlexander Aring 	for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) {
2885609c185SAlexander Aring 		ret = lowpan_dev_debugfs_ctx_init(dev, contexts, i);
2895609c185SAlexander Aring 		if (ret < 0)
2905609c185SAlexander Aring 			goto remove_root;
2915609c185SAlexander Aring 	}
2925609c185SAlexander Aring 
293cfce9465SAlexander Aring 	ret = lowpan_dev_debugfs_802154_init(dev, ldev);
294cfce9465SAlexander Aring 	if (ret < 0)
295cfce9465SAlexander Aring 		goto remove_root;
296cfce9465SAlexander Aring 
297b1815fd9SAlexander Aring 	return 0;
298b1815fd9SAlexander Aring 
2995609c185SAlexander Aring remove_root:
3005609c185SAlexander Aring 	lowpan_dev_debugfs_exit(dev);
301b1815fd9SAlexander Aring fail:
302b1815fd9SAlexander Aring 	return -EINVAL;
303b1815fd9SAlexander Aring }
304b1815fd9SAlexander Aring 
305b1815fd9SAlexander Aring void lowpan_dev_debugfs_exit(struct net_device *dev)
306b1815fd9SAlexander Aring {
3072e4d60cbSAlexander Aring 	debugfs_remove_recursive(lowpan_dev(dev)->iface_debugfs);
308b1815fd9SAlexander Aring }
309b1815fd9SAlexander Aring 
310b1815fd9SAlexander Aring int __init lowpan_debugfs_init(void)
311b1815fd9SAlexander Aring {
312b1815fd9SAlexander Aring 	lowpan_debugfs = debugfs_create_dir("6lowpan", NULL);
313b1815fd9SAlexander Aring 	if (!lowpan_debugfs)
314b1815fd9SAlexander Aring 		return -EINVAL;
315b1815fd9SAlexander Aring 
316b1815fd9SAlexander Aring 	return 0;
317b1815fd9SAlexander Aring }
318b1815fd9SAlexander Aring 
319b1815fd9SAlexander Aring void lowpan_debugfs_exit(void)
320b1815fd9SAlexander Aring {
321b1815fd9SAlexander Aring 	debugfs_remove_recursive(lowpan_debugfs);
322b1815fd9SAlexander Aring }
323