dev.c 7.2 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2018 Cumulus Networks. All rights reserved.
 * Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com>
4
 * Copyright (c) 2019 Mellanox Technologies. All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This software is licensed under the GNU General License Version 2,
 * June 1991 as shown in the file COPYING in the top-level directory of this
 * source tree.
 *
 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 */

J
Jiri Pirko 已提交
18
#include <linux/debugfs.h>
19
#include <linux/device.h>
20
#include <linux/random.h>
21
#include <linux/rtnetlink.h>
22 23 24 25
#include <net/devlink.h>

#include "netdevsim.h"

J
Jiri Pirko 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
static struct dentry *nsim_dev_ddir;

static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
{
	char dev_ddir_name[10];

	sprintf(dev_ddir_name, "%u", nsim_dev->nsim_bus_dev->dev.id);
	nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir);
	if (IS_ERR_OR_NULL(nsim_dev->ddir))
		return PTR_ERR_OR_ZERO(nsim_dev->ddir) ?: -EINVAL;
	return 0;
}

static void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev)
{
	debugfs_remove_recursive(nsim_dev->ddir);
}

44
static u64 nsim_dev_ipv4_fib_resource_occ_get(void *priv)
45
{
46
	struct nsim_dev *nsim_dev = priv;
47

48
	return nsim_fib_get_val(nsim_dev->fib_data,
49
				NSIM_RESOURCE_IPV4_FIB, false);
50 51
}

52
static u64 nsim_dev_ipv4_fib_rules_res_occ_get(void *priv)
53
{
54
	struct nsim_dev *nsim_dev = priv;
55

56
	return nsim_fib_get_val(nsim_dev->fib_data,
57
				NSIM_RESOURCE_IPV4_FIB_RULES, false);
58 59
}

60
static u64 nsim_dev_ipv6_fib_resource_occ_get(void *priv)
61
{
62
	struct nsim_dev *nsim_dev = priv;
63

64
	return nsim_fib_get_val(nsim_dev->fib_data,
65
				NSIM_RESOURCE_IPV6_FIB, false);
66 67
}

68
static u64 nsim_dev_ipv6_fib_rules_res_occ_get(void *priv)
69
{
70
	struct nsim_dev *nsim_dev = priv;
71

72
	return nsim_fib_get_val(nsim_dev->fib_data,
73
				NSIM_RESOURCE_IPV6_FIB_RULES, false);
74 75
}

76
static int nsim_dev_resources_register(struct devlink *devlink)
77
{
78
	struct nsim_dev *nsim_dev = devlink_priv(devlink);
79 80 81 82 83 84 85 86 87 88 89 90
	struct devlink_resource_size_params params = {
		.size_max = (u64)-1,
		.size_granularity = 1,
		.unit = DEVLINK_RESOURCE_UNIT_ENTRY
	};
	int err;
	u64 n;

	/* Resources for IPv4 */
	err = devlink_resource_register(devlink, "IPv4", (u64)-1,
					NSIM_RESOURCE_IPV4,
					DEVLINK_RESOURCE_ID_PARENT_TOP,
91
					&params);
92 93 94 95 96
	if (err) {
		pr_err("Failed to register IPv4 top resource\n");
		goto out;
	}

97
	n = nsim_fib_get_val(nsim_dev->fib_data,
98
			     NSIM_RESOURCE_IPV4_FIB, true);
99 100
	err = devlink_resource_register(devlink, "fib", n,
					NSIM_RESOURCE_IPV4_FIB,
101
					NSIM_RESOURCE_IPV4, &params);
102 103 104 105 106
	if (err) {
		pr_err("Failed to register IPv4 FIB resource\n");
		return err;
	}

107
	n = nsim_fib_get_val(nsim_dev->fib_data,
108
			     NSIM_RESOURCE_IPV4_FIB_RULES, true);
109 110
	err = devlink_resource_register(devlink, "fib-rules", n,
					NSIM_RESOURCE_IPV4_FIB_RULES,
111
					NSIM_RESOURCE_IPV4, &params);
112 113 114 115 116 117 118 119 120
	if (err) {
		pr_err("Failed to register IPv4 FIB rules resource\n");
		return err;
	}

	/* Resources for IPv6 */
	err = devlink_resource_register(devlink, "IPv6", (u64)-1,
					NSIM_RESOURCE_IPV6,
					DEVLINK_RESOURCE_ID_PARENT_TOP,
121
					&params);
122 123 124 125 126
	if (err) {
		pr_err("Failed to register IPv6 top resource\n");
		goto out;
	}

127
	n = nsim_fib_get_val(nsim_dev->fib_data,
128
			     NSIM_RESOURCE_IPV6_FIB, true);
129 130
	err = devlink_resource_register(devlink, "fib", n,
					NSIM_RESOURCE_IPV6_FIB,
131
					NSIM_RESOURCE_IPV6, &params);
132 133 134 135 136
	if (err) {
		pr_err("Failed to register IPv6 FIB resource\n");
		return err;
	}

137
	n = nsim_fib_get_val(nsim_dev->fib_data,
138
			     NSIM_RESOURCE_IPV6_FIB_RULES, true);
139 140
	err = devlink_resource_register(devlink, "fib-rules", n,
					NSIM_RESOURCE_IPV6_FIB_RULES,
141
					NSIM_RESOURCE_IPV6, &params);
142 143 144 145
	if (err) {
		pr_err("Failed to register IPv6 FIB rules resource\n");
		return err;
	}
146 147 148

	devlink_resource_occ_get_register(devlink,
					  NSIM_RESOURCE_IPV4_FIB,
149 150
					  nsim_dev_ipv4_fib_resource_occ_get,
					  nsim_dev);
151 152
	devlink_resource_occ_get_register(devlink,
					  NSIM_RESOURCE_IPV4_FIB_RULES,
153 154
					  nsim_dev_ipv4_fib_rules_res_occ_get,
					  nsim_dev);
155 156
	devlink_resource_occ_get_register(devlink,
					  NSIM_RESOURCE_IPV6_FIB,
157 158
					  nsim_dev_ipv6_fib_resource_occ_get,
					  nsim_dev);
159 160
	devlink_resource_occ_get_register(devlink,
					  NSIM_RESOURCE_IPV6_FIB_RULES,
161 162
					  nsim_dev_ipv6_fib_rules_res_occ_get,
					  nsim_dev);
163 164 165 166
out:
	return err;
}

167 168
static int nsim_dev_reload(struct devlink *devlink,
			   struct netlink_ext_ack *extack)
169
{
170
	struct nsim_dev *nsim_dev = devlink_priv(devlink);
171 172 173 174 175 176 177 178 179 180 181 182
	enum nsim_resource_id res_ids[] = {
		NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES,
		NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES
	};
	int i;

	for (i = 0; i < ARRAY_SIZE(res_ids); ++i) {
		int err;
		u64 val;

		err = devlink_resource_size_get(devlink, res_ids[i], &val);
		if (!err) {
183
			err = nsim_fib_set_max(nsim_dev->fib_data,
184
					       res_ids[i], val, extack);
185 186 187 188 189 190 191 192
			if (err)
				return err;
		}
	}

	return 0;
}

193 194
static const struct devlink_ops nsim_dev_devlink_ops = {
	.reload = nsim_dev_reload,
195 196
};

197
static struct nsim_dev *nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev)
198
{
199
	struct nsim_dev *nsim_dev;
200
	struct devlink *devlink;
201
	int err;
202

203
	devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev));
204
	if (!devlink)
205
		return ERR_PTR(-ENOMEM);
206
	nsim_dev = devlink_priv(devlink);
J
Jiri Pirko 已提交
207
	nsim_dev->nsim_bus_dev = nsim_bus_dev;
208 209
	nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
	get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
210

211 212 213
	nsim_dev->fib_data = nsim_fib_create();
	if (IS_ERR(nsim_dev->fib_data)) {
		err = PTR_ERR(nsim_dev->fib_data);
214
		goto err_devlink_free;
215
	}
216

217
	err = nsim_dev_resources_register(devlink);
218
	if (err)
219
		goto err_fib_destroy;
220

221
	err = devlink_register(devlink, &nsim_bus_dev->dev);
222 223
	if (err)
		goto err_resources_unregister;
224

J
Jiri Pirko 已提交
225 226 227 228 229 230 231 232
	err = nsim_dev_debugfs_init(nsim_dev);
	if (err)
		goto err_dl_unregister;

	err = nsim_bpf_dev_init(nsim_dev);
	if (err)
		goto err_debugfs_exit;

233
	return nsim_dev;
234

J
Jiri Pirko 已提交
235 236 237 238
err_debugfs_exit:
	nsim_dev_debugfs_exit(nsim_dev);
err_dl_unregister:
	devlink_unregister(devlink);
239 240 241
err_resources_unregister:
	devlink_resources_unregister(devlink, NULL);
err_fib_destroy:
242
	nsim_fib_destroy(nsim_dev->fib_data);
243 244
err_devlink_free:
	devlink_free(devlink);
245
	return ERR_PTR(err);
246 247
}

248 249 250
struct nsim_dev *
nsim_dev_create_with_ns(struct nsim_bus_dev *nsim_bus_dev,
			struct netdevsim *ns)
251
{
252
	struct nsim_dev *nsim_dev;
253

254 255
	dev_hold(ns->netdev);
	rtnl_unlock();
256
	nsim_dev = nsim_dev_create(nsim_bus_dev);
257 258
	rtnl_lock();
	dev_put(ns->netdev);
259
	return nsim_dev;
260 261
}

262
void nsim_dev_destroy(struct nsim_dev *nsim_dev)
263
{
264
	struct devlink *devlink = priv_to_devlink(nsim_dev);
265

J
Jiri Pirko 已提交
266 267
	nsim_bpf_dev_exit(nsim_dev);
	nsim_dev_debugfs_exit(nsim_dev);
268 269
	devlink_unregister(devlink);
	devlink_resources_unregister(devlink, NULL);
270
	nsim_fib_destroy(nsim_dev->fib_data);
271
	devlink_free(devlink);
272
}
J
Jiri Pirko 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285

int nsim_dev_init(void)
{
	nsim_dev_ddir = debugfs_create_dir(DRV_NAME "_dev", NULL);
	if (IS_ERR_OR_NULL(nsim_dev_ddir))
		return -ENOMEM;
	return 0;
}

void nsim_dev_exit(void)
{
	debugfs_remove_recursive(nsim_dev_ddir);
}