Skip to content

Commit

Permalink
debugfs for nexthop [xxx]
Browse files Browse the repository at this point in the history
- Eventually this will be part of netdevsim as an interface to the
  simulated offload, but until then, this will be good for testing.
  • Loading branch information
pmachata committed Nov 20, 2020
1 parent 99f90f1 commit eb6c13f
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions net/ipv4/nexthop.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
*/

#include <linux/debugfs.h>
#include <linux/nexthop.h>
#include <linux/rtnetlink.h>
#include <linux/slab.h>
Expand Down Expand Up @@ -2592,8 +2593,106 @@ static struct pernet_operations nexthop_net_ops = {
.exit = nexthop_net_exit,
};

static struct nexthop_debugfs {
struct dentry *ddir;
struct dentry *bucket_activity;
} *nexthop_debugfs;

static ssize_t nexthop_dfs_bucket_activity_write(struct file *file,
const char __user *user_buf,
size_t size, loff_t *ppos)
{
const char *tok_bucketid;
const char *tok_nhid;
struct nexthop *nh;
loff_t pos = *ppos;
char buf[128];
u16 bucketid; // xxx or u32 if we choose so
char *ptr;
u32 nhid;
int err;

if (pos < 0)
return -EINVAL;
if (pos == size)
goto skip;
if (pos)
return -EINVAL;
if (size > sizeof(buf))
return -ENOMEM;
if (copy_from_user(buf, user_buf, size))
return -EFAULT;

buf[sizeof(buf) - 1] = '\0';
ptr = buf;
tok_nhid = strsep(&ptr, " \t");
tok_bucketid = strsep(&ptr, " \t");

if (!tok_nhid || !tok_bucketid || ptr)
return -EINVAL;

err = kstrtou32(tok_nhid, 0, &nhid);
if (err)
return err;

err = kstrtou16(tok_bucketid, 0, &bucketid);
if (err)
return err;

rcu_read_lock();
nh = nexthop_find_by_id(&init_net, nhid);
if (!nh || !nexthop_select_path(nh, bucketid))
err = -ENOENT;
rcu_read_unlock();

skip:
*ppos = size;
return err ?: size;
}

static const struct file_operations nexthop_debugfs_bucket_activity_ops = {
.write = nexthop_dfs_bucket_activity_write,
.llseek = no_llseek,
};

static struct nexthop_debugfs *nexthop_debugfs_alloc(void)
{
struct nexthop_debugfs *dfs;
int err;

dfs = kzalloc(sizeof(*dfs), GFP_KERNEL);
if (!dfs)
return NULL;

dfs->ddir = debugfs_create_dir("nexthop", NULL);
if (IS_ERR(dfs->ddir)) {
err = PTR_ERR(dfs->ddir);
goto err_create_dir;
}

dfs->bucket_activity = debugfs_create_file("bucket_activity", S_IWUSR,
dfs->ddir, NULL,
&nexthop_debugfs_bucket_activity_ops);
if (IS_ERR(dfs->bucket_activity)) {
err = PTR_ERR(dfs->bucket_activity);
goto err_create_bucket_activity;
}

return dfs;

err_create_bucket_activity:
debugfs_remove_recursive(dfs->ddir);
err_create_dir:
kfree(dfs);
return ERR_PTR(err);
}

static int __init nexthop_init(void)
{
nexthop_debugfs = nexthop_debugfs_alloc();
if (IS_ERR(nexthop_debugfs))
return PTR_ERR(nexthop_debugfs);

register_pernet_subsys(&nexthop_net_ops);

register_netdevice_notifier(&nh_netdev_notifier);
Expand Down

0 comments on commit eb6c13f

Please sign in to comment.