Subject: Re: [PATCH net-next 1/3] netdevsim: Introduce support for L3 offload xstats

From: Petr Machata <[email-masked]>
Date: Fri, 11 Mar 2022 10:18:39 +0100

Jakub Kicinski <[email-masked]> writes:

> On Thu, 10 Mar 2022 17:12:22 +0100 Petr Machata wrote:
>> +static ssize_t nsim_dev_hwstats_l3_enable_write(struct file *file,
>> +						const char __user *data,
>> +						size_t count, loff_t *ppos)
>> +{
>> +	return nsim_dev_hwstats_do_write(file, data, count, ppos,
>> +					 NSIM_DEV_HWSTATS_DO_ENABLE,
>> +					 NETDEV_OFFLOAD_XSTATS_TYPE_L3);
>> +}
>
> I think you could avoid having the three wrappers if you kept 
> separate fops and added a switch to the write function keying 
> on debugfs_real_fops().
>
> With that:
>
> Acked-by: Jakub Kicinski <[email-masked]>

How about this?

struct nsim_dev_hwstats_fops {
	const struct file_operations fops;
	enum nsim_dev_hwstats_do action;
	enum netdev_offload_xstats_type type;
};

static ssize_t
nsim_dev_hwstats_do_write(struct file *file,
			  const char __user *data,
			  size_t count, loff_t *ppos)
{
	struct nsim_dev_hwstats *hwstats = file->private_data;
	struct nsim_dev_hwstats_fops *hwsfops;
	struct list_head *hwsdev_list;
	int ifindex;
	int err;

	hwsfops = container_of(debugfs_real_fops(file),
			       struct nsim_dev_hwstats_fops, fops);

	[...]

	switch (hwsfops->action) {
	case NSIM_DEV_HWSTATS_DO_DISABLE:
		err = nsim_dev_hwstats_disable_ifindex(hwstats, ifindex,
						       hwsfops->type,
						       hwsdev_list);
		break;
	[...]
}

const struct file_operations nsim_dev_hwstats_generic_fops = {
	.open = simple_open,
	.write = nsim_dev_hwstats_do_write,
	.llseek = generic_file_llseek,
	.owner = THIS_MODULE,
};

static const struct nsim_dev_hwstats_fops nsim_dev_hwstats_l3_disable_fops = {
	.fops = nsim_dev_hwstats_generic_fops,
	.action = NSIM_DEV_HWSTATS_DO_DISABLE,
	.type = NETDEV_OFFLOAD_XSTATS_TYPE_L3,
};

static const struct nsim_dev_hwstats_fops nsim_dev_hwstats_l3_enable_fops = {
	.fops = nsim_dev_hwstats_generic_fops,
	.action = NSIM_DEV_HWSTATS_DO_ENABLE,
	.type = NETDEV_OFFLOAD_XSTATS_TYPE_L3,
};

[...]