xref: /openbmc/linux/drivers/net/netdevsim/dev.c (revision 911b8eac)
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 
38 static struct dentry *nsim_dev_ddir;
39 
40 #define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)
41 
42 static int
43 nsim_dev_take_snapshot(struct devlink *devlink,
44 		       const struct devlink_region_ops *ops,
45 		       struct netlink_ext_ack *extack,
46 		       u8 **data)
47 {
48 	void *dummy_data;
49 
50 	dummy_data = kmalloc(NSIM_DEV_DUMMY_REGION_SIZE, GFP_KERNEL);
51 	if (!dummy_data)
52 		return -ENOMEM;
53 
54 	get_random_bytes(dummy_data, NSIM_DEV_DUMMY_REGION_SIZE);
55 
56 	*data = dummy_data;
57 
58 	return 0;
59 }
60 
61 static ssize_t nsim_dev_take_snapshot_write(struct file *file,
62 					    const char __user *data,
63 					    size_t count, loff_t *ppos)
64 {
65 	struct nsim_dev *nsim_dev = file->private_data;
66 	struct devlink *devlink;
67 	u8 *dummy_data;
68 	int err;
69 	u32 id;
70 
71 	devlink = priv_to_devlink(nsim_dev);
72 
73 	err = nsim_dev_take_snapshot(devlink, NULL, NULL, &dummy_data);
74 	if (err)
75 		return err;
76 
77 	err = devlink_region_snapshot_id_get(devlink, &id);
78 	if (err) {
79 		pr_err("Failed to get snapshot id\n");
80 		kfree(dummy_data);
81 		return err;
82 	}
83 	err = devlink_region_snapshot_create(nsim_dev->dummy_region,
84 					     dummy_data, id);
85 	devlink_region_snapshot_id_put(devlink, id);
86 	if (err) {
87 		pr_err("Failed to create region snapshot\n");
88 		kfree(dummy_data);
89 		return err;
90 	}
91 
92 	return count;
93 }
94 
95 static const struct file_operations nsim_dev_take_snapshot_fops = {
96 	.open = simple_open,
97 	.write = nsim_dev_take_snapshot_write,
98 	.llseek = generic_file_llseek,
99 };
100 
101 static ssize_t nsim_dev_trap_fa_cookie_read(struct file *file,
102 					    char __user *data,
103 					    size_t count, loff_t *ppos)
104 {
105 	struct nsim_dev *nsim_dev = file->private_data;
106 	struct flow_action_cookie *fa_cookie;
107 	unsigned int buf_len;
108 	ssize_t ret;
109 	char *buf;
110 
111 	spin_lock(&nsim_dev->fa_cookie_lock);
112 	fa_cookie = nsim_dev->fa_cookie;
113 	if (!fa_cookie) {
114 		ret = -EINVAL;
115 		goto errout;
116 	}
117 	buf_len = fa_cookie->cookie_len * 2;
118 	buf = kmalloc(buf_len, GFP_ATOMIC);
119 	if (!buf) {
120 		ret = -ENOMEM;
121 		goto errout;
122 	}
123 	bin2hex(buf, fa_cookie->cookie, fa_cookie->cookie_len);
124 	spin_unlock(&nsim_dev->fa_cookie_lock);
125 
126 	ret = simple_read_from_buffer(data, count, ppos, buf, buf_len);
127 
128 	kfree(buf);
129 	return ret;
130 
131 errout:
132 	spin_unlock(&nsim_dev->fa_cookie_lock);
133 	return ret;
134 }
135 
136 static ssize_t nsim_dev_trap_fa_cookie_write(struct file *file,
137 					     const char __user *data,
138 					     size_t count, loff_t *ppos)
139 {
140 	struct nsim_dev *nsim_dev = file->private_data;
141 	struct flow_action_cookie *fa_cookie;
142 	size_t cookie_len;
143 	ssize_t ret;
144 	char *buf;
145 
146 	if (*ppos != 0)
147 		return -EINVAL;
148 	cookie_len = (count - 1) / 2;
149 	if ((count - 1) % 2)
150 		return -EINVAL;
151 	buf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
152 	if (!buf)
153 		return -ENOMEM;
154 
155 	ret = simple_write_to_buffer(buf, count, ppos, data, count);
156 	if (ret < 0)
157 		goto free_buf;
158 
159 	fa_cookie = kmalloc(sizeof(*fa_cookie) + cookie_len,
160 			    GFP_KERNEL | __GFP_NOWARN);
161 	if (!fa_cookie) {
162 		ret = -ENOMEM;
163 		goto free_buf;
164 	}
165 
166 	fa_cookie->cookie_len = cookie_len;
167 	ret = hex2bin(fa_cookie->cookie, buf, cookie_len);
168 	if (ret)
169 		goto free_fa_cookie;
170 	kfree(buf);
171 
172 	spin_lock(&nsim_dev->fa_cookie_lock);
173 	kfree(nsim_dev->fa_cookie);
174 	nsim_dev->fa_cookie = fa_cookie;
175 	spin_unlock(&nsim_dev->fa_cookie_lock);
176 
177 	return count;
178 
179 free_fa_cookie:
180 	kfree(fa_cookie);
181 free_buf:
182 	kfree(buf);
183 	return ret;
184 }
185 
186 static const struct file_operations nsim_dev_trap_fa_cookie_fops = {
187 	.open = simple_open,
188 	.read = nsim_dev_trap_fa_cookie_read,
189 	.write = nsim_dev_trap_fa_cookie_write,
190 	.llseek = generic_file_llseek,
191 };
192 
193 static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
194 {
195 	char dev_ddir_name[sizeof(DRV_NAME) + 10];
196 
197 	sprintf(dev_ddir_name, DRV_NAME "%u", nsim_dev->nsim_bus_dev->dev.id);
198 	nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir);
199 	if (IS_ERR(nsim_dev->ddir))
200 		return PTR_ERR(nsim_dev->ddir);
201 	nsim_dev->ports_ddir = debugfs_create_dir("ports", nsim_dev->ddir);
202 	if (IS_ERR(nsim_dev->ports_ddir))
203 		return PTR_ERR(nsim_dev->ports_ddir);
204 	debugfs_create_bool("fw_update_status", 0600, nsim_dev->ddir,
205 			    &nsim_dev->fw_update_status);
206 	debugfs_create_u32("fw_update_overwrite_mask", 0600, nsim_dev->ddir,
207 			    &nsim_dev->fw_update_overwrite_mask);
208 	debugfs_create_u32("max_macs", 0600, nsim_dev->ddir,
209 			   &nsim_dev->max_macs);
210 	debugfs_create_bool("test1", 0600, nsim_dev->ddir,
211 			    &nsim_dev->test1);
212 	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
213 						      0200,
214 						      nsim_dev->ddir,
215 						      nsim_dev,
216 						&nsim_dev_take_snapshot_fops);
217 	debugfs_create_bool("dont_allow_reload", 0600, nsim_dev->ddir,
218 			    &nsim_dev->dont_allow_reload);
219 	debugfs_create_bool("fail_reload", 0600, nsim_dev->ddir,
220 			    &nsim_dev->fail_reload);
221 	debugfs_create_file("trap_flow_action_cookie", 0600, nsim_dev->ddir,
222 			    nsim_dev, &nsim_dev_trap_fa_cookie_fops);
223 	debugfs_create_bool("fail_trap_group_set", 0600,
224 			    nsim_dev->ddir,
225 			    &nsim_dev->fail_trap_group_set);
226 	debugfs_create_bool("fail_trap_policer_set", 0600,
227 			    nsim_dev->ddir,
228 			    &nsim_dev->fail_trap_policer_set);
229 	debugfs_create_bool("fail_trap_policer_counter_get", 0600,
230 			    nsim_dev->ddir,
231 			    &nsim_dev->fail_trap_policer_counter_get);
232 	nsim_udp_tunnels_debugfs_create(nsim_dev);
233 	return 0;
234 }
235 
236 static void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev)
237 {
238 	debugfs_remove_recursive(nsim_dev->ports_ddir);
239 	debugfs_remove_recursive(nsim_dev->ddir);
240 }
241 
242 static int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev,
243 				      struct nsim_dev_port *nsim_dev_port)
244 {
245 	char port_ddir_name[16];
246 	char dev_link_name[32];
247 
248 	sprintf(port_ddir_name, "%u", nsim_dev_port->port_index);
249 	nsim_dev_port->ddir = debugfs_create_dir(port_ddir_name,
250 						 nsim_dev->ports_ddir);
251 	if (IS_ERR(nsim_dev_port->ddir))
252 		return PTR_ERR(nsim_dev_port->ddir);
253 
254 	sprintf(dev_link_name, "../../../" DRV_NAME "%u",
255 		nsim_dev->nsim_bus_dev->dev.id);
256 	debugfs_create_symlink("dev", nsim_dev_port->ddir, dev_link_name);
257 
258 	return 0;
259 }
260 
261 static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port)
262 {
263 	debugfs_remove_recursive(nsim_dev_port->ddir);
264 }
265 
266 static int nsim_dev_resources_register(struct devlink *devlink)
267 {
268 	struct devlink_resource_size_params params = {
269 		.size_max = (u64)-1,
270 		.size_granularity = 1,
271 		.unit = DEVLINK_RESOURCE_UNIT_ENTRY
272 	};
273 	int err;
274 
275 	/* Resources for IPv4 */
276 	err = devlink_resource_register(devlink, "IPv4", (u64)-1,
277 					NSIM_RESOURCE_IPV4,
278 					DEVLINK_RESOURCE_ID_PARENT_TOP,
279 					&params);
280 	if (err) {
281 		pr_err("Failed to register IPv4 top resource\n");
282 		goto out;
283 	}
284 
285 	err = devlink_resource_register(devlink, "fib", (u64)-1,
286 					NSIM_RESOURCE_IPV4_FIB,
287 					NSIM_RESOURCE_IPV4, &params);
288 	if (err) {
289 		pr_err("Failed to register IPv4 FIB resource\n");
290 		return err;
291 	}
292 
293 	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
294 					NSIM_RESOURCE_IPV4_FIB_RULES,
295 					NSIM_RESOURCE_IPV4, &params);
296 	if (err) {
297 		pr_err("Failed to register IPv4 FIB rules resource\n");
298 		return err;
299 	}
300 
301 	/* Resources for IPv6 */
302 	err = devlink_resource_register(devlink, "IPv6", (u64)-1,
303 					NSIM_RESOURCE_IPV6,
304 					DEVLINK_RESOURCE_ID_PARENT_TOP,
305 					&params);
306 	if (err) {
307 		pr_err("Failed to register IPv6 top resource\n");
308 		goto out;
309 	}
310 
311 	err = devlink_resource_register(devlink, "fib", (u64)-1,
312 					NSIM_RESOURCE_IPV6_FIB,
313 					NSIM_RESOURCE_IPV6, &params);
314 	if (err) {
315 		pr_err("Failed to register IPv6 FIB resource\n");
316 		return err;
317 	}
318 
319 	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
320 					NSIM_RESOURCE_IPV6_FIB_RULES,
321 					NSIM_RESOURCE_IPV6, &params);
322 	if (err) {
323 		pr_err("Failed to register IPv6 FIB rules resource\n");
324 		return err;
325 	}
326 
327 out:
328 	return err;
329 }
330 
331 enum nsim_devlink_param_id {
332 	NSIM_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
333 	NSIM_DEVLINK_PARAM_ID_TEST1,
334 };
335 
336 static const struct devlink_param nsim_devlink_params[] = {
337 	DEVLINK_PARAM_GENERIC(MAX_MACS,
338 			      BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
339 			      NULL, NULL, NULL),
340 	DEVLINK_PARAM_DRIVER(NSIM_DEVLINK_PARAM_ID_TEST1,
341 			     "test1", DEVLINK_PARAM_TYPE_BOOL,
342 			     BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
343 			     NULL, NULL, NULL),
344 };
345 
346 static void nsim_devlink_set_params_init_values(struct nsim_dev *nsim_dev,
347 						struct devlink *devlink)
348 {
349 	union devlink_param_value value;
350 
351 	value.vu32 = nsim_dev->max_macs;
352 	devlink_param_driverinit_value_set(devlink,
353 					   DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
354 					   value);
355 	value.vbool = nsim_dev->test1;
356 	devlink_param_driverinit_value_set(devlink,
357 					   NSIM_DEVLINK_PARAM_ID_TEST1,
358 					   value);
359 }
360 
361 static void nsim_devlink_param_load_driverinit_values(struct devlink *devlink)
362 {
363 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
364 	union devlink_param_value saved_value;
365 	int err;
366 
367 	err = devlink_param_driverinit_value_get(devlink,
368 						 DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
369 						 &saved_value);
370 	if (!err)
371 		nsim_dev->max_macs = saved_value.vu32;
372 	err = devlink_param_driverinit_value_get(devlink,
373 						 NSIM_DEVLINK_PARAM_ID_TEST1,
374 						 &saved_value);
375 	if (!err)
376 		nsim_dev->test1 = saved_value.vbool;
377 }
378 
379 #define NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX 16
380 
381 static const struct devlink_region_ops dummy_region_ops = {
382 	.name = "dummy",
383 	.destructor = &kfree,
384 	.snapshot = nsim_dev_take_snapshot,
385 };
386 
387 static int nsim_dev_dummy_region_init(struct nsim_dev *nsim_dev,
388 				      struct devlink *devlink)
389 {
390 	nsim_dev->dummy_region =
391 		devlink_region_create(devlink, &dummy_region_ops,
392 				      NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX,
393 				      NSIM_DEV_DUMMY_REGION_SIZE);
394 	return PTR_ERR_OR_ZERO(nsim_dev->dummy_region);
395 }
396 
397 static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
398 {
399 	devlink_region_destroy(nsim_dev->dummy_region);
400 }
401 
402 struct nsim_trap_item {
403 	void *trap_ctx;
404 	enum devlink_trap_action action;
405 };
406 
407 struct nsim_trap_data {
408 	struct delayed_work trap_report_dw;
409 	struct nsim_trap_item *trap_items_arr;
410 	u64 *trap_policers_cnt_arr;
411 	struct nsim_dev *nsim_dev;
412 	spinlock_t trap_lock;	/* Protects trap_items_arr */
413 };
414 
415 /* All driver-specific traps must be documented in
416  * Documentation/networking/devlink/netdevsim.rst
417  */
418 enum {
419 	NSIM_TRAP_ID_BASE = DEVLINK_TRAP_GENERIC_ID_MAX,
420 	NSIM_TRAP_ID_FID_MISS,
421 };
422 
423 #define NSIM_TRAP_NAME_FID_MISS "fid_miss"
424 
425 #define NSIM_TRAP_METADATA DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
426 
427 #define NSIM_TRAP_DROP(_id, _group_id)					      \
428 	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
429 			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
430 			     NSIM_TRAP_METADATA)
431 #define NSIM_TRAP_DROP_EXT(_id, _group_id, _metadata)			      \
432 	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
433 			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
434 			     NSIM_TRAP_METADATA | (_metadata))
435 #define NSIM_TRAP_EXCEPTION(_id, _group_id)				      \
436 	DEVLINK_TRAP_GENERIC(EXCEPTION, TRAP, _id,			      \
437 			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
438 			     NSIM_TRAP_METADATA)
439 #define NSIM_TRAP_CONTROL(_id, _group_id, _action)			      \
440 	DEVLINK_TRAP_GENERIC(CONTROL, _action, _id,			      \
441 			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
442 			     NSIM_TRAP_METADATA)
443 #define NSIM_TRAP_DRIVER_EXCEPTION(_id, _group_id)			      \
444 	DEVLINK_TRAP_DRIVER(EXCEPTION, TRAP, NSIM_TRAP_ID_##_id,	      \
445 			    NSIM_TRAP_NAME_##_id,			      \
446 			    DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
447 			    NSIM_TRAP_METADATA)
448 
449 #define NSIM_DEV_TRAP_POLICER_MIN_RATE	1
450 #define NSIM_DEV_TRAP_POLICER_MAX_RATE	8000
451 #define NSIM_DEV_TRAP_POLICER_MIN_BURST	8
452 #define NSIM_DEV_TRAP_POLICER_MAX_BURST	65536
453 
454 #define NSIM_TRAP_POLICER(_id, _rate, _burst)				      \
455 	DEVLINK_TRAP_POLICER(_id, _rate, _burst,			      \
456 			     NSIM_DEV_TRAP_POLICER_MAX_RATE,		      \
457 			     NSIM_DEV_TRAP_POLICER_MIN_RATE,		      \
458 			     NSIM_DEV_TRAP_POLICER_MAX_BURST,		      \
459 			     NSIM_DEV_TRAP_POLICER_MIN_BURST)
460 
461 static const struct devlink_trap_policer nsim_trap_policers_arr[] = {
462 	NSIM_TRAP_POLICER(1, 1000, 128),
463 	NSIM_TRAP_POLICER(2, 2000, 256),
464 	NSIM_TRAP_POLICER(3, 3000, 512),
465 };
466 
467 static const struct devlink_trap_group nsim_trap_groups_arr[] = {
468 	DEVLINK_TRAP_GROUP_GENERIC(L2_DROPS, 0),
469 	DEVLINK_TRAP_GROUP_GENERIC(L3_DROPS, 1),
470 	DEVLINK_TRAP_GROUP_GENERIC(L3_EXCEPTIONS, 1),
471 	DEVLINK_TRAP_GROUP_GENERIC(BUFFER_DROPS, 2),
472 	DEVLINK_TRAP_GROUP_GENERIC(ACL_DROPS, 3),
473 	DEVLINK_TRAP_GROUP_GENERIC(MC_SNOOPING, 3),
474 };
475 
476 static const struct devlink_trap nsim_traps_arr[] = {
477 	NSIM_TRAP_DROP(SMAC_MC, L2_DROPS),
478 	NSIM_TRAP_DROP(VLAN_TAG_MISMATCH, L2_DROPS),
479 	NSIM_TRAP_DROP(INGRESS_VLAN_FILTER, L2_DROPS),
480 	NSIM_TRAP_DROP(INGRESS_STP_FILTER, L2_DROPS),
481 	NSIM_TRAP_DROP(EMPTY_TX_LIST, L2_DROPS),
482 	NSIM_TRAP_DROP(PORT_LOOPBACK_FILTER, L2_DROPS),
483 	NSIM_TRAP_DRIVER_EXCEPTION(FID_MISS, L2_DROPS),
484 	NSIM_TRAP_DROP(BLACKHOLE_ROUTE, L3_DROPS),
485 	NSIM_TRAP_EXCEPTION(TTL_ERROR, L3_EXCEPTIONS),
486 	NSIM_TRAP_DROP(TAIL_DROP, BUFFER_DROPS),
487 	NSIM_TRAP_DROP_EXT(INGRESS_FLOW_ACTION_DROP, ACL_DROPS,
488 			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
489 	NSIM_TRAP_DROP_EXT(EGRESS_FLOW_ACTION_DROP, ACL_DROPS,
490 			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
491 	NSIM_TRAP_CONTROL(IGMP_QUERY, MC_SNOOPING, MIRROR),
492 	NSIM_TRAP_CONTROL(IGMP_V1_REPORT, MC_SNOOPING, TRAP),
493 };
494 
495 #define NSIM_TRAP_L4_DATA_LEN 100
496 
497 static struct sk_buff *nsim_dev_trap_skb_build(void)
498 {
499 	int tot_len, data_len = NSIM_TRAP_L4_DATA_LEN;
500 	struct sk_buff *skb;
501 	struct udphdr *udph;
502 	struct ethhdr *eth;
503 	struct iphdr *iph;
504 
505 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
506 	if (!skb)
507 		return NULL;
508 	tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len;
509 
510 	skb_reset_mac_header(skb);
511 	eth = skb_put(skb, sizeof(struct ethhdr));
512 	eth_random_addr(eth->h_dest);
513 	eth_random_addr(eth->h_source);
514 	eth->h_proto = htons(ETH_P_IP);
515 	skb->protocol = htons(ETH_P_IP);
516 
517 	skb_set_network_header(skb, skb->len);
518 	iph = skb_put(skb, sizeof(struct iphdr));
519 	iph->protocol = IPPROTO_UDP;
520 	iph->saddr = in_aton("192.0.2.1");
521 	iph->daddr = in_aton("198.51.100.1");
522 	iph->version = 0x4;
523 	iph->frag_off = 0;
524 	iph->ihl = 0x5;
525 	iph->tot_len = htons(tot_len);
526 	iph->ttl = 100;
527 	iph->check = 0;
528 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
529 
530 	skb_set_transport_header(skb, skb->len);
531 	udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len);
532 	get_random_bytes(&udph->source, sizeof(u16));
533 	get_random_bytes(&udph->dest, sizeof(u16));
534 	udph->len = htons(sizeof(struct udphdr) + data_len);
535 
536 	return skb;
537 }
538 
539 static void nsim_dev_trap_report(struct nsim_dev_port *nsim_dev_port)
540 {
541 	struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev;
542 	struct devlink *devlink = priv_to_devlink(nsim_dev);
543 	struct nsim_trap_data *nsim_trap_data;
544 	int i;
545 
546 	nsim_trap_data = nsim_dev->trap_data;
547 
548 	spin_lock(&nsim_trap_data->trap_lock);
549 	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
550 		struct flow_action_cookie *fa_cookie = NULL;
551 		struct nsim_trap_item *nsim_trap_item;
552 		struct sk_buff *skb;
553 		bool has_fa_cookie;
554 
555 		has_fa_cookie = nsim_traps_arr[i].metadata_cap &
556 				DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE;
557 
558 		nsim_trap_item = &nsim_trap_data->trap_items_arr[i];
559 		if (nsim_trap_item->action == DEVLINK_TRAP_ACTION_DROP)
560 			continue;
561 
562 		skb = nsim_dev_trap_skb_build();
563 		if (!skb)
564 			continue;
565 		skb->dev = nsim_dev_port->ns->netdev;
566 
567 		/* Trapped packets are usually passed to devlink in softIRQ,
568 		 * but in this case they are generated in a workqueue. Disable
569 		 * softIRQs to prevent lockdep from complaining about
570 		 * "incosistent lock state".
571 		 */
572 
573 		spin_lock_bh(&nsim_dev->fa_cookie_lock);
574 		fa_cookie = has_fa_cookie ? nsim_dev->fa_cookie : NULL;
575 		devlink_trap_report(devlink, skb, nsim_trap_item->trap_ctx,
576 				    &nsim_dev_port->devlink_port, fa_cookie);
577 		spin_unlock_bh(&nsim_dev->fa_cookie_lock);
578 		consume_skb(skb);
579 	}
580 	spin_unlock(&nsim_trap_data->trap_lock);
581 }
582 
583 #define NSIM_TRAP_REPORT_INTERVAL_MS	100
584 
585 static void nsim_dev_trap_report_work(struct work_struct *work)
586 {
587 	struct nsim_trap_data *nsim_trap_data;
588 	struct nsim_dev_port *nsim_dev_port;
589 	struct nsim_dev *nsim_dev;
590 
591 	nsim_trap_data = container_of(work, struct nsim_trap_data,
592 				      trap_report_dw.work);
593 	nsim_dev = nsim_trap_data->nsim_dev;
594 
595 	/* For each running port and enabled packet trap, generate a UDP
596 	 * packet with a random 5-tuple and report it.
597 	 */
598 	mutex_lock(&nsim_dev->port_list_lock);
599 	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list) {
600 		if (!netif_running(nsim_dev_port->ns->netdev))
601 			continue;
602 
603 		nsim_dev_trap_report(nsim_dev_port);
604 	}
605 	mutex_unlock(&nsim_dev->port_list_lock);
606 
607 	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
608 			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
609 }
610 
611 static int nsim_dev_traps_init(struct devlink *devlink)
612 {
613 	size_t policers_count = ARRAY_SIZE(nsim_trap_policers_arr);
614 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
615 	struct nsim_trap_data *nsim_trap_data;
616 	int err;
617 
618 	nsim_trap_data = kzalloc(sizeof(*nsim_trap_data), GFP_KERNEL);
619 	if (!nsim_trap_data)
620 		return -ENOMEM;
621 
622 	nsim_trap_data->trap_items_arr = kcalloc(ARRAY_SIZE(nsim_traps_arr),
623 						 sizeof(struct nsim_trap_item),
624 						 GFP_KERNEL);
625 	if (!nsim_trap_data->trap_items_arr) {
626 		err = -ENOMEM;
627 		goto err_trap_data_free;
628 	}
629 
630 	nsim_trap_data->trap_policers_cnt_arr = kcalloc(policers_count,
631 							sizeof(u64),
632 							GFP_KERNEL);
633 	if (!nsim_trap_data->trap_policers_cnt_arr) {
634 		err = -ENOMEM;
635 		goto err_trap_items_free;
636 	}
637 
638 	/* The lock is used to protect the action state of the registered
639 	 * traps. The value is written by user and read in delayed work when
640 	 * iterating over all the traps.
641 	 */
642 	spin_lock_init(&nsim_trap_data->trap_lock);
643 	nsim_trap_data->nsim_dev = nsim_dev;
644 	nsim_dev->trap_data = nsim_trap_data;
645 
646 	err = devlink_trap_policers_register(devlink, nsim_trap_policers_arr,
647 					     policers_count);
648 	if (err)
649 		goto err_trap_policers_cnt_free;
650 
651 	err = devlink_trap_groups_register(devlink, nsim_trap_groups_arr,
652 					   ARRAY_SIZE(nsim_trap_groups_arr));
653 	if (err)
654 		goto err_trap_policers_unregister;
655 
656 	err = devlink_traps_register(devlink, nsim_traps_arr,
657 				     ARRAY_SIZE(nsim_traps_arr), NULL);
658 	if (err)
659 		goto err_trap_groups_unregister;
660 
661 	INIT_DELAYED_WORK(&nsim_dev->trap_data->trap_report_dw,
662 			  nsim_dev_trap_report_work);
663 	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
664 			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
665 
666 	return 0;
667 
668 err_trap_groups_unregister:
669 	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
670 				       ARRAY_SIZE(nsim_trap_groups_arr));
671 err_trap_policers_unregister:
672 	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
673 					 ARRAY_SIZE(nsim_trap_policers_arr));
674 err_trap_policers_cnt_free:
675 	kfree(nsim_trap_data->trap_policers_cnt_arr);
676 err_trap_items_free:
677 	kfree(nsim_trap_data->trap_items_arr);
678 err_trap_data_free:
679 	kfree(nsim_trap_data);
680 	return err;
681 }
682 
683 static void nsim_dev_traps_exit(struct devlink *devlink)
684 {
685 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
686 
687 	cancel_delayed_work_sync(&nsim_dev->trap_data->trap_report_dw);
688 	devlink_traps_unregister(devlink, nsim_traps_arr,
689 				 ARRAY_SIZE(nsim_traps_arr));
690 	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
691 				       ARRAY_SIZE(nsim_trap_groups_arr));
692 	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
693 					 ARRAY_SIZE(nsim_trap_policers_arr));
694 	kfree(nsim_dev->trap_data->trap_policers_cnt_arr);
695 	kfree(nsim_dev->trap_data->trap_items_arr);
696 	kfree(nsim_dev->trap_data);
697 }
698 
699 static int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
700 				  struct netlink_ext_ack *extack);
701 static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev);
702 
703 static int nsim_dev_reload_down(struct devlink *devlink, bool netns_change,
704 				struct netlink_ext_ack *extack)
705 {
706 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
707 
708 	if (nsim_dev->dont_allow_reload) {
709 		/* For testing purposes, user set debugfs dont_allow_reload
710 		 * value to true. So forbid it.
711 		 */
712 		NL_SET_ERR_MSG_MOD(extack, "User forbid the reload for testing purposes");
713 		return -EOPNOTSUPP;
714 	}
715 
716 	nsim_dev_reload_destroy(nsim_dev);
717 	return 0;
718 }
719 
720 static int nsim_dev_reload_up(struct devlink *devlink,
721 			      struct netlink_ext_ack *extack)
722 {
723 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
724 
725 	if (nsim_dev->fail_reload) {
726 		/* For testing purposes, user set debugfs fail_reload
727 		 * value to true. Fail right away.
728 		 */
729 		NL_SET_ERR_MSG_MOD(extack, "User setup the reload to fail for testing purposes");
730 		return -EINVAL;
731 	}
732 
733 	return nsim_dev_reload_create(nsim_dev, extack);
734 }
735 
736 static int nsim_dev_info_get(struct devlink *devlink,
737 			     struct devlink_info_req *req,
738 			     struct netlink_ext_ack *extack)
739 {
740 	return devlink_info_driver_name_put(req, DRV_NAME);
741 }
742 
743 #define NSIM_DEV_FLASH_SIZE 500000
744 #define NSIM_DEV_FLASH_CHUNK_SIZE 1000
745 #define NSIM_DEV_FLASH_CHUNK_TIME_MS 10
746 
747 static int nsim_dev_flash_update(struct devlink *devlink,
748 				 struct devlink_flash_update_params *params,
749 				 struct netlink_ext_ack *extack)
750 {
751 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
752 	int i;
753 
754 	if ((params->overwrite_mask & ~nsim_dev->fw_update_overwrite_mask) != 0)
755 		return -EOPNOTSUPP;
756 
757 	if (nsim_dev->fw_update_status) {
758 		devlink_flash_update_begin_notify(devlink);
759 		devlink_flash_update_status_notify(devlink,
760 						   "Preparing to flash",
761 						   params->component, 0, 0);
762 	}
763 
764 	for (i = 0; i < NSIM_DEV_FLASH_SIZE / NSIM_DEV_FLASH_CHUNK_SIZE; i++) {
765 		if (nsim_dev->fw_update_status)
766 			devlink_flash_update_status_notify(devlink, "Flashing",
767 							   params->component,
768 							   i * NSIM_DEV_FLASH_CHUNK_SIZE,
769 							   NSIM_DEV_FLASH_SIZE);
770 		msleep(NSIM_DEV_FLASH_CHUNK_TIME_MS);
771 	}
772 
773 	if (nsim_dev->fw_update_status) {
774 		devlink_flash_update_status_notify(devlink, "Flashing",
775 						   params->component,
776 						   NSIM_DEV_FLASH_SIZE,
777 						   NSIM_DEV_FLASH_SIZE);
778 		devlink_flash_update_timeout_notify(devlink, "Flash select",
779 						    params->component, 81);
780 		devlink_flash_update_status_notify(devlink, "Flashing done",
781 						   params->component, 0, 0);
782 		devlink_flash_update_end_notify(devlink);
783 	}
784 
785 	return 0;
786 }
787 
788 static struct nsim_trap_item *
789 nsim_dev_trap_item_lookup(struct nsim_dev *nsim_dev, u16 trap_id)
790 {
791 	struct nsim_trap_data *nsim_trap_data = nsim_dev->trap_data;
792 	int i;
793 
794 	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
795 		if (nsim_traps_arr[i].id == trap_id)
796 			return &nsim_trap_data->trap_items_arr[i];
797 	}
798 
799 	return NULL;
800 }
801 
802 static int nsim_dev_devlink_trap_init(struct devlink *devlink,
803 				      const struct devlink_trap *trap,
804 				      void *trap_ctx)
805 {
806 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
807 	struct nsim_trap_item *nsim_trap_item;
808 
809 	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
810 	if (WARN_ON(!nsim_trap_item))
811 		return -ENOENT;
812 
813 	nsim_trap_item->trap_ctx = trap_ctx;
814 	nsim_trap_item->action = trap->init_action;
815 
816 	return 0;
817 }
818 
819 static int
820 nsim_dev_devlink_trap_action_set(struct devlink *devlink,
821 				 const struct devlink_trap *trap,
822 				 enum devlink_trap_action action,
823 				 struct netlink_ext_ack *extack)
824 {
825 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
826 	struct nsim_trap_item *nsim_trap_item;
827 
828 	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
829 	if (WARN_ON(!nsim_trap_item))
830 		return -ENOENT;
831 
832 	spin_lock(&nsim_dev->trap_data->trap_lock);
833 	nsim_trap_item->action = action;
834 	spin_unlock(&nsim_dev->trap_data->trap_lock);
835 
836 	return 0;
837 }
838 
839 static int
840 nsim_dev_devlink_trap_group_set(struct devlink *devlink,
841 				const struct devlink_trap_group *group,
842 				const struct devlink_trap_policer *policer,
843 				struct netlink_ext_ack *extack)
844 {
845 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
846 
847 	if (nsim_dev->fail_trap_group_set)
848 		return -EINVAL;
849 
850 	return 0;
851 }
852 
853 static int
854 nsim_dev_devlink_trap_policer_set(struct devlink *devlink,
855 				  const struct devlink_trap_policer *policer,
856 				  u64 rate, u64 burst,
857 				  struct netlink_ext_ack *extack)
858 {
859 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
860 
861 	if (nsim_dev->fail_trap_policer_set) {
862 		NL_SET_ERR_MSG_MOD(extack, "User setup the operation to fail for testing purposes");
863 		return -EINVAL;
864 	}
865 
866 	return 0;
867 }
868 
869 static int
870 nsim_dev_devlink_trap_policer_counter_get(struct devlink *devlink,
871 					  const struct devlink_trap_policer *policer,
872 					  u64 *p_drops)
873 {
874 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
875 	u64 *cnt;
876 
877 	if (nsim_dev->fail_trap_policer_counter_get)
878 		return -EINVAL;
879 
880 	cnt = &nsim_dev->trap_data->trap_policers_cnt_arr[policer->id - 1];
881 	*p_drops = (*cnt)++;
882 
883 	return 0;
884 }
885 
886 static const struct devlink_ops nsim_dev_devlink_ops = {
887 	.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT |
888 					 DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
889 	.reload_down = nsim_dev_reload_down,
890 	.reload_up = nsim_dev_reload_up,
891 	.info_get = nsim_dev_info_get,
892 	.flash_update = nsim_dev_flash_update,
893 	.trap_init = nsim_dev_devlink_trap_init,
894 	.trap_action_set = nsim_dev_devlink_trap_action_set,
895 	.trap_group_set = nsim_dev_devlink_trap_group_set,
896 	.trap_policer_set = nsim_dev_devlink_trap_policer_set,
897 	.trap_policer_counter_get = nsim_dev_devlink_trap_policer_counter_get,
898 };
899 
900 #define NSIM_DEV_MAX_MACS_DEFAULT 32
901 #define NSIM_DEV_TEST1_DEFAULT true
902 
903 static int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
904 			       unsigned int port_index)
905 {
906 	struct devlink_port_attrs attrs = {};
907 	struct nsim_dev_port *nsim_dev_port;
908 	struct devlink_port *devlink_port;
909 	int err;
910 
911 	nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL);
912 	if (!nsim_dev_port)
913 		return -ENOMEM;
914 	nsim_dev_port->port_index = port_index;
915 
916 	devlink_port = &nsim_dev_port->devlink_port;
917 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
918 	attrs.phys.port_number = port_index + 1;
919 	memcpy(attrs.switch_id.id, nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
920 	attrs.switch_id.id_len = nsim_dev->switch_id.id_len;
921 	devlink_port_attrs_set(devlink_port, &attrs);
922 	err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port,
923 				    port_index);
924 	if (err)
925 		goto err_port_free;
926 
927 	err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port);
928 	if (err)
929 		goto err_dl_port_unregister;
930 
931 	nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port);
932 	if (IS_ERR(nsim_dev_port->ns)) {
933 		err = PTR_ERR(nsim_dev_port->ns);
934 		goto err_port_debugfs_exit;
935 	}
936 
937 	devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev);
938 	list_add(&nsim_dev_port->list, &nsim_dev->port_list);
939 
940 	return 0;
941 
942 err_port_debugfs_exit:
943 	nsim_dev_port_debugfs_exit(nsim_dev_port);
944 err_dl_port_unregister:
945 	devlink_port_unregister(devlink_port);
946 err_port_free:
947 	kfree(nsim_dev_port);
948 	return err;
949 }
950 
951 static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
952 {
953 	struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
954 
955 	list_del(&nsim_dev_port->list);
956 	devlink_port_type_clear(devlink_port);
957 	nsim_destroy(nsim_dev_port->ns);
958 	nsim_dev_port_debugfs_exit(nsim_dev_port);
959 	devlink_port_unregister(devlink_port);
960 	kfree(nsim_dev_port);
961 }
962 
963 static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
964 {
965 	struct nsim_dev_port *nsim_dev_port, *tmp;
966 
967 	mutex_lock(&nsim_dev->port_list_lock);
968 	list_for_each_entry_safe(nsim_dev_port, tmp,
969 				 &nsim_dev->port_list, list)
970 		__nsim_dev_port_del(nsim_dev_port);
971 	mutex_unlock(&nsim_dev->port_list_lock);
972 }
973 
974 static int nsim_dev_port_add_all(struct nsim_dev *nsim_dev,
975 				 unsigned int port_count)
976 {
977 	int i, err;
978 
979 	for (i = 0; i < port_count; i++) {
980 		err = __nsim_dev_port_add(nsim_dev, i);
981 		if (err)
982 			goto err_port_del_all;
983 	}
984 	return 0;
985 
986 err_port_del_all:
987 	nsim_dev_port_del_all(nsim_dev);
988 	return err;
989 }
990 
991 static int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
992 				  struct netlink_ext_ack *extack)
993 {
994 	struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
995 	struct devlink *devlink;
996 	int err;
997 
998 	devlink = priv_to_devlink(nsim_dev);
999 	nsim_dev = devlink_priv(devlink);
1000 	INIT_LIST_HEAD(&nsim_dev->port_list);
1001 	mutex_init(&nsim_dev->port_list_lock);
1002 	nsim_dev->fw_update_status = true;
1003 	nsim_dev->fw_update_overwrite_mask = 0;
1004 
1005 	nsim_dev->fib_data = nsim_fib_create(devlink, extack);
1006 	if (IS_ERR(nsim_dev->fib_data))
1007 		return PTR_ERR(nsim_dev->fib_data);
1008 
1009 	nsim_devlink_param_load_driverinit_values(devlink);
1010 
1011 	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
1012 	if (err)
1013 		goto err_fib_destroy;
1014 
1015 	err = nsim_dev_traps_init(devlink);
1016 	if (err)
1017 		goto err_dummy_region_exit;
1018 
1019 	err = nsim_dev_health_init(nsim_dev, devlink);
1020 	if (err)
1021 		goto err_traps_exit;
1022 
1023 	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
1024 	if (err)
1025 		goto err_health_exit;
1026 
1027 	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
1028 						      0200,
1029 						      nsim_dev->ddir,
1030 						      nsim_dev,
1031 						&nsim_dev_take_snapshot_fops);
1032 	return 0;
1033 
1034 err_health_exit:
1035 	nsim_dev_health_exit(nsim_dev);
1036 err_traps_exit:
1037 	nsim_dev_traps_exit(devlink);
1038 err_dummy_region_exit:
1039 	nsim_dev_dummy_region_exit(nsim_dev);
1040 err_fib_destroy:
1041 	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1042 	return err;
1043 }
1044 
1045 int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
1046 {
1047 	struct nsim_dev *nsim_dev;
1048 	struct devlink *devlink;
1049 	int err;
1050 
1051 	devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev));
1052 	if (!devlink)
1053 		return -ENOMEM;
1054 	devlink_net_set(devlink, nsim_bus_dev->initial_net);
1055 	nsim_dev = devlink_priv(devlink);
1056 	nsim_dev->nsim_bus_dev = nsim_bus_dev;
1057 	nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
1058 	get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
1059 	INIT_LIST_HEAD(&nsim_dev->port_list);
1060 	mutex_init(&nsim_dev->port_list_lock);
1061 	nsim_dev->fw_update_status = true;
1062 	nsim_dev->fw_update_overwrite_mask = 0;
1063 	nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT;
1064 	nsim_dev->test1 = NSIM_DEV_TEST1_DEFAULT;
1065 	spin_lock_init(&nsim_dev->fa_cookie_lock);
1066 
1067 	dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
1068 
1069 	err = nsim_dev_resources_register(devlink);
1070 	if (err)
1071 		goto err_devlink_free;
1072 
1073 	nsim_dev->fib_data = nsim_fib_create(devlink, NULL);
1074 	if (IS_ERR(nsim_dev->fib_data)) {
1075 		err = PTR_ERR(nsim_dev->fib_data);
1076 		goto err_resources_unregister;
1077 	}
1078 
1079 	err = devlink_register(devlink, &nsim_bus_dev->dev);
1080 	if (err)
1081 		goto err_fib_destroy;
1082 
1083 	err = devlink_params_register(devlink, nsim_devlink_params,
1084 				      ARRAY_SIZE(nsim_devlink_params));
1085 	if (err)
1086 		goto err_dl_unregister;
1087 	nsim_devlink_set_params_init_values(nsim_dev, devlink);
1088 
1089 	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
1090 	if (err)
1091 		goto err_params_unregister;
1092 
1093 	err = nsim_dev_traps_init(devlink);
1094 	if (err)
1095 		goto err_dummy_region_exit;
1096 
1097 	err = nsim_dev_debugfs_init(nsim_dev);
1098 	if (err)
1099 		goto err_traps_exit;
1100 
1101 	err = nsim_dev_health_init(nsim_dev, devlink);
1102 	if (err)
1103 		goto err_debugfs_exit;
1104 
1105 	err = nsim_bpf_dev_init(nsim_dev);
1106 	if (err)
1107 		goto err_health_exit;
1108 
1109 	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
1110 	if (err)
1111 		goto err_bpf_dev_exit;
1112 
1113 	devlink_params_publish(devlink);
1114 	devlink_reload_enable(devlink);
1115 	return 0;
1116 
1117 err_bpf_dev_exit:
1118 	nsim_bpf_dev_exit(nsim_dev);
1119 err_health_exit:
1120 	nsim_dev_health_exit(nsim_dev);
1121 err_debugfs_exit:
1122 	nsim_dev_debugfs_exit(nsim_dev);
1123 err_traps_exit:
1124 	nsim_dev_traps_exit(devlink);
1125 err_dummy_region_exit:
1126 	nsim_dev_dummy_region_exit(nsim_dev);
1127 err_params_unregister:
1128 	devlink_params_unregister(devlink, nsim_devlink_params,
1129 				  ARRAY_SIZE(nsim_devlink_params));
1130 err_dl_unregister:
1131 	devlink_unregister(devlink);
1132 err_fib_destroy:
1133 	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1134 err_resources_unregister:
1135 	devlink_resources_unregister(devlink, NULL);
1136 err_devlink_free:
1137 	devlink_free(devlink);
1138 	return err;
1139 }
1140 
1141 static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev)
1142 {
1143 	struct devlink *devlink = priv_to_devlink(nsim_dev);
1144 
1145 	if (devlink_is_reload_failed(devlink))
1146 		return;
1147 	debugfs_remove(nsim_dev->take_snapshot);
1148 	nsim_dev_port_del_all(nsim_dev);
1149 	nsim_dev_health_exit(nsim_dev);
1150 	nsim_dev_traps_exit(devlink);
1151 	nsim_dev_dummy_region_exit(nsim_dev);
1152 	mutex_destroy(&nsim_dev->port_list_lock);
1153 	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1154 }
1155 
1156 void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
1157 {
1158 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1159 	struct devlink *devlink = priv_to_devlink(nsim_dev);
1160 
1161 	devlink_reload_disable(devlink);
1162 
1163 	nsim_dev_reload_destroy(nsim_dev);
1164 
1165 	nsim_bpf_dev_exit(nsim_dev);
1166 	nsim_dev_debugfs_exit(nsim_dev);
1167 	devlink_params_unregister(devlink, nsim_devlink_params,
1168 				  ARRAY_SIZE(nsim_devlink_params));
1169 	devlink_unregister(devlink);
1170 	devlink_resources_unregister(devlink, NULL);
1171 	devlink_free(devlink);
1172 }
1173 
1174 static struct nsim_dev_port *
1175 __nsim_dev_port_lookup(struct nsim_dev *nsim_dev, unsigned int port_index)
1176 {
1177 	struct nsim_dev_port *nsim_dev_port;
1178 
1179 	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
1180 		if (nsim_dev_port->port_index == port_index)
1181 			return nsim_dev_port;
1182 	return NULL;
1183 }
1184 
1185 int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
1186 		      unsigned int port_index)
1187 {
1188 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1189 	int err;
1190 
1191 	mutex_lock(&nsim_dev->port_list_lock);
1192 	if (__nsim_dev_port_lookup(nsim_dev, port_index))
1193 		err = -EEXIST;
1194 	else
1195 		err = __nsim_dev_port_add(nsim_dev, port_index);
1196 	mutex_unlock(&nsim_dev->port_list_lock);
1197 	return err;
1198 }
1199 
1200 int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
1201 		      unsigned int port_index)
1202 {
1203 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1204 	struct nsim_dev_port *nsim_dev_port;
1205 	int err = 0;
1206 
1207 	mutex_lock(&nsim_dev->port_list_lock);
1208 	nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, port_index);
1209 	if (!nsim_dev_port)
1210 		err = -ENOENT;
1211 	else
1212 		__nsim_dev_port_del(nsim_dev_port);
1213 	mutex_unlock(&nsim_dev->port_list_lock);
1214 	return err;
1215 }
1216 
1217 int nsim_dev_init(void)
1218 {
1219 	nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
1220 	return PTR_ERR_OR_ZERO(nsim_dev_ddir);
1221 }
1222 
1223 void nsim_dev_exit(void)
1224 {
1225 	debugfs_remove_recursive(nsim_dev_ddir);
1226 }
1227