Linux Audio

Check our new training course

Loading...
v5.14.15
   1/*
   2 * Copyright (c) 2018 Cumulus Networks. All rights reserved.
   3 * Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com>
   4 * Copyright (c) 2019 Mellanox Technologies. All rights reserved.
   5 *
   6 * This software is licensed under the GNU General License Version 2,
   7 * June 1991 as shown in the file COPYING in the top-level directory of this
   8 * source tree.
   9 *
  10 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
  11 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  12 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  13 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
  14 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
  15 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  16 */
  17
  18#include <linux/debugfs.h>
  19#include <linux/device.h>
  20#include <linux/etherdevice.h>
  21#include <linux/inet.h>
  22#include <linux/jiffies.h>
  23#include <linux/kernel.h>
  24#include <linux/list.h>
  25#include <linux/mutex.h>
  26#include <linux/random.h>
  27#include <linux/rtnetlink.h>
  28#include <linux/workqueue.h>
  29#include <net/devlink.h>
  30#include <net/ip.h>
  31#include <net/flow_offload.h>
  32#include <uapi/linux/devlink.h>
  33#include <uapi/linux/ip.h>
  34#include <uapi/linux/udp.h>
  35
  36#include "netdevsim.h"
  37
  38static unsigned int
  39nsim_dev_port_index(enum nsim_dev_port_type type, unsigned int port_index)
  40{
  41	switch (type) {
  42	case NSIM_DEV_PORT_TYPE_VF:
  43		port_index = NSIM_DEV_VF_PORT_INDEX_BASE + port_index;
  44		break;
  45	case NSIM_DEV_PORT_TYPE_PF:
  46		break;
  47	}
  48
  49	return port_index;
  50}
  51
  52static inline unsigned int nsim_dev_port_index_to_vf_index(unsigned int port_index)
  53{
  54	return port_index - NSIM_DEV_VF_PORT_INDEX_BASE;
  55}
  56
  57static struct dentry *nsim_dev_ddir;
  58
  59#define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)
  60
  61static int
  62nsim_dev_take_snapshot(struct devlink *devlink,
  63		       const struct devlink_region_ops *ops,
  64		       struct netlink_ext_ack *extack,
  65		       u8 **data)
  66{
  67	void *dummy_data;
  68
  69	dummy_data = kmalloc(NSIM_DEV_DUMMY_REGION_SIZE, GFP_KERNEL);
  70	if (!dummy_data)
  71		return -ENOMEM;
  72
  73	get_random_bytes(dummy_data, NSIM_DEV_DUMMY_REGION_SIZE);
  74
  75	*data = dummy_data;
  76
  77	return 0;
  78}
  79
  80static ssize_t nsim_dev_take_snapshot_write(struct file *file,
  81					    const char __user *data,
  82					    size_t count, loff_t *ppos)
  83{
  84	struct nsim_dev *nsim_dev = file->private_data;
  85	struct devlink *devlink;
  86	u8 *dummy_data;
  87	int err;
  88	u32 id;
  89
  90	devlink = priv_to_devlink(nsim_dev);
  91
  92	err = nsim_dev_take_snapshot(devlink, NULL, NULL, &dummy_data);
  93	if (err)
  94		return err;
  95
  96	err = devlink_region_snapshot_id_get(devlink, &id);
  97	if (err) {
  98		pr_err("Failed to get snapshot id\n");
  99		kfree(dummy_data);
 100		return err;
 101	}
 102	err = devlink_region_snapshot_create(nsim_dev->dummy_region,
 103					     dummy_data, id);
 104	devlink_region_snapshot_id_put(devlink, id);
 105	if (err) {
 106		pr_err("Failed to create region snapshot\n");
 107		kfree(dummy_data);
 108		return err;
 109	}
 110
 111	return count;
 112}
 113
 114static const struct file_operations nsim_dev_take_snapshot_fops = {
 115	.open = simple_open,
 116	.write = nsim_dev_take_snapshot_write,
 117	.llseek = generic_file_llseek,
 118	.owner = THIS_MODULE,
 119};
 120
 121static ssize_t nsim_dev_trap_fa_cookie_read(struct file *file,
 122					    char __user *data,
 123					    size_t count, loff_t *ppos)
 124{
 125	struct nsim_dev *nsim_dev = file->private_data;
 126	struct flow_action_cookie *fa_cookie;
 127	unsigned int buf_len;
 128	ssize_t ret;
 129	char *buf;
 130
 131	spin_lock(&nsim_dev->fa_cookie_lock);
 132	fa_cookie = nsim_dev->fa_cookie;
 133	if (!fa_cookie) {
 134		ret = -EINVAL;
 135		goto errout;
 136	}
 137	buf_len = fa_cookie->cookie_len * 2;
 138	buf = kmalloc(buf_len, GFP_ATOMIC);
 139	if (!buf) {
 140		ret = -ENOMEM;
 141		goto errout;
 142	}
 143	bin2hex(buf, fa_cookie->cookie, fa_cookie->cookie_len);
 144	spin_unlock(&nsim_dev->fa_cookie_lock);
 145
 146	ret = simple_read_from_buffer(data, count, ppos, buf, buf_len);
 147
 148	kfree(buf);
 149	return ret;
 150
 151errout:
 152	spin_unlock(&nsim_dev->fa_cookie_lock);
 153	return ret;
 154}
 155
 156static ssize_t nsim_dev_trap_fa_cookie_write(struct file *file,
 157					     const char __user *data,
 158					     size_t count, loff_t *ppos)
 159{
 160	struct nsim_dev *nsim_dev = file->private_data;
 161	struct flow_action_cookie *fa_cookie;
 162	size_t cookie_len;
 163	ssize_t ret;
 164	char *buf;
 165
 166	if (*ppos != 0)
 167		return -EINVAL;
 168	cookie_len = (count - 1) / 2;
 169	if ((count - 1) % 2)
 170		return -EINVAL;
 171	buf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
 172	if (!buf)
 173		return -ENOMEM;
 174
 175	ret = simple_write_to_buffer(buf, count, ppos, data, count);
 176	if (ret < 0)
 177		goto free_buf;
 178
 179	fa_cookie = kmalloc(sizeof(*fa_cookie) + cookie_len,
 180			    GFP_KERNEL | __GFP_NOWARN);
 181	if (!fa_cookie) {
 182		ret = -ENOMEM;
 183		goto free_buf;
 184	}
 185
 186	fa_cookie->cookie_len = cookie_len;
 187	ret = hex2bin(fa_cookie->cookie, buf, cookie_len);
 188	if (ret)
 189		goto free_fa_cookie;
 190	kfree(buf);
 191
 192	spin_lock(&nsim_dev->fa_cookie_lock);
 193	kfree(nsim_dev->fa_cookie);
 194	nsim_dev->fa_cookie = fa_cookie;
 195	spin_unlock(&nsim_dev->fa_cookie_lock);
 196
 197	return count;
 198
 199free_fa_cookie:
 200	kfree(fa_cookie);
 201free_buf:
 202	kfree(buf);
 203	return ret;
 204}
 205
 206static const struct file_operations nsim_dev_trap_fa_cookie_fops = {
 207	.open = simple_open,
 208	.read = nsim_dev_trap_fa_cookie_read,
 209	.write = nsim_dev_trap_fa_cookie_write,
 210	.llseek = generic_file_llseek,
 211	.owner = THIS_MODULE,
 212};
 213
 214static const struct file_operations nsim_dev_max_vfs_fops = {
 215	.open = simple_open,
 216	.read = nsim_bus_dev_max_vfs_read,
 217	.write = nsim_bus_dev_max_vfs_write,
 218	.llseek = generic_file_llseek,
 219	.owner = THIS_MODULE,
 220};
 221
 222static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
 223{
 224	char dev_ddir_name[sizeof(DRV_NAME) + 10];
 225	int err;
 226
 227	sprintf(dev_ddir_name, DRV_NAME "%u", nsim_dev->nsim_bus_dev->dev.id);
 228	nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir);
 229	if (IS_ERR(nsim_dev->ddir))
 230		return PTR_ERR(nsim_dev->ddir);
 231	nsim_dev->ports_ddir = debugfs_create_dir("ports", nsim_dev->ddir);
 232	if (IS_ERR(nsim_dev->ports_ddir))
 233		return PTR_ERR(nsim_dev->ports_ddir);
 234	debugfs_create_bool("fw_update_status", 0600, nsim_dev->ddir,
 235			    &nsim_dev->fw_update_status);
 236	debugfs_create_u32("fw_update_overwrite_mask", 0600, nsim_dev->ddir,
 237			    &nsim_dev->fw_update_overwrite_mask);
 238	debugfs_create_u32("max_macs", 0600, nsim_dev->ddir,
 239			   &nsim_dev->max_macs);
 240	debugfs_create_bool("test1", 0600, nsim_dev->ddir,
 241			    &nsim_dev->test1);
 242	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
 243						      0200,
 244						      nsim_dev->ddir,
 245						      nsim_dev,
 246						&nsim_dev_take_snapshot_fops);
 247	debugfs_create_bool("dont_allow_reload", 0600, nsim_dev->ddir,
 248			    &nsim_dev->dont_allow_reload);
 249	debugfs_create_bool("fail_reload", 0600, nsim_dev->ddir,
 250			    &nsim_dev->fail_reload);
 251	debugfs_create_file("trap_flow_action_cookie", 0600, nsim_dev->ddir,
 252			    nsim_dev, &nsim_dev_trap_fa_cookie_fops);
 253	debugfs_create_bool("fail_trap_group_set", 0600,
 254			    nsim_dev->ddir,
 255			    &nsim_dev->fail_trap_group_set);
 256	debugfs_create_bool("fail_trap_policer_set", 0600,
 257			    nsim_dev->ddir,
 258			    &nsim_dev->fail_trap_policer_set);
 259	debugfs_create_bool("fail_trap_policer_counter_get", 0600,
 260			    nsim_dev->ddir,
 261			    &nsim_dev->fail_trap_policer_counter_get);
 262	nsim_dev->max_vfs = debugfs_create_file("max_vfs",
 263						0600,
 264						nsim_dev->ddir,
 265						nsim_dev->nsim_bus_dev,
 266						&nsim_dev_max_vfs_fops);
 267	nsim_dev->nodes_ddir = debugfs_create_dir("rate_nodes", nsim_dev->ddir);
 268	if (IS_ERR(nsim_dev->nodes_ddir)) {
 269		err = PTR_ERR(nsim_dev->nodes_ddir);
 270		goto err_out;
 271	}
 272	debugfs_create_bool("fail_trap_drop_counter_get", 0600,
 273			    nsim_dev->ddir,
 274			    &nsim_dev->fail_trap_drop_counter_get);
 275	nsim_udp_tunnels_debugfs_create(nsim_dev);
 276	return 0;
 277
 278err_out:
 279	debugfs_remove_recursive(nsim_dev->ports_ddir);
 280	debugfs_remove_recursive(nsim_dev->ddir);
 281	return err;
 282}
 283
 284static void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev)
 285{
 286	debugfs_remove_recursive(nsim_dev->nodes_ddir);
 287	debugfs_remove_recursive(nsim_dev->ports_ddir);
 288	debugfs_remove_recursive(nsim_dev->ddir);
 289}
 290
 291static ssize_t nsim_dev_rate_parent_read(struct file *file,
 292					 char __user *data,
 293					 size_t count, loff_t *ppos)
 294{
 295	char **name_ptr = file->private_data;
 296	size_t len;
 297
 298	if (!*name_ptr)
 299		return 0;
 300
 301	len = strlen(*name_ptr);
 302	return simple_read_from_buffer(data, count, ppos, *name_ptr, len);
 303}
 304
 305static const struct file_operations nsim_dev_rate_parent_fops = {
 306	.open = simple_open,
 307	.read = nsim_dev_rate_parent_read,
 308	.llseek = generic_file_llseek,
 309	.owner = THIS_MODULE,
 310};
 311
 312static int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev,
 313				      struct nsim_dev_port *nsim_dev_port)
 314{
 315	struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
 316	unsigned int port_index = nsim_dev_port->port_index;
 317	char port_ddir_name[16];
 318	char dev_link_name[32];
 319
 320	sprintf(port_ddir_name, "%u", port_index);
 321	nsim_dev_port->ddir = debugfs_create_dir(port_ddir_name,
 322						 nsim_dev->ports_ddir);
 323	if (IS_ERR(nsim_dev_port->ddir))
 324		return PTR_ERR(nsim_dev_port->ddir);
 325
 326	sprintf(dev_link_name, "../../../" DRV_NAME "%u", nsim_bus_dev->dev.id);
 327	if (nsim_dev_port_is_vf(nsim_dev_port)) {
 328		unsigned int vf_id = nsim_dev_port_index_to_vf_index(port_index);
 329
 330		debugfs_create_u16("tx_share", 0400, nsim_dev_port->ddir,
 331				   &nsim_bus_dev->vfconfigs[vf_id].min_tx_rate);
 332		debugfs_create_u16("tx_max", 0400, nsim_dev_port->ddir,
 333				   &nsim_bus_dev->vfconfigs[vf_id].max_tx_rate);
 334		nsim_dev_port->rate_parent = debugfs_create_file("rate_parent",
 335								 0400,
 336								 nsim_dev_port->ddir,
 337								 &nsim_dev_port->parent_name,
 338								 &nsim_dev_rate_parent_fops);
 339	}
 340	debugfs_create_symlink("dev", nsim_dev_port->ddir, dev_link_name);
 341
 342	return 0;
 343}
 344
 345static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port)
 346{
 347	debugfs_remove_recursive(nsim_dev_port->ddir);
 348}
 349
 350static int nsim_dev_resources_register(struct devlink *devlink)
 351{
 352	struct devlink_resource_size_params params = {
 353		.size_max = (u64)-1,
 354		.size_granularity = 1,
 355		.unit = DEVLINK_RESOURCE_UNIT_ENTRY
 356	};
 357	int err;
 358
 359	/* Resources for IPv4 */
 360	err = devlink_resource_register(devlink, "IPv4", (u64)-1,
 361					NSIM_RESOURCE_IPV4,
 362					DEVLINK_RESOURCE_ID_PARENT_TOP,
 363					&params);
 364	if (err) {
 365		pr_err("Failed to register IPv4 top resource\n");
 366		goto out;
 367	}
 368
 369	err = devlink_resource_register(devlink, "fib", (u64)-1,
 370					NSIM_RESOURCE_IPV4_FIB,
 371					NSIM_RESOURCE_IPV4, &params);
 372	if (err) {
 373		pr_err("Failed to register IPv4 FIB resource\n");
 374		return err;
 375	}
 376
 377	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
 378					NSIM_RESOURCE_IPV4_FIB_RULES,
 379					NSIM_RESOURCE_IPV4, &params);
 380	if (err) {
 381		pr_err("Failed to register IPv4 FIB rules resource\n");
 382		return err;
 383	}
 384
 385	/* Resources for IPv6 */
 386	err = devlink_resource_register(devlink, "IPv6", (u64)-1,
 387					NSIM_RESOURCE_IPV6,
 388					DEVLINK_RESOURCE_ID_PARENT_TOP,
 389					&params);
 390	if (err) {
 391		pr_err("Failed to register IPv6 top resource\n");
 392		goto out;
 393	}
 394
 395	err = devlink_resource_register(devlink, "fib", (u64)-1,
 396					NSIM_RESOURCE_IPV6_FIB,
 397					NSIM_RESOURCE_IPV6, &params);
 398	if (err) {
 399		pr_err("Failed to register IPv6 FIB resource\n");
 400		return err;
 401	}
 402
 403	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
 404					NSIM_RESOURCE_IPV6_FIB_RULES,
 405					NSIM_RESOURCE_IPV6, &params);
 406	if (err) {
 407		pr_err("Failed to register IPv6 FIB rules resource\n");
 408		return err;
 409	}
 410
 411	/* Resources for nexthops */
 412	err = devlink_resource_register(devlink, "nexthops", (u64)-1,
 413					NSIM_RESOURCE_NEXTHOPS,
 414					DEVLINK_RESOURCE_ID_PARENT_TOP,
 415					&params);
 416
 417out:
 418	return err;
 419}
 420
 421enum nsim_devlink_param_id {
 422	NSIM_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
 423	NSIM_DEVLINK_PARAM_ID_TEST1,
 424};
 425
 426static const struct devlink_param nsim_devlink_params[] = {
 427	DEVLINK_PARAM_GENERIC(MAX_MACS,
 428			      BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
 429			      NULL, NULL, NULL),
 430	DEVLINK_PARAM_DRIVER(NSIM_DEVLINK_PARAM_ID_TEST1,
 431			     "test1", DEVLINK_PARAM_TYPE_BOOL,
 432			     BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
 433			     NULL, NULL, NULL),
 434};
 435
 436static void nsim_devlink_set_params_init_values(struct nsim_dev *nsim_dev,
 437						struct devlink *devlink)
 438{
 439	union devlink_param_value value;
 440
 441	value.vu32 = nsim_dev->max_macs;
 442	devlink_param_driverinit_value_set(devlink,
 443					   DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
 444					   value);
 445	value.vbool = nsim_dev->test1;
 446	devlink_param_driverinit_value_set(devlink,
 447					   NSIM_DEVLINK_PARAM_ID_TEST1,
 448					   value);
 449}
 450
 451static void nsim_devlink_param_load_driverinit_values(struct devlink *devlink)
 452{
 453	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 454	union devlink_param_value saved_value;
 455	int err;
 456
 457	err = devlink_param_driverinit_value_get(devlink,
 458						 DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
 459						 &saved_value);
 460	if (!err)
 461		nsim_dev->max_macs = saved_value.vu32;
 462	err = devlink_param_driverinit_value_get(devlink,
 463						 NSIM_DEVLINK_PARAM_ID_TEST1,
 464						 &saved_value);
 465	if (!err)
 466		nsim_dev->test1 = saved_value.vbool;
 467}
 468
 469#define NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX 16
 470
 471static const struct devlink_region_ops dummy_region_ops = {
 472	.name = "dummy",
 473	.destructor = &kfree,
 474	.snapshot = nsim_dev_take_snapshot,
 475};
 476
 477static int nsim_dev_dummy_region_init(struct nsim_dev *nsim_dev,
 478				      struct devlink *devlink)
 479{
 480	nsim_dev->dummy_region =
 481		devlink_region_create(devlink, &dummy_region_ops,
 482				      NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX,
 483				      NSIM_DEV_DUMMY_REGION_SIZE);
 484	return PTR_ERR_OR_ZERO(nsim_dev->dummy_region);
 485}
 486
 487static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
 488{
 489	devlink_region_destroy(nsim_dev->dummy_region);
 490}
 491
 492static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port);
 493int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack)
 494{
 495	struct devlink *devlink = priv_to_devlink(nsim_dev);
 496	struct nsim_dev_port *nsim_dev_port, *tmp;
 497
 498	devlink_rate_nodes_destroy(devlink);
 499	mutex_lock(&nsim_dev->port_list_lock);
 500	list_for_each_entry_safe(nsim_dev_port, tmp, &nsim_dev->port_list, list)
 501		if (nsim_dev_port_is_vf(nsim_dev_port))
 502			__nsim_dev_port_del(nsim_dev_port);
 503	mutex_unlock(&nsim_dev->port_list_lock);
 504	nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_LEGACY;
 505	return 0;
 506}
 507
 508int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev, struct netlink_ext_ack *extack)
 509{
 510	struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
 511	int i, err;
 512
 513	for (i = 0; i < nsim_bus_dev->num_vfs; i++) {
 514		err = nsim_dev_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_VF, i);
 515		if (err) {
 516			NL_SET_ERR_MSG_MOD(extack, "Failed to initialize VFs' netdevsim ports");
 517			pr_err("Failed to initialize VF id=%d. %d.\n", i, err);
 518			goto err_port_add_vfs;
 519		}
 520	}
 521	nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
 522	return 0;
 523
 524err_port_add_vfs:
 525	for (i--; i >= 0; i--)
 526		nsim_dev_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_VF, i);
 527	return err;
 528}
 529
 530static int nsim_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
 531					 struct netlink_ext_ack *extack)
 532{
 533	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 534	int err = 0;
 535
 536	mutex_lock(&nsim_dev->nsim_bus_dev->vfs_lock);
 537	if (mode == nsim_dev->esw_mode)
 538		goto unlock;
 539
 540	if (mode == DEVLINK_ESWITCH_MODE_LEGACY)
 541		err = nsim_esw_legacy_enable(nsim_dev, extack);
 542	else if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
 543		err = nsim_esw_switchdev_enable(nsim_dev, extack);
 544	else
 545		err = -EINVAL;
 546
 547unlock:
 548	mutex_unlock(&nsim_dev->nsim_bus_dev->vfs_lock);
 549	return err;
 550}
 551
 552static int nsim_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
 553{
 554	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 555
 556	*mode = nsim_dev->esw_mode;
 557	return 0;
 558}
 559
 560struct nsim_trap_item {
 561	void *trap_ctx;
 562	enum devlink_trap_action action;
 563};
 564
 565struct nsim_trap_data {
 566	struct delayed_work trap_report_dw;
 567	struct nsim_trap_item *trap_items_arr;
 568	u64 *trap_policers_cnt_arr;
 569	u64 trap_pkt_cnt;
 570	struct nsim_dev *nsim_dev;
 571	spinlock_t trap_lock;	/* Protects trap_items_arr */
 572};
 573
 574/* All driver-specific traps must be documented in
 575 * Documentation/networking/devlink/netdevsim.rst
 576 */
 577enum {
 578	NSIM_TRAP_ID_BASE = DEVLINK_TRAP_GENERIC_ID_MAX,
 579	NSIM_TRAP_ID_FID_MISS,
 580};
 581
 582#define NSIM_TRAP_NAME_FID_MISS "fid_miss"
 583
 584#define NSIM_TRAP_METADATA DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
 585
 586#define NSIM_TRAP_DROP(_id, _group_id)					      \
 587	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
 588			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 589			     NSIM_TRAP_METADATA)
 590#define NSIM_TRAP_DROP_EXT(_id, _group_id, _metadata)			      \
 591	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
 592			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 593			     NSIM_TRAP_METADATA | (_metadata))
 594#define NSIM_TRAP_EXCEPTION(_id, _group_id)				      \
 595	DEVLINK_TRAP_GENERIC(EXCEPTION, TRAP, _id,			      \
 596			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 597			     NSIM_TRAP_METADATA)
 598#define NSIM_TRAP_CONTROL(_id, _group_id, _action)			      \
 599	DEVLINK_TRAP_GENERIC(CONTROL, _action, _id,			      \
 600			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 601			     NSIM_TRAP_METADATA)
 602#define NSIM_TRAP_DRIVER_EXCEPTION(_id, _group_id)			      \
 603	DEVLINK_TRAP_DRIVER(EXCEPTION, TRAP, NSIM_TRAP_ID_##_id,	      \
 604			    NSIM_TRAP_NAME_##_id,			      \
 605			    DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 606			    NSIM_TRAP_METADATA)
 607
 608#define NSIM_DEV_TRAP_POLICER_MIN_RATE	1
 609#define NSIM_DEV_TRAP_POLICER_MAX_RATE	8000
 610#define NSIM_DEV_TRAP_POLICER_MIN_BURST	8
 611#define NSIM_DEV_TRAP_POLICER_MAX_BURST	65536
 612
 613#define NSIM_TRAP_POLICER(_id, _rate, _burst)				      \
 614	DEVLINK_TRAP_POLICER(_id, _rate, _burst,			      \
 615			     NSIM_DEV_TRAP_POLICER_MAX_RATE,		      \
 616			     NSIM_DEV_TRAP_POLICER_MIN_RATE,		      \
 617			     NSIM_DEV_TRAP_POLICER_MAX_BURST,		      \
 618			     NSIM_DEV_TRAP_POLICER_MIN_BURST)
 619
 620static const struct devlink_trap_policer nsim_trap_policers_arr[] = {
 621	NSIM_TRAP_POLICER(1, 1000, 128),
 622	NSIM_TRAP_POLICER(2, 2000, 256),
 623	NSIM_TRAP_POLICER(3, 3000, 512),
 624};
 625
 626static const struct devlink_trap_group nsim_trap_groups_arr[] = {
 627	DEVLINK_TRAP_GROUP_GENERIC(L2_DROPS, 0),
 628	DEVLINK_TRAP_GROUP_GENERIC(L3_DROPS, 1),
 629	DEVLINK_TRAP_GROUP_GENERIC(L3_EXCEPTIONS, 1),
 630	DEVLINK_TRAP_GROUP_GENERIC(BUFFER_DROPS, 2),
 631	DEVLINK_TRAP_GROUP_GENERIC(ACL_DROPS, 3),
 632	DEVLINK_TRAP_GROUP_GENERIC(MC_SNOOPING, 3),
 633};
 634
 635static const struct devlink_trap nsim_traps_arr[] = {
 636	NSIM_TRAP_DROP(SMAC_MC, L2_DROPS),
 637	NSIM_TRAP_DROP(VLAN_TAG_MISMATCH, L2_DROPS),
 638	NSIM_TRAP_DROP(INGRESS_VLAN_FILTER, L2_DROPS),
 639	NSIM_TRAP_DROP(INGRESS_STP_FILTER, L2_DROPS),
 640	NSIM_TRAP_DROP(EMPTY_TX_LIST, L2_DROPS),
 641	NSIM_TRAP_DROP(PORT_LOOPBACK_FILTER, L2_DROPS),
 642	NSIM_TRAP_DRIVER_EXCEPTION(FID_MISS, L2_DROPS),
 643	NSIM_TRAP_DROP(BLACKHOLE_ROUTE, L3_DROPS),
 644	NSIM_TRAP_EXCEPTION(TTL_ERROR, L3_EXCEPTIONS),
 645	NSIM_TRAP_DROP(TAIL_DROP, BUFFER_DROPS),
 646	NSIM_TRAP_DROP_EXT(INGRESS_FLOW_ACTION_DROP, ACL_DROPS,
 647			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
 648	NSIM_TRAP_DROP_EXT(EGRESS_FLOW_ACTION_DROP, ACL_DROPS,
 649			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
 650	NSIM_TRAP_CONTROL(IGMP_QUERY, MC_SNOOPING, MIRROR),
 651	NSIM_TRAP_CONTROL(IGMP_V1_REPORT, MC_SNOOPING, TRAP),
 652};
 653
 654#define NSIM_TRAP_L4_DATA_LEN 100
 655
 656static struct sk_buff *nsim_dev_trap_skb_build(void)
 657{
 658	int tot_len, data_len = NSIM_TRAP_L4_DATA_LEN;
 659	struct sk_buff *skb;
 660	struct udphdr *udph;
 661	struct ethhdr *eth;
 662	struct iphdr *iph;
 663
 664	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
 665	if (!skb)
 666		return NULL;
 667	tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len;
 668
 669	skb_reset_mac_header(skb);
 670	eth = skb_put(skb, sizeof(struct ethhdr));
 671	eth_random_addr(eth->h_dest);
 672	eth_random_addr(eth->h_source);
 673	eth->h_proto = htons(ETH_P_IP);
 674	skb->protocol = htons(ETH_P_IP);
 675
 676	skb_set_network_header(skb, skb->len);
 677	iph = skb_put(skb, sizeof(struct iphdr));
 678	iph->protocol = IPPROTO_UDP;
 679	iph->saddr = in_aton("192.0.2.1");
 680	iph->daddr = in_aton("198.51.100.1");
 681	iph->version = 0x4;
 682	iph->frag_off = 0;
 683	iph->ihl = 0x5;
 684	iph->tot_len = htons(tot_len);
 685	iph->ttl = 100;
 686	iph->check = 0;
 687	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
 688
 689	skb_set_transport_header(skb, skb->len);
 690	udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len);
 691	get_random_bytes(&udph->source, sizeof(u16));
 692	get_random_bytes(&udph->dest, sizeof(u16));
 693	udph->len = htons(sizeof(struct udphdr) + data_len);
 694
 695	return skb;
 696}
 697
 698static void nsim_dev_trap_report(struct nsim_dev_port *nsim_dev_port)
 699{
 700	struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev;
 701	struct devlink *devlink = priv_to_devlink(nsim_dev);
 702	struct nsim_trap_data *nsim_trap_data;
 703	int i;
 704
 705	nsim_trap_data = nsim_dev->trap_data;
 706
 707	spin_lock(&nsim_trap_data->trap_lock);
 708	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
 709		struct flow_action_cookie *fa_cookie = NULL;
 710		struct nsim_trap_item *nsim_trap_item;
 711		struct sk_buff *skb;
 712		bool has_fa_cookie;
 713
 714		has_fa_cookie = nsim_traps_arr[i].metadata_cap &
 715				DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE;
 716
 717		nsim_trap_item = &nsim_trap_data->trap_items_arr[i];
 718		if (nsim_trap_item->action == DEVLINK_TRAP_ACTION_DROP)
 719			continue;
 720
 721		skb = nsim_dev_trap_skb_build();
 722		if (!skb)
 723			continue;
 724		skb->dev = nsim_dev_port->ns->netdev;
 725
 726		/* Trapped packets are usually passed to devlink in softIRQ,
 727		 * but in this case they are generated in a workqueue. Disable
 728		 * softIRQs to prevent lockdep from complaining about
 729		 * "incosistent lock state".
 730		 */
 731
 732		spin_lock_bh(&nsim_dev->fa_cookie_lock);
 733		fa_cookie = has_fa_cookie ? nsim_dev->fa_cookie : NULL;
 734		devlink_trap_report(devlink, skb, nsim_trap_item->trap_ctx,
 735				    &nsim_dev_port->devlink_port, fa_cookie);
 736		spin_unlock_bh(&nsim_dev->fa_cookie_lock);
 737		consume_skb(skb);
 738	}
 739	spin_unlock(&nsim_trap_data->trap_lock);
 740}
 741
 742#define NSIM_TRAP_REPORT_INTERVAL_MS	100
 743
 744static void nsim_dev_trap_report_work(struct work_struct *work)
 745{
 746	struct nsim_trap_data *nsim_trap_data;
 747	struct nsim_dev_port *nsim_dev_port;
 748	struct nsim_dev *nsim_dev;
 749
 750	nsim_trap_data = container_of(work, struct nsim_trap_data,
 751				      trap_report_dw.work);
 752	nsim_dev = nsim_trap_data->nsim_dev;
 753
 754	/* For each running port and enabled packet trap, generate a UDP
 755	 * packet with a random 5-tuple and report it.
 756	 */
 757	mutex_lock(&nsim_dev->port_list_lock);
 758	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list) {
 759		if (!netif_running(nsim_dev_port->ns->netdev))
 760			continue;
 761
 762		nsim_dev_trap_report(nsim_dev_port);
 763	}
 764	mutex_unlock(&nsim_dev->port_list_lock);
 765
 766	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
 767			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
 768}
 769
 770static int nsim_dev_traps_init(struct devlink *devlink)
 771{
 772	size_t policers_count = ARRAY_SIZE(nsim_trap_policers_arr);
 773	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 774	struct nsim_trap_data *nsim_trap_data;
 775	int err;
 776
 777	nsim_trap_data = kzalloc(sizeof(*nsim_trap_data), GFP_KERNEL);
 778	if (!nsim_trap_data)
 779		return -ENOMEM;
 780
 781	nsim_trap_data->trap_items_arr = kcalloc(ARRAY_SIZE(nsim_traps_arr),
 782						 sizeof(struct nsim_trap_item),
 783						 GFP_KERNEL);
 784	if (!nsim_trap_data->trap_items_arr) {
 785		err = -ENOMEM;
 786		goto err_trap_data_free;
 787	}
 788
 789	nsim_trap_data->trap_policers_cnt_arr = kcalloc(policers_count,
 790							sizeof(u64),
 791							GFP_KERNEL);
 792	if (!nsim_trap_data->trap_policers_cnt_arr) {
 793		err = -ENOMEM;
 794		goto err_trap_items_free;
 795	}
 796
 797	/* The lock is used to protect the action state of the registered
 798	 * traps. The value is written by user and read in delayed work when
 799	 * iterating over all the traps.
 800	 */
 801	spin_lock_init(&nsim_trap_data->trap_lock);
 802	nsim_trap_data->nsim_dev = nsim_dev;
 803	nsim_dev->trap_data = nsim_trap_data;
 804
 805	err = devlink_trap_policers_register(devlink, nsim_trap_policers_arr,
 806					     policers_count);
 807	if (err)
 808		goto err_trap_policers_cnt_free;
 809
 810	err = devlink_trap_groups_register(devlink, nsim_trap_groups_arr,
 811					   ARRAY_SIZE(nsim_trap_groups_arr));
 812	if (err)
 813		goto err_trap_policers_unregister;
 814
 815	err = devlink_traps_register(devlink, nsim_traps_arr,
 816				     ARRAY_SIZE(nsim_traps_arr), NULL);
 817	if (err)
 818		goto err_trap_groups_unregister;
 819
 820	INIT_DELAYED_WORK(&nsim_dev->trap_data->trap_report_dw,
 821			  nsim_dev_trap_report_work);
 822	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
 823			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
 824
 825	return 0;
 826
 827err_trap_groups_unregister:
 828	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
 829				       ARRAY_SIZE(nsim_trap_groups_arr));
 830err_trap_policers_unregister:
 831	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
 832					 ARRAY_SIZE(nsim_trap_policers_arr));
 833err_trap_policers_cnt_free:
 834	kfree(nsim_trap_data->trap_policers_cnt_arr);
 835err_trap_items_free:
 836	kfree(nsim_trap_data->trap_items_arr);
 837err_trap_data_free:
 838	kfree(nsim_trap_data);
 839	return err;
 840}
 841
 842static void nsim_dev_traps_exit(struct devlink *devlink)
 843{
 844	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 845
 846	cancel_delayed_work_sync(&nsim_dev->trap_data->trap_report_dw);
 847	devlink_traps_unregister(devlink, nsim_traps_arr,
 848				 ARRAY_SIZE(nsim_traps_arr));
 849	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
 850				       ARRAY_SIZE(nsim_trap_groups_arr));
 851	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
 852					 ARRAY_SIZE(nsim_trap_policers_arr));
 853	kfree(nsim_dev->trap_data->trap_policers_cnt_arr);
 854	kfree(nsim_dev->trap_data->trap_items_arr);
 855	kfree(nsim_dev->trap_data);
 856}
 857
 858static int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
 859				  struct netlink_ext_ack *extack);
 860static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev);
 861
 862static int nsim_dev_reload_down(struct devlink *devlink, bool netns_change,
 863				enum devlink_reload_action action, enum devlink_reload_limit limit,
 864				struct netlink_ext_ack *extack)
 865{
 866	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 867
 868	if (nsim_dev->dont_allow_reload) {
 869		/* For testing purposes, user set debugfs dont_allow_reload
 870		 * value to true. So forbid it.
 871		 */
 872		NL_SET_ERR_MSG_MOD(extack, "User forbid the reload for testing purposes");
 873		return -EOPNOTSUPP;
 874	}
 875
 876	nsim_dev_reload_destroy(nsim_dev);
 877	return 0;
 878}
 879
 880static int nsim_dev_reload_up(struct devlink *devlink, enum devlink_reload_action action,
 881			      enum devlink_reload_limit limit, u32 *actions_performed,
 882			      struct netlink_ext_ack *extack)
 883{
 884	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 885
 886	if (nsim_dev->fail_reload) {
 887		/* For testing purposes, user set debugfs fail_reload
 888		 * value to true. Fail right away.
 889		 */
 890		NL_SET_ERR_MSG_MOD(extack, "User setup the reload to fail for testing purposes");
 891		return -EINVAL;
 892	}
 893
 894	*actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
 895	return nsim_dev_reload_create(nsim_dev, extack);
 896}
 897
 898static int nsim_dev_info_get(struct devlink *devlink,
 899			     struct devlink_info_req *req,
 900			     struct netlink_ext_ack *extack)
 901{
 902	return devlink_info_driver_name_put(req, DRV_NAME);
 903}
 904
 905#define NSIM_DEV_FLASH_SIZE 500000
 906#define NSIM_DEV_FLASH_CHUNK_SIZE 1000
 907#define NSIM_DEV_FLASH_CHUNK_TIME_MS 10
 908
 909static int nsim_dev_flash_update(struct devlink *devlink,
 910				 struct devlink_flash_update_params *params,
 911				 struct netlink_ext_ack *extack)
 912{
 913	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 914	int i;
 915
 916	if ((params->overwrite_mask & ~nsim_dev->fw_update_overwrite_mask) != 0)
 917		return -EOPNOTSUPP;
 918
 919	if (nsim_dev->fw_update_status) {
 
 920		devlink_flash_update_status_notify(devlink,
 921						   "Preparing to flash",
 922						   params->component, 0, 0);
 923	}
 924
 925	for (i = 0; i < NSIM_DEV_FLASH_SIZE / NSIM_DEV_FLASH_CHUNK_SIZE; i++) {
 926		if (nsim_dev->fw_update_status)
 927			devlink_flash_update_status_notify(devlink, "Flashing",
 928							   params->component,
 929							   i * NSIM_DEV_FLASH_CHUNK_SIZE,
 930							   NSIM_DEV_FLASH_SIZE);
 931		msleep(NSIM_DEV_FLASH_CHUNK_TIME_MS);
 932	}
 933
 934	if (nsim_dev->fw_update_status) {
 935		devlink_flash_update_status_notify(devlink, "Flashing",
 936						   params->component,
 937						   NSIM_DEV_FLASH_SIZE,
 938						   NSIM_DEV_FLASH_SIZE);
 939		devlink_flash_update_timeout_notify(devlink, "Flash select",
 940						    params->component, 81);
 941		devlink_flash_update_status_notify(devlink, "Flashing done",
 942						   params->component, 0, 0);
 
 943	}
 944
 945	return 0;
 946}
 947
 948static struct nsim_trap_item *
 949nsim_dev_trap_item_lookup(struct nsim_dev *nsim_dev, u16 trap_id)
 950{
 951	struct nsim_trap_data *nsim_trap_data = nsim_dev->trap_data;
 952	int i;
 953
 954	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
 955		if (nsim_traps_arr[i].id == trap_id)
 956			return &nsim_trap_data->trap_items_arr[i];
 957	}
 958
 959	return NULL;
 960}
 961
 962static int nsim_dev_devlink_trap_init(struct devlink *devlink,
 963				      const struct devlink_trap *trap,
 964				      void *trap_ctx)
 965{
 966	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 967	struct nsim_trap_item *nsim_trap_item;
 968
 969	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
 970	if (WARN_ON(!nsim_trap_item))
 971		return -ENOENT;
 972
 973	nsim_trap_item->trap_ctx = trap_ctx;
 974	nsim_trap_item->action = trap->init_action;
 975
 976	return 0;
 977}
 978
 979static int
 980nsim_dev_devlink_trap_action_set(struct devlink *devlink,
 981				 const struct devlink_trap *trap,
 982				 enum devlink_trap_action action,
 983				 struct netlink_ext_ack *extack)
 984{
 985	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 986	struct nsim_trap_item *nsim_trap_item;
 987
 988	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
 989	if (WARN_ON(!nsim_trap_item))
 990		return -ENOENT;
 991
 992	spin_lock(&nsim_dev->trap_data->trap_lock);
 993	nsim_trap_item->action = action;
 994	spin_unlock(&nsim_dev->trap_data->trap_lock);
 995
 996	return 0;
 997}
 998
 999static int
1000nsim_dev_devlink_trap_group_set(struct devlink *devlink,
1001				const struct devlink_trap_group *group,
1002				const struct devlink_trap_policer *policer,
1003				struct netlink_ext_ack *extack)
1004{
1005	struct nsim_dev *nsim_dev = devlink_priv(devlink);
1006
1007	if (nsim_dev->fail_trap_group_set)
1008		return -EINVAL;
1009
1010	return 0;
1011}
1012
1013static int
1014nsim_dev_devlink_trap_policer_set(struct devlink *devlink,
1015				  const struct devlink_trap_policer *policer,
1016				  u64 rate, u64 burst,
1017				  struct netlink_ext_ack *extack)
1018{
1019	struct nsim_dev *nsim_dev = devlink_priv(devlink);
1020
1021	if (nsim_dev->fail_trap_policer_set) {
1022		NL_SET_ERR_MSG_MOD(extack, "User setup the operation to fail for testing purposes");
1023		return -EINVAL;
1024	}
1025
1026	return 0;
1027}
1028
1029static int
1030nsim_dev_devlink_trap_policer_counter_get(struct devlink *devlink,
1031					  const struct devlink_trap_policer *policer,
1032					  u64 *p_drops)
1033{
1034	struct nsim_dev *nsim_dev = devlink_priv(devlink);
1035	u64 *cnt;
1036
1037	if (nsim_dev->fail_trap_policer_counter_get)
1038		return -EINVAL;
1039
1040	cnt = &nsim_dev->trap_data->trap_policers_cnt_arr[policer->id - 1];
1041	*p_drops = (*cnt)++;
1042
1043	return 0;
1044}
1045
1046#define NSIM_LINK_SPEED_MAX     5000 /* Mbps */
1047#define NSIM_LINK_SPEED_UNIT    125000 /* 1 Mbps given in bytes/sec to avoid
1048					* u64 overflow during conversion from
1049					* bytes to bits.
1050					*/
1051
1052static int nsim_rate_bytes_to_units(char *name, u64 *rate, struct netlink_ext_ack *extack)
1053{
1054	u64 val;
1055	u32 rem;
1056
1057	val = div_u64_rem(*rate, NSIM_LINK_SPEED_UNIT, &rem);
1058	if (rem) {
1059		pr_err("%s rate value %lluBps not in link speed units of 1Mbps.\n",
1060		       name, *rate);
1061		NL_SET_ERR_MSG_MOD(extack, "TX rate value not in link speed units of 1Mbps.");
1062		return -EINVAL;
1063	}
1064
1065	if (val > NSIM_LINK_SPEED_MAX) {
1066		pr_err("%s rate value %lluMbps exceed link maximum speed 5000Mbps.\n",
1067		       name, val);
1068		NL_SET_ERR_MSG_MOD(extack, "TX rate value exceed link maximum speed 5000Mbps.");
1069		return -EINVAL;
1070	}
1071	*rate = val;
1072	return 0;
1073}
1074
1075static int nsim_leaf_tx_share_set(struct devlink_rate *devlink_rate, void *priv,
1076				  u64 tx_share, struct netlink_ext_ack *extack)
1077{
1078	struct nsim_dev_port *nsim_dev_port = priv;
1079	struct nsim_bus_dev *nsim_bus_dev = nsim_dev_port->ns->nsim_bus_dev;
1080	int vf_id = nsim_dev_port_index_to_vf_index(nsim_dev_port->port_index);
1081	int err;
1082
1083	err = nsim_rate_bytes_to_units("tx_share", &tx_share, extack);
1084	if (err)
1085		return err;
1086
1087	nsim_bus_dev->vfconfigs[vf_id].min_tx_rate = tx_share;
1088	return 0;
1089}
1090
1091static int nsim_leaf_tx_max_set(struct devlink_rate *devlink_rate, void *priv,
1092				u64 tx_max, struct netlink_ext_ack *extack)
1093{
1094	struct nsim_dev_port *nsim_dev_port = priv;
1095	struct nsim_bus_dev *nsim_bus_dev = nsim_dev_port->ns->nsim_bus_dev;
1096	int vf_id = nsim_dev_port_index_to_vf_index(nsim_dev_port->port_index);
1097	int err;
1098
1099	err = nsim_rate_bytes_to_units("tx_max", &tx_max, extack);
1100	if (err)
1101		return err;
1102
1103	nsim_bus_dev->vfconfigs[vf_id].max_tx_rate = tx_max;
1104	return 0;
1105}
1106
1107struct nsim_rate_node {
1108	struct dentry *ddir;
1109	struct dentry *rate_parent;
1110	char *parent_name;
1111	u16 tx_share;
1112	u16 tx_max;
1113};
1114
1115static int nsim_node_tx_share_set(struct devlink_rate *devlink_rate, void *priv,
1116				  u64 tx_share, struct netlink_ext_ack *extack)
1117{
1118	struct nsim_rate_node *nsim_node = priv;
1119	int err;
1120
1121	err = nsim_rate_bytes_to_units("tx_share", &tx_share, extack);
1122	if (err)
1123		return err;
1124
1125	nsim_node->tx_share = tx_share;
1126	return 0;
1127}
1128
1129static int nsim_node_tx_max_set(struct devlink_rate *devlink_rate, void *priv,
1130				u64 tx_max, struct netlink_ext_ack *extack)
1131{
1132	struct nsim_rate_node *nsim_node = priv;
1133	int err;
1134
1135	err = nsim_rate_bytes_to_units("tx_max", &tx_max, extack);
1136	if (err)
1137		return err;
1138
1139	nsim_node->tx_max = tx_max;
1140	return 0;
1141}
1142
1143static int nsim_rate_node_new(struct devlink_rate *node, void **priv,
1144			      struct netlink_ext_ack *extack)
1145{
1146	struct nsim_dev *nsim_dev = devlink_priv(node->devlink);
1147	struct nsim_rate_node *nsim_node;
1148
1149	if (!nsim_esw_mode_is_switchdev(nsim_dev)) {
1150		NL_SET_ERR_MSG_MOD(extack, "Node creation allowed only in switchdev mode.");
1151		return -EOPNOTSUPP;
1152	}
1153
1154	nsim_node = kzalloc(sizeof(*nsim_node), GFP_KERNEL);
1155	if (!nsim_node)
1156		return -ENOMEM;
1157
1158	nsim_node->ddir = debugfs_create_dir(node->name, nsim_dev->nodes_ddir);
1159
1160	debugfs_create_u16("tx_share", 0400, nsim_node->ddir, &nsim_node->tx_share);
1161	debugfs_create_u16("tx_max", 0400, nsim_node->ddir, &nsim_node->tx_max);
1162	nsim_node->rate_parent = debugfs_create_file("rate_parent", 0400,
1163						     nsim_node->ddir,
1164						     &nsim_node->parent_name,
1165						     &nsim_dev_rate_parent_fops);
1166
1167	*priv = nsim_node;
1168	return 0;
1169}
1170
1171static int nsim_rate_node_del(struct devlink_rate *node, void *priv,
1172			      struct netlink_ext_ack *extack)
1173{
1174	struct nsim_rate_node *nsim_node = priv;
1175
1176	debugfs_remove(nsim_node->rate_parent);
1177	debugfs_remove_recursive(nsim_node->ddir);
1178	kfree(nsim_node);
1179	return 0;
1180}
1181
1182static int nsim_rate_leaf_parent_set(struct devlink_rate *child,
1183				     struct devlink_rate *parent,
1184				     void *priv_child, void *priv_parent,
1185				     struct netlink_ext_ack *extack)
1186{
1187	struct nsim_dev_port *nsim_dev_port = priv_child;
1188
1189	if (parent)
1190		nsim_dev_port->parent_name = parent->name;
1191	else
1192		nsim_dev_port->parent_name = NULL;
1193	return 0;
1194}
1195
1196static int nsim_rate_node_parent_set(struct devlink_rate *child,
1197				     struct devlink_rate *parent,
1198				     void *priv_child, void *priv_parent,
1199				     struct netlink_ext_ack *extack)
1200{
1201	struct nsim_rate_node *nsim_node = priv_child;
1202
1203	if (parent)
1204		nsim_node->parent_name = parent->name;
1205	else
1206		nsim_node->parent_name = NULL;
1207	return 0;
1208}
1209
1210static int
1211nsim_dev_devlink_trap_drop_counter_get(struct devlink *devlink,
1212				       const struct devlink_trap *trap,
1213				       u64 *p_drops)
1214{
1215	struct nsim_dev *nsim_dev = devlink_priv(devlink);
1216	u64 *cnt;
1217
1218	if (nsim_dev->fail_trap_drop_counter_get)
1219		return -EINVAL;
1220
1221	cnt = &nsim_dev->trap_data->trap_pkt_cnt;
1222	*p_drops = (*cnt)++;
1223
1224	return 0;
1225}
1226
1227static const struct devlink_ops nsim_dev_devlink_ops = {
1228	.eswitch_mode_set = nsim_devlink_eswitch_mode_set,
1229	.eswitch_mode_get = nsim_devlink_eswitch_mode_get,
1230	.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT |
1231					 DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
1232	.reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT),
1233	.reload_down = nsim_dev_reload_down,
1234	.reload_up = nsim_dev_reload_up,
1235	.info_get = nsim_dev_info_get,
1236	.flash_update = nsim_dev_flash_update,
1237	.trap_init = nsim_dev_devlink_trap_init,
1238	.trap_action_set = nsim_dev_devlink_trap_action_set,
1239	.trap_group_set = nsim_dev_devlink_trap_group_set,
1240	.trap_policer_set = nsim_dev_devlink_trap_policer_set,
1241	.trap_policer_counter_get = nsim_dev_devlink_trap_policer_counter_get,
1242	.rate_leaf_tx_share_set = nsim_leaf_tx_share_set,
1243	.rate_leaf_tx_max_set = nsim_leaf_tx_max_set,
1244	.rate_node_tx_share_set = nsim_node_tx_share_set,
1245	.rate_node_tx_max_set = nsim_node_tx_max_set,
1246	.rate_node_new = nsim_rate_node_new,
1247	.rate_node_del = nsim_rate_node_del,
1248	.rate_leaf_parent_set = nsim_rate_leaf_parent_set,
1249	.rate_node_parent_set = nsim_rate_node_parent_set,
1250	.trap_drop_counter_get = nsim_dev_devlink_trap_drop_counter_get,
1251};
1252
1253#define NSIM_DEV_MAX_MACS_DEFAULT 32
1254#define NSIM_DEV_TEST1_DEFAULT true
1255
1256static int __nsim_dev_port_add(struct nsim_dev *nsim_dev, enum nsim_dev_port_type type,
1257			       unsigned int port_index)
1258{
1259	struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
1260	struct devlink_port_attrs attrs = {};
1261	struct nsim_dev_port *nsim_dev_port;
1262	struct devlink_port *devlink_port;
1263	int err;
1264
1265	if (type == NSIM_DEV_PORT_TYPE_VF && !nsim_bus_dev->num_vfs)
1266		return -EINVAL;
1267
1268	nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL);
1269	if (!nsim_dev_port)
1270		return -ENOMEM;
1271	nsim_dev_port->port_index = nsim_dev_port_index(type, port_index);
1272	nsim_dev_port->port_type = type;
1273
1274	devlink_port = &nsim_dev_port->devlink_port;
1275	if (nsim_dev_port_is_pf(nsim_dev_port)) {
1276		attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
1277		attrs.phys.port_number = port_index + 1;
1278	} else {
1279		attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
1280		attrs.pci_vf.pf = 0;
1281		attrs.pci_vf.vf = port_index;
1282	}
1283	memcpy(attrs.switch_id.id, nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
1284	attrs.switch_id.id_len = nsim_dev->switch_id.id_len;
1285	devlink_port_attrs_set(devlink_port, &attrs);
1286	err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port,
1287				    nsim_dev_port->port_index);
1288	if (err)
1289		goto err_port_free;
1290
1291	err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port);
1292	if (err)
1293		goto err_dl_port_unregister;
1294
1295	nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port);
1296	if (IS_ERR(nsim_dev_port->ns)) {
1297		err = PTR_ERR(nsim_dev_port->ns);
1298		goto err_port_debugfs_exit;
1299	}
1300
1301	if (nsim_dev_port_is_vf(nsim_dev_port)) {
1302		err = devlink_rate_leaf_create(&nsim_dev_port->devlink_port,
1303					       nsim_dev_port);
1304		if (err)
1305			goto err_nsim_destroy;
1306	}
1307
1308	devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev);
1309	list_add(&nsim_dev_port->list, &nsim_dev->port_list);
1310
1311	return 0;
1312
1313err_nsim_destroy:
1314	nsim_destroy(nsim_dev_port->ns);
1315err_port_debugfs_exit:
1316	nsim_dev_port_debugfs_exit(nsim_dev_port);
1317err_dl_port_unregister:
1318	devlink_port_unregister(devlink_port);
1319err_port_free:
1320	kfree(nsim_dev_port);
1321	return err;
1322}
1323
1324static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
1325{
1326	struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
1327
1328	list_del(&nsim_dev_port->list);
1329	if (nsim_dev_port_is_vf(nsim_dev_port))
1330		devlink_rate_leaf_destroy(&nsim_dev_port->devlink_port);
1331	devlink_port_type_clear(devlink_port);
1332	nsim_destroy(nsim_dev_port->ns);
1333	nsim_dev_port_debugfs_exit(nsim_dev_port);
1334	devlink_port_unregister(devlink_port);
1335	kfree(nsim_dev_port);
1336}
1337
1338static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
1339{
1340	struct nsim_dev_port *nsim_dev_port, *tmp;
1341
1342	mutex_lock(&nsim_dev->port_list_lock);
1343	list_for_each_entry_safe(nsim_dev_port, tmp,
1344				 &nsim_dev->port_list, list)
1345		__nsim_dev_port_del(nsim_dev_port);
1346	mutex_unlock(&nsim_dev->port_list_lock);
1347}
1348
1349static int nsim_dev_port_add_all(struct nsim_dev *nsim_dev,
1350				 unsigned int port_count)
1351{
1352	int i, err;
1353
1354	for (i = 0; i < port_count; i++) {
1355		err = __nsim_dev_port_add(nsim_dev, NSIM_DEV_PORT_TYPE_PF, i);
1356		if (err)
1357			goto err_port_del_all;
1358	}
1359	return 0;
1360
1361err_port_del_all:
1362	nsim_dev_port_del_all(nsim_dev);
1363	return err;
1364}
1365
1366static int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
1367				  struct netlink_ext_ack *extack)
1368{
1369	struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
1370	struct devlink *devlink;
1371	int err;
1372
1373	devlink = priv_to_devlink(nsim_dev);
1374	nsim_dev = devlink_priv(devlink);
1375	INIT_LIST_HEAD(&nsim_dev->port_list);
1376	mutex_init(&nsim_dev->port_list_lock);
1377	nsim_dev->fw_update_status = true;
1378	nsim_dev->fw_update_overwrite_mask = 0;
 
 
 
1379
1380	nsim_devlink_param_load_driverinit_values(devlink);
1381
1382	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
1383	if (err)
1384		return err;
1385
1386	err = nsim_dev_traps_init(devlink);
1387	if (err)
1388		goto err_dummy_region_exit;
1389
1390	nsim_dev->fib_data = nsim_fib_create(devlink, extack);
1391	if (IS_ERR(nsim_dev->fib_data)) {
1392		err = PTR_ERR(nsim_dev->fib_data);
1393		goto err_traps_exit;
1394	}
1395
1396	err = nsim_dev_health_init(nsim_dev, devlink);
1397	if (err)
1398		goto err_fib_destroy;
1399
1400	err = nsim_dev_psample_init(nsim_dev);
1401	if (err)
1402		goto err_health_exit;
1403
1404	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
1405	if (err)
1406		goto err_psample_exit;
1407
1408	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
1409						      0200,
1410						      nsim_dev->ddir,
1411						      nsim_dev,
1412						&nsim_dev_take_snapshot_fops);
1413	return 0;
1414
1415err_psample_exit:
1416	nsim_dev_psample_exit(nsim_dev);
1417err_health_exit:
1418	nsim_dev_health_exit(nsim_dev);
1419err_fib_destroy:
1420	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1421err_traps_exit:
1422	nsim_dev_traps_exit(devlink);
1423err_dummy_region_exit:
1424	nsim_dev_dummy_region_exit(nsim_dev);
 
 
1425	return err;
1426}
1427
1428int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
1429{
1430	struct nsim_dev *nsim_dev;
1431	struct devlink *devlink;
1432	int err;
1433
1434	devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev));
1435	if (!devlink)
1436		return -ENOMEM;
1437	devlink_net_set(devlink, nsim_bus_dev->initial_net);
1438	nsim_dev = devlink_priv(devlink);
1439	nsim_dev->nsim_bus_dev = nsim_bus_dev;
1440	nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
1441	get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
1442	INIT_LIST_HEAD(&nsim_dev->port_list);
1443	mutex_init(&nsim_dev->port_list_lock);
1444	nsim_dev->fw_update_status = true;
1445	nsim_dev->fw_update_overwrite_mask = 0;
1446	nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT;
1447	nsim_dev->test1 = NSIM_DEV_TEST1_DEFAULT;
1448	spin_lock_init(&nsim_dev->fa_cookie_lock);
1449
1450	dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
1451
1452	err = nsim_dev_resources_register(devlink);
1453	if (err)
1454		goto err_devlink_free;
1455
 
 
 
 
 
 
1456	err = devlink_register(devlink, &nsim_bus_dev->dev);
1457	if (err)
1458		goto err_resources_unregister;
1459
1460	err = devlink_params_register(devlink, nsim_devlink_params,
1461				      ARRAY_SIZE(nsim_devlink_params));
1462	if (err)
1463		goto err_dl_unregister;
1464	nsim_devlink_set_params_init_values(nsim_dev, devlink);
1465
1466	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
1467	if (err)
1468		goto err_params_unregister;
1469
1470	err = nsim_dev_traps_init(devlink);
1471	if (err)
1472		goto err_dummy_region_exit;
1473
1474	err = nsim_dev_debugfs_init(nsim_dev);
1475	if (err)
1476		goto err_traps_exit;
1477
1478	nsim_dev->fib_data = nsim_fib_create(devlink, NULL);
1479	if (IS_ERR(nsim_dev->fib_data)) {
1480		err = PTR_ERR(nsim_dev->fib_data);
1481		goto err_debugfs_exit;
1482	}
1483
1484	err = nsim_dev_health_init(nsim_dev, devlink);
1485	if (err)
1486		goto err_fib_destroy;
1487
1488	err = nsim_bpf_dev_init(nsim_dev);
1489	if (err)
1490		goto err_health_exit;
1491
1492	err = nsim_dev_psample_init(nsim_dev);
1493	if (err)
1494		goto err_bpf_dev_exit;
1495
1496	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
1497	if (err)
1498		goto err_psample_exit;
1499
1500	devlink_params_publish(devlink);
1501	devlink_reload_enable(devlink);
1502	nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_LEGACY;
1503	return 0;
1504
1505err_psample_exit:
1506	nsim_dev_psample_exit(nsim_dev);
1507err_bpf_dev_exit:
1508	nsim_bpf_dev_exit(nsim_dev);
1509err_health_exit:
1510	nsim_dev_health_exit(nsim_dev);
1511err_fib_destroy:
1512	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1513err_debugfs_exit:
1514	nsim_dev_debugfs_exit(nsim_dev);
1515err_traps_exit:
1516	nsim_dev_traps_exit(devlink);
1517err_dummy_region_exit:
1518	nsim_dev_dummy_region_exit(nsim_dev);
1519err_params_unregister:
1520	devlink_params_unregister(devlink, nsim_devlink_params,
1521				  ARRAY_SIZE(nsim_devlink_params));
1522err_dl_unregister:
1523	devlink_unregister(devlink);
 
 
1524err_resources_unregister:
1525	devlink_resources_unregister(devlink, NULL);
1526err_devlink_free:
1527	devlink_free(devlink);
1528	return err;
1529}
1530
1531static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev)
1532{
1533	struct devlink *devlink = priv_to_devlink(nsim_dev);
1534
1535	if (devlink_is_reload_failed(devlink))
1536		return;
1537	debugfs_remove(nsim_dev->take_snapshot);
1538
1539	mutex_lock(&nsim_dev->nsim_bus_dev->vfs_lock);
1540	if (nsim_dev->nsim_bus_dev->num_vfs)
1541		nsim_bus_dev_vfs_disable(nsim_dev->nsim_bus_dev);
1542	mutex_unlock(&nsim_dev->nsim_bus_dev->vfs_lock);
1543
1544	nsim_dev_port_del_all(nsim_dev);
1545	nsim_dev_psample_exit(nsim_dev);
1546	nsim_dev_health_exit(nsim_dev);
1547	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1548	nsim_dev_traps_exit(devlink);
1549	nsim_dev_dummy_region_exit(nsim_dev);
1550	mutex_destroy(&nsim_dev->port_list_lock);
 
1551}
1552
1553void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
1554{
1555	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1556	struct devlink *devlink = priv_to_devlink(nsim_dev);
1557
1558	devlink_reload_disable(devlink);
1559
1560	nsim_dev_reload_destroy(nsim_dev);
1561
1562	nsim_bpf_dev_exit(nsim_dev);
1563	nsim_dev_debugfs_exit(nsim_dev);
1564	devlink_params_unregister(devlink, nsim_devlink_params,
1565				  ARRAY_SIZE(nsim_devlink_params));
1566	devlink_unregister(devlink);
1567	devlink_resources_unregister(devlink, NULL);
1568	devlink_free(devlink);
1569}
1570
1571static struct nsim_dev_port *
1572__nsim_dev_port_lookup(struct nsim_dev *nsim_dev, enum nsim_dev_port_type type,
1573		       unsigned int port_index)
1574{
1575	struct nsim_dev_port *nsim_dev_port;
1576
1577	port_index = nsim_dev_port_index(type, port_index);
1578	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
1579		if (nsim_dev_port->port_index == port_index)
1580			return nsim_dev_port;
1581	return NULL;
1582}
1583
1584int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev, enum nsim_dev_port_type type,
1585		      unsigned int port_index)
1586{
1587	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1588	int err;
1589
1590	mutex_lock(&nsim_dev->port_list_lock);
1591	if (__nsim_dev_port_lookup(nsim_dev, type, port_index))
1592		err = -EEXIST;
1593	else
1594		err = __nsim_dev_port_add(nsim_dev, type, port_index);
1595	mutex_unlock(&nsim_dev->port_list_lock);
1596	return err;
1597}
1598
1599int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev, enum nsim_dev_port_type type,
1600		      unsigned int port_index)
1601{
1602	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1603	struct nsim_dev_port *nsim_dev_port;
1604	int err = 0;
1605
1606	mutex_lock(&nsim_dev->port_list_lock);
1607	nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, type, port_index);
1608	if (!nsim_dev_port)
1609		err = -ENOENT;
1610	else
1611		__nsim_dev_port_del(nsim_dev_port);
1612	mutex_unlock(&nsim_dev->port_list_lock);
1613	return err;
1614}
1615
1616int nsim_dev_init(void)
1617{
1618	nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
1619	return PTR_ERR_OR_ZERO(nsim_dev_ddir);
1620}
1621
1622void nsim_dev_exit(void)
1623{
1624	debugfs_remove_recursive(nsim_dev_ddir);
1625}
v5.9
   1/*
   2 * Copyright (c) 2018 Cumulus Networks. All rights reserved.
   3 * Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com>
   4 * Copyright (c) 2019 Mellanox Technologies. All rights reserved.
   5 *
   6 * This software is licensed under the GNU General License Version 2,
   7 * June 1991 as shown in the file COPYING in the top-level directory of this
   8 * source tree.
   9 *
  10 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
  11 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  12 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  13 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
  14 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
  15 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  16 */
  17
  18#include <linux/debugfs.h>
  19#include <linux/device.h>
  20#include <linux/etherdevice.h>
  21#include <linux/inet.h>
  22#include <linux/jiffies.h>
  23#include <linux/kernel.h>
  24#include <linux/list.h>
  25#include <linux/mutex.h>
  26#include <linux/random.h>
  27#include <linux/rtnetlink.h>
  28#include <linux/workqueue.h>
  29#include <net/devlink.h>
  30#include <net/ip.h>
  31#include <net/flow_offload.h>
  32#include <uapi/linux/devlink.h>
  33#include <uapi/linux/ip.h>
  34#include <uapi/linux/udp.h>
  35
  36#include "netdevsim.h"
  37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  38static struct dentry *nsim_dev_ddir;
  39
  40#define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)
  41
  42static int
  43nsim_dev_take_snapshot(struct devlink *devlink, struct netlink_ext_ack *extack,
 
 
  44		       u8 **data)
  45{
  46	void *dummy_data;
  47
  48	dummy_data = kmalloc(NSIM_DEV_DUMMY_REGION_SIZE, GFP_KERNEL);
  49	if (!dummy_data)
  50		return -ENOMEM;
  51
  52	get_random_bytes(dummy_data, NSIM_DEV_DUMMY_REGION_SIZE);
  53
  54	*data = dummy_data;
  55
  56	return 0;
  57}
  58
  59static ssize_t nsim_dev_take_snapshot_write(struct file *file,
  60					    const char __user *data,
  61					    size_t count, loff_t *ppos)
  62{
  63	struct nsim_dev *nsim_dev = file->private_data;
  64	struct devlink *devlink;
  65	u8 *dummy_data;
  66	int err;
  67	u32 id;
  68
  69	devlink = priv_to_devlink(nsim_dev);
  70
  71	err = nsim_dev_take_snapshot(devlink, NULL, &dummy_data);
  72	if (err)
  73		return err;
  74
  75	err = devlink_region_snapshot_id_get(devlink, &id);
  76	if (err) {
  77		pr_err("Failed to get snapshot id\n");
  78		kfree(dummy_data);
  79		return err;
  80	}
  81	err = devlink_region_snapshot_create(nsim_dev->dummy_region,
  82					     dummy_data, id);
  83	devlink_region_snapshot_id_put(devlink, id);
  84	if (err) {
  85		pr_err("Failed to create region snapshot\n");
  86		kfree(dummy_data);
  87		return err;
  88	}
  89
  90	return count;
  91}
  92
  93static const struct file_operations nsim_dev_take_snapshot_fops = {
  94	.open = simple_open,
  95	.write = nsim_dev_take_snapshot_write,
  96	.llseek = generic_file_llseek,
 
  97};
  98
  99static ssize_t nsim_dev_trap_fa_cookie_read(struct file *file,
 100					    char __user *data,
 101					    size_t count, loff_t *ppos)
 102{
 103	struct nsim_dev *nsim_dev = file->private_data;
 104	struct flow_action_cookie *fa_cookie;
 105	unsigned int buf_len;
 106	ssize_t ret;
 107	char *buf;
 108
 109	spin_lock(&nsim_dev->fa_cookie_lock);
 110	fa_cookie = nsim_dev->fa_cookie;
 111	if (!fa_cookie) {
 112		ret = -EINVAL;
 113		goto errout;
 114	}
 115	buf_len = fa_cookie->cookie_len * 2;
 116	buf = kmalloc(buf_len, GFP_ATOMIC);
 117	if (!buf) {
 118		ret = -ENOMEM;
 119		goto errout;
 120	}
 121	bin2hex(buf, fa_cookie->cookie, fa_cookie->cookie_len);
 122	spin_unlock(&nsim_dev->fa_cookie_lock);
 123
 124	ret = simple_read_from_buffer(data, count, ppos, buf, buf_len);
 125
 126	kfree(buf);
 127	return ret;
 128
 129errout:
 130	spin_unlock(&nsim_dev->fa_cookie_lock);
 131	return ret;
 132}
 133
 134static ssize_t nsim_dev_trap_fa_cookie_write(struct file *file,
 135					     const char __user *data,
 136					     size_t count, loff_t *ppos)
 137{
 138	struct nsim_dev *nsim_dev = file->private_data;
 139	struct flow_action_cookie *fa_cookie;
 140	size_t cookie_len;
 141	ssize_t ret;
 142	char *buf;
 143
 144	if (*ppos != 0)
 145		return -EINVAL;
 146	cookie_len = (count - 1) / 2;
 147	if ((count - 1) % 2)
 148		return -EINVAL;
 149	buf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
 150	if (!buf)
 151		return -ENOMEM;
 152
 153	ret = simple_write_to_buffer(buf, count, ppos, data, count);
 154	if (ret < 0)
 155		goto free_buf;
 156
 157	fa_cookie = kmalloc(sizeof(*fa_cookie) + cookie_len,
 158			    GFP_KERNEL | __GFP_NOWARN);
 159	if (!fa_cookie) {
 160		ret = -ENOMEM;
 161		goto free_buf;
 162	}
 163
 164	fa_cookie->cookie_len = cookie_len;
 165	ret = hex2bin(fa_cookie->cookie, buf, cookie_len);
 166	if (ret)
 167		goto free_fa_cookie;
 168	kfree(buf);
 169
 170	spin_lock(&nsim_dev->fa_cookie_lock);
 171	kfree(nsim_dev->fa_cookie);
 172	nsim_dev->fa_cookie = fa_cookie;
 173	spin_unlock(&nsim_dev->fa_cookie_lock);
 174
 175	return count;
 176
 177free_fa_cookie:
 178	kfree(fa_cookie);
 179free_buf:
 180	kfree(buf);
 181	return ret;
 182}
 183
 184static const struct file_operations nsim_dev_trap_fa_cookie_fops = {
 185	.open = simple_open,
 186	.read = nsim_dev_trap_fa_cookie_read,
 187	.write = nsim_dev_trap_fa_cookie_write,
 188	.llseek = generic_file_llseek,
 
 
 
 
 
 
 
 
 
 189};
 190
 191static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
 192{
 193	char dev_ddir_name[sizeof(DRV_NAME) + 10];
 
 194
 195	sprintf(dev_ddir_name, DRV_NAME "%u", nsim_dev->nsim_bus_dev->dev.id);
 196	nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir);
 197	if (IS_ERR(nsim_dev->ddir))
 198		return PTR_ERR(nsim_dev->ddir);
 199	nsim_dev->ports_ddir = debugfs_create_dir("ports", nsim_dev->ddir);
 200	if (IS_ERR(nsim_dev->ports_ddir))
 201		return PTR_ERR(nsim_dev->ports_ddir);
 202	debugfs_create_bool("fw_update_status", 0600, nsim_dev->ddir,
 203			    &nsim_dev->fw_update_status);
 
 
 204	debugfs_create_u32("max_macs", 0600, nsim_dev->ddir,
 205			   &nsim_dev->max_macs);
 206	debugfs_create_bool("test1", 0600, nsim_dev->ddir,
 207			    &nsim_dev->test1);
 208	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
 209						      0200,
 210						      nsim_dev->ddir,
 211						      nsim_dev,
 212						&nsim_dev_take_snapshot_fops);
 213	debugfs_create_bool("dont_allow_reload", 0600, nsim_dev->ddir,
 214			    &nsim_dev->dont_allow_reload);
 215	debugfs_create_bool("fail_reload", 0600, nsim_dev->ddir,
 216			    &nsim_dev->fail_reload);
 217	debugfs_create_file("trap_flow_action_cookie", 0600, nsim_dev->ddir,
 218			    nsim_dev, &nsim_dev_trap_fa_cookie_fops);
 219	debugfs_create_bool("fail_trap_group_set", 0600,
 220			    nsim_dev->ddir,
 221			    &nsim_dev->fail_trap_group_set);
 222	debugfs_create_bool("fail_trap_policer_set", 0600,
 223			    nsim_dev->ddir,
 224			    &nsim_dev->fail_trap_policer_set);
 225	debugfs_create_bool("fail_trap_policer_counter_get", 0600,
 226			    nsim_dev->ddir,
 227			    &nsim_dev->fail_trap_policer_counter_get);
 
 
 
 
 
 
 
 
 
 
 
 
 
 228	nsim_udp_tunnels_debugfs_create(nsim_dev);
 229	return 0;
 
 
 
 
 
 230}
 231
 232static void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev)
 233{
 
 234	debugfs_remove_recursive(nsim_dev->ports_ddir);
 235	debugfs_remove_recursive(nsim_dev->ddir);
 236}
 237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 238static int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev,
 239				      struct nsim_dev_port *nsim_dev_port)
 240{
 
 
 241	char port_ddir_name[16];
 242	char dev_link_name[32];
 243
 244	sprintf(port_ddir_name, "%u", nsim_dev_port->port_index);
 245	nsim_dev_port->ddir = debugfs_create_dir(port_ddir_name,
 246						 nsim_dev->ports_ddir);
 247	if (IS_ERR(nsim_dev_port->ddir))
 248		return PTR_ERR(nsim_dev_port->ddir);
 249
 250	sprintf(dev_link_name, "../../../" DRV_NAME "%u",
 251		nsim_dev->nsim_bus_dev->dev.id);
 
 
 
 
 
 
 
 
 
 
 
 
 252	debugfs_create_symlink("dev", nsim_dev_port->ddir, dev_link_name);
 253
 254	return 0;
 255}
 256
 257static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port)
 258{
 259	debugfs_remove_recursive(nsim_dev_port->ddir);
 260}
 261
 262static int nsim_dev_resources_register(struct devlink *devlink)
 263{
 264	struct devlink_resource_size_params params = {
 265		.size_max = (u64)-1,
 266		.size_granularity = 1,
 267		.unit = DEVLINK_RESOURCE_UNIT_ENTRY
 268	};
 269	int err;
 270
 271	/* Resources for IPv4 */
 272	err = devlink_resource_register(devlink, "IPv4", (u64)-1,
 273					NSIM_RESOURCE_IPV4,
 274					DEVLINK_RESOURCE_ID_PARENT_TOP,
 275					&params);
 276	if (err) {
 277		pr_err("Failed to register IPv4 top resource\n");
 278		goto out;
 279	}
 280
 281	err = devlink_resource_register(devlink, "fib", (u64)-1,
 282					NSIM_RESOURCE_IPV4_FIB,
 283					NSIM_RESOURCE_IPV4, &params);
 284	if (err) {
 285		pr_err("Failed to register IPv4 FIB resource\n");
 286		return err;
 287	}
 288
 289	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
 290					NSIM_RESOURCE_IPV4_FIB_RULES,
 291					NSIM_RESOURCE_IPV4, &params);
 292	if (err) {
 293		pr_err("Failed to register IPv4 FIB rules resource\n");
 294		return err;
 295	}
 296
 297	/* Resources for IPv6 */
 298	err = devlink_resource_register(devlink, "IPv6", (u64)-1,
 299					NSIM_RESOURCE_IPV6,
 300					DEVLINK_RESOURCE_ID_PARENT_TOP,
 301					&params);
 302	if (err) {
 303		pr_err("Failed to register IPv6 top resource\n");
 304		goto out;
 305	}
 306
 307	err = devlink_resource_register(devlink, "fib", (u64)-1,
 308					NSIM_RESOURCE_IPV6_FIB,
 309					NSIM_RESOURCE_IPV6, &params);
 310	if (err) {
 311		pr_err("Failed to register IPv6 FIB resource\n");
 312		return err;
 313	}
 314
 315	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
 316					NSIM_RESOURCE_IPV6_FIB_RULES,
 317					NSIM_RESOURCE_IPV6, &params);
 318	if (err) {
 319		pr_err("Failed to register IPv6 FIB rules resource\n");
 320		return err;
 321	}
 322
 
 
 
 
 
 
 323out:
 324	return err;
 325}
 326
 327enum nsim_devlink_param_id {
 328	NSIM_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
 329	NSIM_DEVLINK_PARAM_ID_TEST1,
 330};
 331
 332static const struct devlink_param nsim_devlink_params[] = {
 333	DEVLINK_PARAM_GENERIC(MAX_MACS,
 334			      BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
 335			      NULL, NULL, NULL),
 336	DEVLINK_PARAM_DRIVER(NSIM_DEVLINK_PARAM_ID_TEST1,
 337			     "test1", DEVLINK_PARAM_TYPE_BOOL,
 338			     BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
 339			     NULL, NULL, NULL),
 340};
 341
 342static void nsim_devlink_set_params_init_values(struct nsim_dev *nsim_dev,
 343						struct devlink *devlink)
 344{
 345	union devlink_param_value value;
 346
 347	value.vu32 = nsim_dev->max_macs;
 348	devlink_param_driverinit_value_set(devlink,
 349					   DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
 350					   value);
 351	value.vbool = nsim_dev->test1;
 352	devlink_param_driverinit_value_set(devlink,
 353					   NSIM_DEVLINK_PARAM_ID_TEST1,
 354					   value);
 355}
 356
 357static void nsim_devlink_param_load_driverinit_values(struct devlink *devlink)
 358{
 359	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 360	union devlink_param_value saved_value;
 361	int err;
 362
 363	err = devlink_param_driverinit_value_get(devlink,
 364						 DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
 365						 &saved_value);
 366	if (!err)
 367		nsim_dev->max_macs = saved_value.vu32;
 368	err = devlink_param_driverinit_value_get(devlink,
 369						 NSIM_DEVLINK_PARAM_ID_TEST1,
 370						 &saved_value);
 371	if (!err)
 372		nsim_dev->test1 = saved_value.vbool;
 373}
 374
 375#define NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX 16
 376
 377static const struct devlink_region_ops dummy_region_ops = {
 378	.name = "dummy",
 379	.destructor = &kfree,
 380	.snapshot = nsim_dev_take_snapshot,
 381};
 382
 383static int nsim_dev_dummy_region_init(struct nsim_dev *nsim_dev,
 384				      struct devlink *devlink)
 385{
 386	nsim_dev->dummy_region =
 387		devlink_region_create(devlink, &dummy_region_ops,
 388				      NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX,
 389				      NSIM_DEV_DUMMY_REGION_SIZE);
 390	return PTR_ERR_OR_ZERO(nsim_dev->dummy_region);
 391}
 392
 393static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
 394{
 395	devlink_region_destroy(nsim_dev->dummy_region);
 396}
 397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 398struct nsim_trap_item {
 399	void *trap_ctx;
 400	enum devlink_trap_action action;
 401};
 402
 403struct nsim_trap_data {
 404	struct delayed_work trap_report_dw;
 405	struct nsim_trap_item *trap_items_arr;
 406	u64 *trap_policers_cnt_arr;
 
 407	struct nsim_dev *nsim_dev;
 408	spinlock_t trap_lock;	/* Protects trap_items_arr */
 409};
 410
 411/* All driver-specific traps must be documented in
 412 * Documentation/networking/devlink/netdevsim.rst
 413 */
 414enum {
 415	NSIM_TRAP_ID_BASE = DEVLINK_TRAP_GENERIC_ID_MAX,
 416	NSIM_TRAP_ID_FID_MISS,
 417};
 418
 419#define NSIM_TRAP_NAME_FID_MISS "fid_miss"
 420
 421#define NSIM_TRAP_METADATA DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
 422
 423#define NSIM_TRAP_DROP(_id, _group_id)					      \
 424	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
 425			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 426			     NSIM_TRAP_METADATA)
 427#define NSIM_TRAP_DROP_EXT(_id, _group_id, _metadata)			      \
 428	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
 429			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 430			     NSIM_TRAP_METADATA | (_metadata))
 431#define NSIM_TRAP_EXCEPTION(_id, _group_id)				      \
 432	DEVLINK_TRAP_GENERIC(EXCEPTION, TRAP, _id,			      \
 433			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 434			     NSIM_TRAP_METADATA)
 435#define NSIM_TRAP_CONTROL(_id, _group_id, _action)			      \
 436	DEVLINK_TRAP_GENERIC(CONTROL, _action, _id,			      \
 437			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 438			     NSIM_TRAP_METADATA)
 439#define NSIM_TRAP_DRIVER_EXCEPTION(_id, _group_id)			      \
 440	DEVLINK_TRAP_DRIVER(EXCEPTION, TRAP, NSIM_TRAP_ID_##_id,	      \
 441			    NSIM_TRAP_NAME_##_id,			      \
 442			    DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
 443			    NSIM_TRAP_METADATA)
 444
 445#define NSIM_DEV_TRAP_POLICER_MIN_RATE	1
 446#define NSIM_DEV_TRAP_POLICER_MAX_RATE	8000
 447#define NSIM_DEV_TRAP_POLICER_MIN_BURST	8
 448#define NSIM_DEV_TRAP_POLICER_MAX_BURST	65536
 449
 450#define NSIM_TRAP_POLICER(_id, _rate, _burst)				      \
 451	DEVLINK_TRAP_POLICER(_id, _rate, _burst,			      \
 452			     NSIM_DEV_TRAP_POLICER_MAX_RATE,		      \
 453			     NSIM_DEV_TRAP_POLICER_MIN_RATE,		      \
 454			     NSIM_DEV_TRAP_POLICER_MAX_BURST,		      \
 455			     NSIM_DEV_TRAP_POLICER_MIN_BURST)
 456
 457static const struct devlink_trap_policer nsim_trap_policers_arr[] = {
 458	NSIM_TRAP_POLICER(1, 1000, 128),
 459	NSIM_TRAP_POLICER(2, 2000, 256),
 460	NSIM_TRAP_POLICER(3, 3000, 512),
 461};
 462
 463static const struct devlink_trap_group nsim_trap_groups_arr[] = {
 464	DEVLINK_TRAP_GROUP_GENERIC(L2_DROPS, 0),
 465	DEVLINK_TRAP_GROUP_GENERIC(L3_DROPS, 1),
 466	DEVLINK_TRAP_GROUP_GENERIC(L3_EXCEPTIONS, 1),
 467	DEVLINK_TRAP_GROUP_GENERIC(BUFFER_DROPS, 2),
 468	DEVLINK_TRAP_GROUP_GENERIC(ACL_DROPS, 3),
 469	DEVLINK_TRAP_GROUP_GENERIC(MC_SNOOPING, 3),
 470};
 471
 472static const struct devlink_trap nsim_traps_arr[] = {
 473	NSIM_TRAP_DROP(SMAC_MC, L2_DROPS),
 474	NSIM_TRAP_DROP(VLAN_TAG_MISMATCH, L2_DROPS),
 475	NSIM_TRAP_DROP(INGRESS_VLAN_FILTER, L2_DROPS),
 476	NSIM_TRAP_DROP(INGRESS_STP_FILTER, L2_DROPS),
 477	NSIM_TRAP_DROP(EMPTY_TX_LIST, L2_DROPS),
 478	NSIM_TRAP_DROP(PORT_LOOPBACK_FILTER, L2_DROPS),
 479	NSIM_TRAP_DRIVER_EXCEPTION(FID_MISS, L2_DROPS),
 480	NSIM_TRAP_DROP(BLACKHOLE_ROUTE, L3_DROPS),
 481	NSIM_TRAP_EXCEPTION(TTL_ERROR, L3_EXCEPTIONS),
 482	NSIM_TRAP_DROP(TAIL_DROP, BUFFER_DROPS),
 483	NSIM_TRAP_DROP_EXT(INGRESS_FLOW_ACTION_DROP, ACL_DROPS,
 484			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
 485	NSIM_TRAP_DROP_EXT(EGRESS_FLOW_ACTION_DROP, ACL_DROPS,
 486			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
 487	NSIM_TRAP_CONTROL(IGMP_QUERY, MC_SNOOPING, MIRROR),
 488	NSIM_TRAP_CONTROL(IGMP_V1_REPORT, MC_SNOOPING, TRAP),
 489};
 490
 491#define NSIM_TRAP_L4_DATA_LEN 100
 492
 493static struct sk_buff *nsim_dev_trap_skb_build(void)
 494{
 495	int tot_len, data_len = NSIM_TRAP_L4_DATA_LEN;
 496	struct sk_buff *skb;
 497	struct udphdr *udph;
 498	struct ethhdr *eth;
 499	struct iphdr *iph;
 500
 501	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
 502	if (!skb)
 503		return NULL;
 504	tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len;
 505
 506	skb_reset_mac_header(skb);
 507	eth = skb_put(skb, sizeof(struct ethhdr));
 508	eth_random_addr(eth->h_dest);
 509	eth_random_addr(eth->h_source);
 510	eth->h_proto = htons(ETH_P_IP);
 511	skb->protocol = htons(ETH_P_IP);
 512
 513	skb_set_network_header(skb, skb->len);
 514	iph = skb_put(skb, sizeof(struct iphdr));
 515	iph->protocol = IPPROTO_UDP;
 516	iph->saddr = in_aton("192.0.2.1");
 517	iph->daddr = in_aton("198.51.100.1");
 518	iph->version = 0x4;
 519	iph->frag_off = 0;
 520	iph->ihl = 0x5;
 521	iph->tot_len = htons(tot_len);
 522	iph->ttl = 100;
 523	iph->check = 0;
 524	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
 525
 526	skb_set_transport_header(skb, skb->len);
 527	udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len);
 528	get_random_bytes(&udph->source, sizeof(u16));
 529	get_random_bytes(&udph->dest, sizeof(u16));
 530	udph->len = htons(sizeof(struct udphdr) + data_len);
 531
 532	return skb;
 533}
 534
 535static void nsim_dev_trap_report(struct nsim_dev_port *nsim_dev_port)
 536{
 537	struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev;
 538	struct devlink *devlink = priv_to_devlink(nsim_dev);
 539	struct nsim_trap_data *nsim_trap_data;
 540	int i;
 541
 542	nsim_trap_data = nsim_dev->trap_data;
 543
 544	spin_lock(&nsim_trap_data->trap_lock);
 545	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
 546		struct flow_action_cookie *fa_cookie = NULL;
 547		struct nsim_trap_item *nsim_trap_item;
 548		struct sk_buff *skb;
 549		bool has_fa_cookie;
 550
 551		has_fa_cookie = nsim_traps_arr[i].metadata_cap &
 552				DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE;
 553
 554		nsim_trap_item = &nsim_trap_data->trap_items_arr[i];
 555		if (nsim_trap_item->action == DEVLINK_TRAP_ACTION_DROP)
 556			continue;
 557
 558		skb = nsim_dev_trap_skb_build();
 559		if (!skb)
 560			continue;
 561		skb->dev = nsim_dev_port->ns->netdev;
 562
 563		/* Trapped packets are usually passed to devlink in softIRQ,
 564		 * but in this case they are generated in a workqueue. Disable
 565		 * softIRQs to prevent lockdep from complaining about
 566		 * "incosistent lock state".
 567		 */
 568
 569		spin_lock_bh(&nsim_dev->fa_cookie_lock);
 570		fa_cookie = has_fa_cookie ? nsim_dev->fa_cookie : NULL;
 571		devlink_trap_report(devlink, skb, nsim_trap_item->trap_ctx,
 572				    &nsim_dev_port->devlink_port, fa_cookie);
 573		spin_unlock_bh(&nsim_dev->fa_cookie_lock);
 574		consume_skb(skb);
 575	}
 576	spin_unlock(&nsim_trap_data->trap_lock);
 577}
 578
 579#define NSIM_TRAP_REPORT_INTERVAL_MS	100
 580
 581static void nsim_dev_trap_report_work(struct work_struct *work)
 582{
 583	struct nsim_trap_data *nsim_trap_data;
 584	struct nsim_dev_port *nsim_dev_port;
 585	struct nsim_dev *nsim_dev;
 586
 587	nsim_trap_data = container_of(work, struct nsim_trap_data,
 588				      trap_report_dw.work);
 589	nsim_dev = nsim_trap_data->nsim_dev;
 590
 591	/* For each running port and enabled packet trap, generate a UDP
 592	 * packet with a random 5-tuple and report it.
 593	 */
 594	mutex_lock(&nsim_dev->port_list_lock);
 595	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list) {
 596		if (!netif_running(nsim_dev_port->ns->netdev))
 597			continue;
 598
 599		nsim_dev_trap_report(nsim_dev_port);
 600	}
 601	mutex_unlock(&nsim_dev->port_list_lock);
 602
 603	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
 604			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
 605}
 606
 607static int nsim_dev_traps_init(struct devlink *devlink)
 608{
 609	size_t policers_count = ARRAY_SIZE(nsim_trap_policers_arr);
 610	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 611	struct nsim_trap_data *nsim_trap_data;
 612	int err;
 613
 614	nsim_trap_data = kzalloc(sizeof(*nsim_trap_data), GFP_KERNEL);
 615	if (!nsim_trap_data)
 616		return -ENOMEM;
 617
 618	nsim_trap_data->trap_items_arr = kcalloc(ARRAY_SIZE(nsim_traps_arr),
 619						 sizeof(struct nsim_trap_item),
 620						 GFP_KERNEL);
 621	if (!nsim_trap_data->trap_items_arr) {
 622		err = -ENOMEM;
 623		goto err_trap_data_free;
 624	}
 625
 626	nsim_trap_data->trap_policers_cnt_arr = kcalloc(policers_count,
 627							sizeof(u64),
 628							GFP_KERNEL);
 629	if (!nsim_trap_data->trap_policers_cnt_arr) {
 630		err = -ENOMEM;
 631		goto err_trap_items_free;
 632	}
 633
 634	/* The lock is used to protect the action state of the registered
 635	 * traps. The value is written by user and read in delayed work when
 636	 * iterating over all the traps.
 637	 */
 638	spin_lock_init(&nsim_trap_data->trap_lock);
 639	nsim_trap_data->nsim_dev = nsim_dev;
 640	nsim_dev->trap_data = nsim_trap_data;
 641
 642	err = devlink_trap_policers_register(devlink, nsim_trap_policers_arr,
 643					     policers_count);
 644	if (err)
 645		goto err_trap_policers_cnt_free;
 646
 647	err = devlink_trap_groups_register(devlink, nsim_trap_groups_arr,
 648					   ARRAY_SIZE(nsim_trap_groups_arr));
 649	if (err)
 650		goto err_trap_policers_unregister;
 651
 652	err = devlink_traps_register(devlink, nsim_traps_arr,
 653				     ARRAY_SIZE(nsim_traps_arr), NULL);
 654	if (err)
 655		goto err_trap_groups_unregister;
 656
 657	INIT_DELAYED_WORK(&nsim_dev->trap_data->trap_report_dw,
 658			  nsim_dev_trap_report_work);
 659	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
 660			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
 661
 662	return 0;
 663
 664err_trap_groups_unregister:
 665	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
 666				       ARRAY_SIZE(nsim_trap_groups_arr));
 667err_trap_policers_unregister:
 668	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
 669					 ARRAY_SIZE(nsim_trap_policers_arr));
 670err_trap_policers_cnt_free:
 671	kfree(nsim_trap_data->trap_policers_cnt_arr);
 672err_trap_items_free:
 673	kfree(nsim_trap_data->trap_items_arr);
 674err_trap_data_free:
 675	kfree(nsim_trap_data);
 676	return err;
 677}
 678
 679static void nsim_dev_traps_exit(struct devlink *devlink)
 680{
 681	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 682
 683	cancel_delayed_work_sync(&nsim_dev->trap_data->trap_report_dw);
 684	devlink_traps_unregister(devlink, nsim_traps_arr,
 685				 ARRAY_SIZE(nsim_traps_arr));
 686	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
 687				       ARRAY_SIZE(nsim_trap_groups_arr));
 688	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
 689					 ARRAY_SIZE(nsim_trap_policers_arr));
 690	kfree(nsim_dev->trap_data->trap_policers_cnt_arr);
 691	kfree(nsim_dev->trap_data->trap_items_arr);
 692	kfree(nsim_dev->trap_data);
 693}
 694
 695static int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
 696				  struct netlink_ext_ack *extack);
 697static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev);
 698
 699static int nsim_dev_reload_down(struct devlink *devlink, bool netns_change,
 
 700				struct netlink_ext_ack *extack)
 701{
 702	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 703
 704	if (nsim_dev->dont_allow_reload) {
 705		/* For testing purposes, user set debugfs dont_allow_reload
 706		 * value to true. So forbid it.
 707		 */
 708		NL_SET_ERR_MSG_MOD(extack, "User forbid the reload for testing purposes");
 709		return -EOPNOTSUPP;
 710	}
 711
 712	nsim_dev_reload_destroy(nsim_dev);
 713	return 0;
 714}
 715
 716static int nsim_dev_reload_up(struct devlink *devlink,
 
 717			      struct netlink_ext_ack *extack)
 718{
 719	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 720
 721	if (nsim_dev->fail_reload) {
 722		/* For testing purposes, user set debugfs fail_reload
 723		 * value to true. Fail right away.
 724		 */
 725		NL_SET_ERR_MSG_MOD(extack, "User setup the reload to fail for testing purposes");
 726		return -EINVAL;
 727	}
 728
 
 729	return nsim_dev_reload_create(nsim_dev, extack);
 730}
 731
 732static int nsim_dev_info_get(struct devlink *devlink,
 733			     struct devlink_info_req *req,
 734			     struct netlink_ext_ack *extack)
 735{
 736	return devlink_info_driver_name_put(req, DRV_NAME);
 737}
 738
 739#define NSIM_DEV_FLASH_SIZE 500000
 740#define NSIM_DEV_FLASH_CHUNK_SIZE 1000
 741#define NSIM_DEV_FLASH_CHUNK_TIME_MS 10
 742
 743static int nsim_dev_flash_update(struct devlink *devlink, const char *file_name,
 744				 const char *component,
 745				 struct netlink_ext_ack *extack)
 746{
 747	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 748	int i;
 749
 
 
 
 750	if (nsim_dev->fw_update_status) {
 751		devlink_flash_update_begin_notify(devlink);
 752		devlink_flash_update_status_notify(devlink,
 753						   "Preparing to flash",
 754						   component, 0, 0);
 755	}
 756
 757	for (i = 0; i < NSIM_DEV_FLASH_SIZE / NSIM_DEV_FLASH_CHUNK_SIZE; i++) {
 758		if (nsim_dev->fw_update_status)
 759			devlink_flash_update_status_notify(devlink, "Flashing",
 760							   component,
 761							   i * NSIM_DEV_FLASH_CHUNK_SIZE,
 762							   NSIM_DEV_FLASH_SIZE);
 763		msleep(NSIM_DEV_FLASH_CHUNK_TIME_MS);
 764	}
 765
 766	if (nsim_dev->fw_update_status) {
 767		devlink_flash_update_status_notify(devlink, "Flashing",
 768						   component,
 769						   NSIM_DEV_FLASH_SIZE,
 770						   NSIM_DEV_FLASH_SIZE);
 
 
 771		devlink_flash_update_status_notify(devlink, "Flashing done",
 772						   component, 0, 0);
 773		devlink_flash_update_end_notify(devlink);
 774	}
 775
 776	return 0;
 777}
 778
 779static struct nsim_trap_item *
 780nsim_dev_trap_item_lookup(struct nsim_dev *nsim_dev, u16 trap_id)
 781{
 782	struct nsim_trap_data *nsim_trap_data = nsim_dev->trap_data;
 783	int i;
 784
 785	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
 786		if (nsim_traps_arr[i].id == trap_id)
 787			return &nsim_trap_data->trap_items_arr[i];
 788	}
 789
 790	return NULL;
 791}
 792
 793static int nsim_dev_devlink_trap_init(struct devlink *devlink,
 794				      const struct devlink_trap *trap,
 795				      void *trap_ctx)
 796{
 797	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 798	struct nsim_trap_item *nsim_trap_item;
 799
 800	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
 801	if (WARN_ON(!nsim_trap_item))
 802		return -ENOENT;
 803
 804	nsim_trap_item->trap_ctx = trap_ctx;
 805	nsim_trap_item->action = trap->init_action;
 806
 807	return 0;
 808}
 809
 810static int
 811nsim_dev_devlink_trap_action_set(struct devlink *devlink,
 812				 const struct devlink_trap *trap,
 813				 enum devlink_trap_action action,
 814				 struct netlink_ext_ack *extack)
 815{
 816	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 817	struct nsim_trap_item *nsim_trap_item;
 818
 819	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
 820	if (WARN_ON(!nsim_trap_item))
 821		return -ENOENT;
 822
 823	spin_lock(&nsim_dev->trap_data->trap_lock);
 824	nsim_trap_item->action = action;
 825	spin_unlock(&nsim_dev->trap_data->trap_lock);
 826
 827	return 0;
 828}
 829
 830static int
 831nsim_dev_devlink_trap_group_set(struct devlink *devlink,
 832				const struct devlink_trap_group *group,
 833				const struct devlink_trap_policer *policer,
 834				struct netlink_ext_ack *extack)
 835{
 836	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 837
 838	if (nsim_dev->fail_trap_group_set)
 839		return -EINVAL;
 840
 841	return 0;
 842}
 843
 844static int
 845nsim_dev_devlink_trap_policer_set(struct devlink *devlink,
 846				  const struct devlink_trap_policer *policer,
 847				  u64 rate, u64 burst,
 848				  struct netlink_ext_ack *extack)
 849{
 850	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 851
 852	if (nsim_dev->fail_trap_policer_set) {
 853		NL_SET_ERR_MSG_MOD(extack, "User setup the operation to fail for testing purposes");
 854		return -EINVAL;
 855	}
 856
 857	return 0;
 858}
 859
 860static int
 861nsim_dev_devlink_trap_policer_counter_get(struct devlink *devlink,
 862					  const struct devlink_trap_policer *policer,
 863					  u64 *p_drops)
 864{
 865	struct nsim_dev *nsim_dev = devlink_priv(devlink);
 866	u64 *cnt;
 867
 868	if (nsim_dev->fail_trap_policer_counter_get)
 869		return -EINVAL;
 870
 871	cnt = &nsim_dev->trap_data->trap_policers_cnt_arr[policer->id - 1];
 872	*p_drops = (*cnt)++;
 873
 874	return 0;
 875}
 876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 877static const struct devlink_ops nsim_dev_devlink_ops = {
 
 
 
 
 
 878	.reload_down = nsim_dev_reload_down,
 879	.reload_up = nsim_dev_reload_up,
 880	.info_get = nsim_dev_info_get,
 881	.flash_update = nsim_dev_flash_update,
 882	.trap_init = nsim_dev_devlink_trap_init,
 883	.trap_action_set = nsim_dev_devlink_trap_action_set,
 884	.trap_group_set = nsim_dev_devlink_trap_group_set,
 885	.trap_policer_set = nsim_dev_devlink_trap_policer_set,
 886	.trap_policer_counter_get = nsim_dev_devlink_trap_policer_counter_get,
 
 
 
 
 
 
 
 
 
 887};
 888
 889#define NSIM_DEV_MAX_MACS_DEFAULT 32
 890#define NSIM_DEV_TEST1_DEFAULT true
 891
 892static int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
 893			       unsigned int port_index)
 894{
 
 895	struct devlink_port_attrs attrs = {};
 896	struct nsim_dev_port *nsim_dev_port;
 897	struct devlink_port *devlink_port;
 898	int err;
 899
 
 
 
 900	nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL);
 901	if (!nsim_dev_port)
 902		return -ENOMEM;
 903	nsim_dev_port->port_index = port_index;
 
 904
 905	devlink_port = &nsim_dev_port->devlink_port;
 906	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
 907	attrs.phys.port_number = port_index + 1;
 
 
 
 
 
 
 908	memcpy(attrs.switch_id.id, nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
 909	attrs.switch_id.id_len = nsim_dev->switch_id.id_len;
 910	devlink_port_attrs_set(devlink_port, &attrs);
 911	err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port,
 912				    port_index);
 913	if (err)
 914		goto err_port_free;
 915
 916	err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port);
 917	if (err)
 918		goto err_dl_port_unregister;
 919
 920	nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port);
 921	if (IS_ERR(nsim_dev_port->ns)) {
 922		err = PTR_ERR(nsim_dev_port->ns);
 923		goto err_port_debugfs_exit;
 924	}
 925
 
 
 
 
 
 
 
 926	devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev);
 927	list_add(&nsim_dev_port->list, &nsim_dev->port_list);
 928
 929	return 0;
 930
 
 
 931err_port_debugfs_exit:
 932	nsim_dev_port_debugfs_exit(nsim_dev_port);
 933err_dl_port_unregister:
 934	devlink_port_unregister(devlink_port);
 935err_port_free:
 936	kfree(nsim_dev_port);
 937	return err;
 938}
 939
 940static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
 941{
 942	struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
 943
 944	list_del(&nsim_dev_port->list);
 
 
 945	devlink_port_type_clear(devlink_port);
 946	nsim_destroy(nsim_dev_port->ns);
 947	nsim_dev_port_debugfs_exit(nsim_dev_port);
 948	devlink_port_unregister(devlink_port);
 949	kfree(nsim_dev_port);
 950}
 951
 952static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
 953{
 954	struct nsim_dev_port *nsim_dev_port, *tmp;
 955
 956	mutex_lock(&nsim_dev->port_list_lock);
 957	list_for_each_entry_safe(nsim_dev_port, tmp,
 958				 &nsim_dev->port_list, list)
 959		__nsim_dev_port_del(nsim_dev_port);
 960	mutex_unlock(&nsim_dev->port_list_lock);
 961}
 962
 963static int nsim_dev_port_add_all(struct nsim_dev *nsim_dev,
 964				 unsigned int port_count)
 965{
 966	int i, err;
 967
 968	for (i = 0; i < port_count; i++) {
 969		err = __nsim_dev_port_add(nsim_dev, i);
 970		if (err)
 971			goto err_port_del_all;
 972	}
 973	return 0;
 974
 975err_port_del_all:
 976	nsim_dev_port_del_all(nsim_dev);
 977	return err;
 978}
 979
 980static int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
 981				  struct netlink_ext_ack *extack)
 982{
 983	struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
 984	struct devlink *devlink;
 985	int err;
 986
 987	devlink = priv_to_devlink(nsim_dev);
 988	nsim_dev = devlink_priv(devlink);
 989	INIT_LIST_HEAD(&nsim_dev->port_list);
 990	mutex_init(&nsim_dev->port_list_lock);
 991	nsim_dev->fw_update_status = true;
 992
 993	nsim_dev->fib_data = nsim_fib_create(devlink, extack);
 994	if (IS_ERR(nsim_dev->fib_data))
 995		return PTR_ERR(nsim_dev->fib_data);
 996
 997	nsim_devlink_param_load_driverinit_values(devlink);
 998
 999	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
1000	if (err)
1001		goto err_fib_destroy;
1002
1003	err = nsim_dev_traps_init(devlink);
1004	if (err)
1005		goto err_dummy_region_exit;
1006
 
 
 
 
 
 
1007	err = nsim_dev_health_init(nsim_dev, devlink);
1008	if (err)
1009		goto err_traps_exit;
 
 
 
 
1010
1011	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
1012	if (err)
1013		goto err_health_exit;
1014
1015	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
1016						      0200,
1017						      nsim_dev->ddir,
1018						      nsim_dev,
1019						&nsim_dev_take_snapshot_fops);
1020	return 0;
1021
 
 
1022err_health_exit:
1023	nsim_dev_health_exit(nsim_dev);
 
 
1024err_traps_exit:
1025	nsim_dev_traps_exit(devlink);
1026err_dummy_region_exit:
1027	nsim_dev_dummy_region_exit(nsim_dev);
1028err_fib_destroy:
1029	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1030	return err;
1031}
1032
1033int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
1034{
1035	struct nsim_dev *nsim_dev;
1036	struct devlink *devlink;
1037	int err;
1038
1039	devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev));
1040	if (!devlink)
1041		return -ENOMEM;
1042	devlink_net_set(devlink, nsim_bus_dev->initial_net);
1043	nsim_dev = devlink_priv(devlink);
1044	nsim_dev->nsim_bus_dev = nsim_bus_dev;
1045	nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
1046	get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
1047	INIT_LIST_HEAD(&nsim_dev->port_list);
1048	mutex_init(&nsim_dev->port_list_lock);
1049	nsim_dev->fw_update_status = true;
 
1050	nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT;
1051	nsim_dev->test1 = NSIM_DEV_TEST1_DEFAULT;
1052	spin_lock_init(&nsim_dev->fa_cookie_lock);
1053
1054	dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
1055
1056	err = nsim_dev_resources_register(devlink);
1057	if (err)
1058		goto err_devlink_free;
1059
1060	nsim_dev->fib_data = nsim_fib_create(devlink, NULL);
1061	if (IS_ERR(nsim_dev->fib_data)) {
1062		err = PTR_ERR(nsim_dev->fib_data);
1063		goto err_resources_unregister;
1064	}
1065
1066	err = devlink_register(devlink, &nsim_bus_dev->dev);
1067	if (err)
1068		goto err_fib_destroy;
1069
1070	err = devlink_params_register(devlink, nsim_devlink_params,
1071				      ARRAY_SIZE(nsim_devlink_params));
1072	if (err)
1073		goto err_dl_unregister;
1074	nsim_devlink_set_params_init_values(nsim_dev, devlink);
1075
1076	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
1077	if (err)
1078		goto err_params_unregister;
1079
1080	err = nsim_dev_traps_init(devlink);
1081	if (err)
1082		goto err_dummy_region_exit;
1083
1084	err = nsim_dev_debugfs_init(nsim_dev);
1085	if (err)
1086		goto err_traps_exit;
1087
 
 
 
 
 
 
1088	err = nsim_dev_health_init(nsim_dev, devlink);
1089	if (err)
1090		goto err_debugfs_exit;
1091
1092	err = nsim_bpf_dev_init(nsim_dev);
1093	if (err)
1094		goto err_health_exit;
1095
 
 
 
 
1096	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
1097	if (err)
1098		goto err_bpf_dev_exit;
1099
1100	devlink_params_publish(devlink);
1101	devlink_reload_enable(devlink);
 
1102	return 0;
1103
 
 
1104err_bpf_dev_exit:
1105	nsim_bpf_dev_exit(nsim_dev);
1106err_health_exit:
1107	nsim_dev_health_exit(nsim_dev);
 
 
1108err_debugfs_exit:
1109	nsim_dev_debugfs_exit(nsim_dev);
1110err_traps_exit:
1111	nsim_dev_traps_exit(devlink);
1112err_dummy_region_exit:
1113	nsim_dev_dummy_region_exit(nsim_dev);
1114err_params_unregister:
1115	devlink_params_unregister(devlink, nsim_devlink_params,
1116				  ARRAY_SIZE(nsim_devlink_params));
1117err_dl_unregister:
1118	devlink_unregister(devlink);
1119err_fib_destroy:
1120	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1121err_resources_unregister:
1122	devlink_resources_unregister(devlink, NULL);
1123err_devlink_free:
1124	devlink_free(devlink);
1125	return err;
1126}
1127
1128static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev)
1129{
1130	struct devlink *devlink = priv_to_devlink(nsim_dev);
1131
1132	if (devlink_is_reload_failed(devlink))
1133		return;
1134	debugfs_remove(nsim_dev->take_snapshot);
 
 
 
 
 
 
1135	nsim_dev_port_del_all(nsim_dev);
 
1136	nsim_dev_health_exit(nsim_dev);
 
1137	nsim_dev_traps_exit(devlink);
1138	nsim_dev_dummy_region_exit(nsim_dev);
1139	mutex_destroy(&nsim_dev->port_list_lock);
1140	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1141}
1142
1143void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
1144{
1145	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1146	struct devlink *devlink = priv_to_devlink(nsim_dev);
1147
1148	devlink_reload_disable(devlink);
1149
1150	nsim_dev_reload_destroy(nsim_dev);
1151
1152	nsim_bpf_dev_exit(nsim_dev);
1153	nsim_dev_debugfs_exit(nsim_dev);
1154	devlink_params_unregister(devlink, nsim_devlink_params,
1155				  ARRAY_SIZE(nsim_devlink_params));
1156	devlink_unregister(devlink);
1157	devlink_resources_unregister(devlink, NULL);
1158	devlink_free(devlink);
1159}
1160
1161static struct nsim_dev_port *
1162__nsim_dev_port_lookup(struct nsim_dev *nsim_dev, unsigned int port_index)
 
1163{
1164	struct nsim_dev_port *nsim_dev_port;
1165
 
1166	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
1167		if (nsim_dev_port->port_index == port_index)
1168			return nsim_dev_port;
1169	return NULL;
1170}
1171
1172int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
1173		      unsigned int port_index)
1174{
1175	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1176	int err;
1177
1178	mutex_lock(&nsim_dev->port_list_lock);
1179	if (__nsim_dev_port_lookup(nsim_dev, port_index))
1180		err = -EEXIST;
1181	else
1182		err = __nsim_dev_port_add(nsim_dev, port_index);
1183	mutex_unlock(&nsim_dev->port_list_lock);
1184	return err;
1185}
1186
1187int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
1188		      unsigned int port_index)
1189{
1190	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1191	struct nsim_dev_port *nsim_dev_port;
1192	int err = 0;
1193
1194	mutex_lock(&nsim_dev->port_list_lock);
1195	nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, port_index);
1196	if (!nsim_dev_port)
1197		err = -ENOENT;
1198	else
1199		__nsim_dev_port_del(nsim_dev_port);
1200	mutex_unlock(&nsim_dev->port_list_lock);
1201	return err;
1202}
1203
1204int nsim_dev_init(void)
1205{
1206	nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
1207	return PTR_ERR_OR_ZERO(nsim_dev_ddir);
1208}
1209
1210void nsim_dev_exit(void)
1211{
1212	debugfs_remove_recursive(nsim_dev_ddir);
1213}