xref: /openbmc/linux/drivers/net/netdevsim/dev.c (revision 7a2eb736)
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 <uapi/linux/devlink.h>
32 #include <uapi/linux/ip.h>
33 #include <uapi/linux/udp.h>
34 
35 #include "netdevsim.h"
36 
37 static struct dentry *nsim_dev_ddir;
38 
39 #define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)
40 
41 static ssize_t nsim_dev_take_snapshot_write(struct file *file,
42 					    const char __user *data,
43 					    size_t count, loff_t *ppos)
44 {
45 	struct nsim_dev *nsim_dev = file->private_data;
46 	void *dummy_data;
47 	int err;
48 	u32 id;
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 	id = devlink_region_shapshot_id_get(priv_to_devlink(nsim_dev));
57 	err = devlink_region_snapshot_create(nsim_dev->dummy_region,
58 					     dummy_data, id, kfree);
59 	if (err) {
60 		pr_err("Failed to create region snapshot\n");
61 		kfree(dummy_data);
62 		return err;
63 	}
64 
65 	return count;
66 }
67 
68 static const struct file_operations nsim_dev_take_snapshot_fops = {
69 	.open = simple_open,
70 	.write = nsim_dev_take_snapshot_write,
71 	.llseek = generic_file_llseek,
72 };
73 
74 static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
75 {
76 	char dev_ddir_name[16];
77 
78 	sprintf(dev_ddir_name, DRV_NAME "%u", nsim_dev->nsim_bus_dev->dev.id);
79 	nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir);
80 	if (IS_ERR_OR_NULL(nsim_dev->ddir))
81 		return PTR_ERR_OR_ZERO(nsim_dev->ddir) ?: -EINVAL;
82 	nsim_dev->ports_ddir = debugfs_create_dir("ports", nsim_dev->ddir);
83 	if (IS_ERR_OR_NULL(nsim_dev->ports_ddir))
84 		return PTR_ERR_OR_ZERO(nsim_dev->ports_ddir) ?: -EINVAL;
85 	debugfs_create_bool("fw_update_status", 0600, nsim_dev->ddir,
86 			    &nsim_dev->fw_update_status);
87 	debugfs_create_u32("max_macs", 0600, nsim_dev->ddir,
88 			   &nsim_dev->max_macs);
89 	debugfs_create_bool("test1", 0600, nsim_dev->ddir,
90 			    &nsim_dev->test1);
91 	debugfs_create_file("take_snapshot", 0200, nsim_dev->ddir, nsim_dev,
92 			    &nsim_dev_take_snapshot_fops);
93 	return 0;
94 }
95 
96 static void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev)
97 {
98 	debugfs_remove_recursive(nsim_dev->ports_ddir);
99 	debugfs_remove_recursive(nsim_dev->ddir);
100 }
101 
102 static int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev,
103 				      struct nsim_dev_port *nsim_dev_port)
104 {
105 	char port_ddir_name[16];
106 	char dev_link_name[32];
107 
108 	sprintf(port_ddir_name, "%u", nsim_dev_port->port_index);
109 	nsim_dev_port->ddir = debugfs_create_dir(port_ddir_name,
110 						 nsim_dev->ports_ddir);
111 	if (IS_ERR_OR_NULL(nsim_dev_port->ddir))
112 		return -ENOMEM;
113 
114 	sprintf(dev_link_name, "../../../" DRV_NAME "%u",
115 		nsim_dev->nsim_bus_dev->dev.id);
116 	debugfs_create_symlink("dev", nsim_dev_port->ddir, dev_link_name);
117 
118 	return 0;
119 }
120 
121 static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port)
122 {
123 	debugfs_remove_recursive(nsim_dev_port->ddir);
124 }
125 
126 static struct net *nsim_devlink_net(struct devlink *devlink)
127 {
128 	return &init_net;
129 }
130 
131 static u64 nsim_dev_ipv4_fib_resource_occ_get(void *priv)
132 {
133 	struct net *net = priv;
134 
135 	return nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB, false);
136 }
137 
138 static u64 nsim_dev_ipv4_fib_rules_res_occ_get(void *priv)
139 {
140 	struct net *net = priv;
141 
142 	return nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB_RULES, false);
143 }
144 
145 static u64 nsim_dev_ipv6_fib_resource_occ_get(void *priv)
146 {
147 	struct net *net = priv;
148 
149 	return nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB, false);
150 }
151 
152 static u64 nsim_dev_ipv6_fib_rules_res_occ_get(void *priv)
153 {
154 	struct net *net = priv;
155 
156 	return nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB_RULES, false);
157 }
158 
159 static int nsim_dev_resources_register(struct devlink *devlink)
160 {
161 	struct devlink_resource_size_params params = {
162 		.size_max = (u64)-1,
163 		.size_granularity = 1,
164 		.unit = DEVLINK_RESOURCE_UNIT_ENTRY
165 	};
166 	struct net *net = nsim_devlink_net(devlink);
167 	int err;
168 	u64 n;
169 
170 	/* Resources for IPv4 */
171 	err = devlink_resource_register(devlink, "IPv4", (u64)-1,
172 					NSIM_RESOURCE_IPV4,
173 					DEVLINK_RESOURCE_ID_PARENT_TOP,
174 					&params);
175 	if (err) {
176 		pr_err("Failed to register IPv4 top resource\n");
177 		goto out;
178 	}
179 
180 	n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB, true);
181 	err = devlink_resource_register(devlink, "fib", n,
182 					NSIM_RESOURCE_IPV4_FIB,
183 					NSIM_RESOURCE_IPV4, &params);
184 	if (err) {
185 		pr_err("Failed to register IPv4 FIB resource\n");
186 		return err;
187 	}
188 
189 	n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB_RULES, true);
190 	err = devlink_resource_register(devlink, "fib-rules", n,
191 					NSIM_RESOURCE_IPV4_FIB_RULES,
192 					NSIM_RESOURCE_IPV4, &params);
193 	if (err) {
194 		pr_err("Failed to register IPv4 FIB rules resource\n");
195 		return err;
196 	}
197 
198 	/* Resources for IPv6 */
199 	err = devlink_resource_register(devlink, "IPv6", (u64)-1,
200 					NSIM_RESOURCE_IPV6,
201 					DEVLINK_RESOURCE_ID_PARENT_TOP,
202 					&params);
203 	if (err) {
204 		pr_err("Failed to register IPv6 top resource\n");
205 		goto out;
206 	}
207 
208 	n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB, true);
209 	err = devlink_resource_register(devlink, "fib", n,
210 					NSIM_RESOURCE_IPV6_FIB,
211 					NSIM_RESOURCE_IPV6, &params);
212 	if (err) {
213 		pr_err("Failed to register IPv6 FIB resource\n");
214 		return err;
215 	}
216 
217 	n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB_RULES, true);
218 	err = devlink_resource_register(devlink, "fib-rules", n,
219 					NSIM_RESOURCE_IPV6_FIB_RULES,
220 					NSIM_RESOURCE_IPV6, &params);
221 	if (err) {
222 		pr_err("Failed to register IPv6 FIB rules resource\n");
223 		return err;
224 	}
225 
226 	devlink_resource_occ_get_register(devlink,
227 					  NSIM_RESOURCE_IPV4_FIB,
228 					  nsim_dev_ipv4_fib_resource_occ_get,
229 					  net);
230 	devlink_resource_occ_get_register(devlink,
231 					  NSIM_RESOURCE_IPV4_FIB_RULES,
232 					  nsim_dev_ipv4_fib_rules_res_occ_get,
233 					  net);
234 	devlink_resource_occ_get_register(devlink,
235 					  NSIM_RESOURCE_IPV6_FIB,
236 					  nsim_dev_ipv6_fib_resource_occ_get,
237 					  net);
238 	devlink_resource_occ_get_register(devlink,
239 					  NSIM_RESOURCE_IPV6_FIB_RULES,
240 					  nsim_dev_ipv6_fib_rules_res_occ_get,
241 					  net);
242 out:
243 	return err;
244 }
245 
246 enum nsim_devlink_param_id {
247 	NSIM_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
248 	NSIM_DEVLINK_PARAM_ID_TEST1,
249 };
250 
251 static const struct devlink_param nsim_devlink_params[] = {
252 	DEVLINK_PARAM_GENERIC(MAX_MACS,
253 			      BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
254 			      NULL, NULL, NULL),
255 	DEVLINK_PARAM_DRIVER(NSIM_DEVLINK_PARAM_ID_TEST1,
256 			     "test1", DEVLINK_PARAM_TYPE_BOOL,
257 			     BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
258 			     NULL, NULL, NULL),
259 };
260 
261 static void nsim_devlink_set_params_init_values(struct nsim_dev *nsim_dev,
262 						struct devlink *devlink)
263 {
264 	union devlink_param_value value;
265 
266 	value.vu32 = nsim_dev->max_macs;
267 	devlink_param_driverinit_value_set(devlink,
268 					   DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
269 					   value);
270 	value.vbool = nsim_dev->test1;
271 	devlink_param_driverinit_value_set(devlink,
272 					   NSIM_DEVLINK_PARAM_ID_TEST1,
273 					   value);
274 }
275 
276 static void nsim_devlink_param_load_driverinit_values(struct devlink *devlink)
277 {
278 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
279 	union devlink_param_value saved_value;
280 	int err;
281 
282 	err = devlink_param_driverinit_value_get(devlink,
283 						 DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
284 						 &saved_value);
285 	if (!err)
286 		nsim_dev->max_macs = saved_value.vu32;
287 	err = devlink_param_driverinit_value_get(devlink,
288 						 NSIM_DEVLINK_PARAM_ID_TEST1,
289 						 &saved_value);
290 	if (!err)
291 		nsim_dev->test1 = saved_value.vbool;
292 }
293 
294 #define NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX 16
295 
296 static int nsim_dev_dummy_region_init(struct nsim_dev *nsim_dev,
297 				      struct devlink *devlink)
298 {
299 	nsim_dev->dummy_region =
300 		devlink_region_create(devlink, "dummy",
301 				      NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX,
302 				      NSIM_DEV_DUMMY_REGION_SIZE);
303 	return PTR_ERR_OR_ZERO(nsim_dev->dummy_region);
304 }
305 
306 static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
307 {
308 	devlink_region_destroy(nsim_dev->dummy_region);
309 }
310 
311 struct nsim_trap_item {
312 	void *trap_ctx;
313 	enum devlink_trap_action action;
314 };
315 
316 struct nsim_trap_data {
317 	struct delayed_work trap_report_dw;
318 	struct nsim_trap_item *trap_items_arr;
319 	struct nsim_dev *nsim_dev;
320 	spinlock_t trap_lock;	/* Protects trap_items_arr */
321 };
322 
323 /* All driver-specific traps must be documented in
324  * Documentation/networking/devlink-trap-netdevsim.rst
325  */
326 enum {
327 	NSIM_TRAP_ID_BASE = DEVLINK_TRAP_GENERIC_ID_MAX,
328 	NSIM_TRAP_ID_FID_MISS,
329 };
330 
331 #define NSIM_TRAP_NAME_FID_MISS "fid_miss"
332 
333 #define NSIM_TRAP_METADATA DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
334 
335 #define NSIM_TRAP_DROP(_id, _group_id)					      \
336 	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
337 			     DEVLINK_TRAP_GROUP_GENERIC(_group_id),	      \
338 			     NSIM_TRAP_METADATA)
339 #define NSIM_TRAP_EXCEPTION(_id, _group_id)				      \
340 	DEVLINK_TRAP_GENERIC(EXCEPTION, TRAP, _id,			      \
341 			     DEVLINK_TRAP_GROUP_GENERIC(_group_id),	      \
342 			     NSIM_TRAP_METADATA)
343 #define NSIM_TRAP_DRIVER_EXCEPTION(_id, _group_id)			      \
344 	DEVLINK_TRAP_DRIVER(EXCEPTION, TRAP, NSIM_TRAP_ID_##_id,	      \
345 			    NSIM_TRAP_NAME_##_id,			      \
346 			    DEVLINK_TRAP_GROUP_GENERIC(_group_id),	      \
347 			    NSIM_TRAP_METADATA)
348 
349 static const struct devlink_trap nsim_traps_arr[] = {
350 	NSIM_TRAP_DROP(SMAC_MC, L2_DROPS),
351 	NSIM_TRAP_DROP(VLAN_TAG_MISMATCH, L2_DROPS),
352 	NSIM_TRAP_DROP(INGRESS_VLAN_FILTER, L2_DROPS),
353 	NSIM_TRAP_DROP(INGRESS_STP_FILTER, L2_DROPS),
354 	NSIM_TRAP_DROP(EMPTY_TX_LIST, L2_DROPS),
355 	NSIM_TRAP_DROP(PORT_LOOPBACK_FILTER, L2_DROPS),
356 	NSIM_TRAP_DRIVER_EXCEPTION(FID_MISS, L2_DROPS),
357 	NSIM_TRAP_DROP(BLACKHOLE_ROUTE, L3_DROPS),
358 	NSIM_TRAP_EXCEPTION(TTL_ERROR, L3_DROPS),
359 	NSIM_TRAP_DROP(TAIL_DROP, BUFFER_DROPS),
360 };
361 
362 #define NSIM_TRAP_L4_DATA_LEN 100
363 
364 static struct sk_buff *nsim_dev_trap_skb_build(void)
365 {
366 	int tot_len, data_len = NSIM_TRAP_L4_DATA_LEN;
367 	struct sk_buff *skb;
368 	struct udphdr *udph;
369 	struct ethhdr *eth;
370 	struct iphdr *iph;
371 
372 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
373 	if (!skb)
374 		return NULL;
375 	tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len;
376 
377 	eth = skb_put(skb, sizeof(struct ethhdr));
378 	eth_random_addr(eth->h_dest);
379 	eth_random_addr(eth->h_source);
380 	eth->h_proto = htons(ETH_P_IP);
381 	skb->protocol = htons(ETH_P_IP);
382 
383 	iph = skb_put(skb, sizeof(struct iphdr));
384 	iph->protocol = IPPROTO_UDP;
385 	iph->saddr = in_aton("192.0.2.1");
386 	iph->daddr = in_aton("198.51.100.1");
387 	iph->version = 0x4;
388 	iph->frag_off = 0;
389 	iph->ihl = 0x5;
390 	iph->tot_len = htons(tot_len);
391 	iph->ttl = 100;
392 	iph->check = 0;
393 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
394 
395 	udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len);
396 	get_random_bytes(&udph->source, sizeof(u16));
397 	get_random_bytes(&udph->dest, sizeof(u16));
398 	udph->len = htons(sizeof(struct udphdr) + data_len);
399 
400 	return skb;
401 }
402 
403 static void nsim_dev_trap_report(struct nsim_dev_port *nsim_dev_port)
404 {
405 	struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev;
406 	struct devlink *devlink = priv_to_devlink(nsim_dev);
407 	struct nsim_trap_data *nsim_trap_data;
408 	int i;
409 
410 	nsim_trap_data = nsim_dev->trap_data;
411 
412 	spin_lock(&nsim_trap_data->trap_lock);
413 	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
414 		struct nsim_trap_item *nsim_trap_item;
415 		struct sk_buff *skb;
416 
417 		nsim_trap_item = &nsim_trap_data->trap_items_arr[i];
418 		if (nsim_trap_item->action == DEVLINK_TRAP_ACTION_DROP)
419 			continue;
420 
421 		skb = nsim_dev_trap_skb_build();
422 		if (!skb)
423 			continue;
424 		skb->dev = nsim_dev_port->ns->netdev;
425 
426 		/* Trapped packets are usually passed to devlink in softIRQ,
427 		 * but in this case they are generated in a workqueue. Disable
428 		 * softIRQs to prevent lockdep from complaining about
429 		 * "incosistent lock state".
430 		 */
431 		local_bh_disable();
432 		devlink_trap_report(devlink, skb, nsim_trap_item->trap_ctx,
433 				    &nsim_dev_port->devlink_port);
434 		local_bh_enable();
435 		consume_skb(skb);
436 	}
437 	spin_unlock(&nsim_trap_data->trap_lock);
438 }
439 
440 #define NSIM_TRAP_REPORT_INTERVAL_MS	100
441 
442 static void nsim_dev_trap_report_work(struct work_struct *work)
443 {
444 	struct nsim_trap_data *nsim_trap_data;
445 	struct nsim_dev_port *nsim_dev_port;
446 	struct nsim_dev *nsim_dev;
447 
448 	nsim_trap_data = container_of(work, struct nsim_trap_data,
449 				      trap_report_dw.work);
450 	nsim_dev = nsim_trap_data->nsim_dev;
451 
452 	/* For each running port and enabled packet trap, generate a UDP
453 	 * packet with a random 5-tuple and report it.
454 	 */
455 	mutex_lock(&nsim_dev->port_list_lock);
456 	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list) {
457 		if (!netif_running(nsim_dev_port->ns->netdev))
458 			continue;
459 
460 		nsim_dev_trap_report(nsim_dev_port);
461 	}
462 	mutex_unlock(&nsim_dev->port_list_lock);
463 
464 	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
465 			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
466 }
467 
468 static int nsim_dev_traps_init(struct devlink *devlink)
469 {
470 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
471 	struct nsim_trap_data *nsim_trap_data;
472 	int err;
473 
474 	nsim_trap_data = kzalloc(sizeof(*nsim_trap_data), GFP_KERNEL);
475 	if (!nsim_trap_data)
476 		return -ENOMEM;
477 
478 	nsim_trap_data->trap_items_arr = kcalloc(ARRAY_SIZE(nsim_traps_arr),
479 						 sizeof(struct nsim_trap_item),
480 						 GFP_KERNEL);
481 	if (!nsim_trap_data->trap_items_arr) {
482 		err = -ENOMEM;
483 		goto err_trap_data_free;
484 	}
485 
486 	/* The lock is used to protect the action state of the registered
487 	 * traps. The value is written by user and read in delayed work when
488 	 * iterating over all the traps.
489 	 */
490 	spin_lock_init(&nsim_trap_data->trap_lock);
491 	nsim_trap_data->nsim_dev = nsim_dev;
492 	nsim_dev->trap_data = nsim_trap_data;
493 
494 	err = devlink_traps_register(devlink, nsim_traps_arr,
495 				     ARRAY_SIZE(nsim_traps_arr), NULL);
496 	if (err)
497 		goto err_trap_items_free;
498 
499 	INIT_DELAYED_WORK(&nsim_dev->trap_data->trap_report_dw,
500 			  nsim_dev_trap_report_work);
501 	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
502 			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
503 
504 	return 0;
505 
506 err_trap_items_free:
507 	kfree(nsim_trap_data->trap_items_arr);
508 err_trap_data_free:
509 	kfree(nsim_trap_data);
510 	return err;
511 }
512 
513 static void nsim_dev_traps_exit(struct devlink *devlink)
514 {
515 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
516 
517 	cancel_delayed_work_sync(&nsim_dev->trap_data->trap_report_dw);
518 	devlink_traps_unregister(devlink, nsim_traps_arr,
519 				 ARRAY_SIZE(nsim_traps_arr));
520 	kfree(nsim_dev->trap_data->trap_items_arr);
521 	kfree(nsim_dev->trap_data);
522 }
523 
524 static int nsim_dev_reload(struct devlink *devlink,
525 			   struct netlink_ext_ack *extack)
526 {
527 	enum nsim_resource_id res_ids[] = {
528 		NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES,
529 		NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES
530 	};
531 	struct net *net = nsim_devlink_net(devlink);
532 	int i;
533 
534 	for (i = 0; i < ARRAY_SIZE(res_ids); ++i) {
535 		int err;
536 		u64 val;
537 
538 		err = devlink_resource_size_get(devlink, res_ids[i], &val);
539 		if (!err) {
540 			err = nsim_fib_set_max(net, res_ids[i], val, extack);
541 			if (err)
542 				return err;
543 		}
544 	}
545 	nsim_devlink_param_load_driverinit_values(devlink);
546 
547 	return 0;
548 }
549 
550 #define NSIM_DEV_FLASH_SIZE 500000
551 #define NSIM_DEV_FLASH_CHUNK_SIZE 1000
552 #define NSIM_DEV_FLASH_CHUNK_TIME_MS 10
553 
554 static int nsim_dev_flash_update(struct devlink *devlink, const char *file_name,
555 				 const char *component,
556 				 struct netlink_ext_ack *extack)
557 {
558 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
559 	int i;
560 
561 	if (nsim_dev->fw_update_status) {
562 		devlink_flash_update_begin_notify(devlink);
563 		devlink_flash_update_status_notify(devlink,
564 						   "Preparing to flash",
565 						   component, 0, 0);
566 	}
567 
568 	for (i = 0; i < NSIM_DEV_FLASH_SIZE / NSIM_DEV_FLASH_CHUNK_SIZE; i++) {
569 		if (nsim_dev->fw_update_status)
570 			devlink_flash_update_status_notify(devlink, "Flashing",
571 							   component,
572 							   i * NSIM_DEV_FLASH_CHUNK_SIZE,
573 							   NSIM_DEV_FLASH_SIZE);
574 		msleep(NSIM_DEV_FLASH_CHUNK_TIME_MS);
575 	}
576 
577 	if (nsim_dev->fw_update_status) {
578 		devlink_flash_update_status_notify(devlink, "Flashing",
579 						   component,
580 						   NSIM_DEV_FLASH_SIZE,
581 						   NSIM_DEV_FLASH_SIZE);
582 		devlink_flash_update_status_notify(devlink, "Flashing done",
583 						   component, 0, 0);
584 		devlink_flash_update_end_notify(devlink);
585 	}
586 
587 	return 0;
588 }
589 
590 static struct nsim_trap_item *
591 nsim_dev_trap_item_lookup(struct nsim_dev *nsim_dev, u16 trap_id)
592 {
593 	struct nsim_trap_data *nsim_trap_data = nsim_dev->trap_data;
594 	int i;
595 
596 	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
597 		if (nsim_traps_arr[i].id == trap_id)
598 			return &nsim_trap_data->trap_items_arr[i];
599 	}
600 
601 	return NULL;
602 }
603 
604 static int nsim_dev_devlink_trap_init(struct devlink *devlink,
605 				      const struct devlink_trap *trap,
606 				      void *trap_ctx)
607 {
608 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
609 	struct nsim_trap_item *nsim_trap_item;
610 
611 	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
612 	if (WARN_ON(!nsim_trap_item))
613 		return -ENOENT;
614 
615 	nsim_trap_item->trap_ctx = trap_ctx;
616 	nsim_trap_item->action = trap->init_action;
617 
618 	return 0;
619 }
620 
621 static int
622 nsim_dev_devlink_trap_action_set(struct devlink *devlink,
623 				 const struct devlink_trap *trap,
624 				 enum devlink_trap_action action)
625 {
626 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
627 	struct nsim_trap_item *nsim_trap_item;
628 
629 	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
630 	if (WARN_ON(!nsim_trap_item))
631 		return -ENOENT;
632 
633 	spin_lock(&nsim_dev->trap_data->trap_lock);
634 	nsim_trap_item->action = action;
635 	spin_unlock(&nsim_dev->trap_data->trap_lock);
636 
637 	return 0;
638 }
639 
640 static const struct devlink_ops nsim_dev_devlink_ops = {
641 	.reload = nsim_dev_reload,
642 	.flash_update = nsim_dev_flash_update,
643 	.trap_init = nsim_dev_devlink_trap_init,
644 	.trap_action_set = nsim_dev_devlink_trap_action_set,
645 };
646 
647 #define NSIM_DEV_MAX_MACS_DEFAULT 32
648 #define NSIM_DEV_TEST1_DEFAULT true
649 
650 static struct nsim_dev *
651 nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
652 {
653 	struct nsim_dev *nsim_dev;
654 	struct devlink *devlink;
655 	int err;
656 
657 	devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev));
658 	if (!devlink)
659 		return ERR_PTR(-ENOMEM);
660 	nsim_dev = devlink_priv(devlink);
661 	nsim_dev->nsim_bus_dev = nsim_bus_dev;
662 	nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
663 	get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
664 	INIT_LIST_HEAD(&nsim_dev->port_list);
665 	mutex_init(&nsim_dev->port_list_lock);
666 	nsim_dev->fw_update_status = true;
667 	nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT;
668 	nsim_dev->test1 = NSIM_DEV_TEST1_DEFAULT;
669 
670 	err = nsim_dev_resources_register(devlink);
671 	if (err)
672 		goto err_devlink_free;
673 
674 	err = devlink_register(devlink, &nsim_bus_dev->dev);
675 	if (err)
676 		goto err_resources_unregister;
677 
678 	err = devlink_params_register(devlink, nsim_devlink_params,
679 				      ARRAY_SIZE(nsim_devlink_params));
680 	if (err)
681 		goto err_dl_unregister;
682 	nsim_devlink_set_params_init_values(nsim_dev, devlink);
683 
684 	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
685 	if (err)
686 		goto err_params_unregister;
687 
688 	err = nsim_dev_traps_init(devlink);
689 	if (err)
690 		goto err_dummy_region_exit;
691 
692 	err = nsim_dev_debugfs_init(nsim_dev);
693 	if (err)
694 		goto err_traps_exit;
695 
696 	err = nsim_bpf_dev_init(nsim_dev);
697 	if (err)
698 		goto err_debugfs_exit;
699 
700 	devlink_params_publish(devlink);
701 	return nsim_dev;
702 
703 err_debugfs_exit:
704 	nsim_dev_debugfs_exit(nsim_dev);
705 err_traps_exit:
706 	nsim_dev_traps_exit(devlink);
707 err_dummy_region_exit:
708 	nsim_dev_dummy_region_exit(nsim_dev);
709 err_params_unregister:
710 	devlink_params_unregister(devlink, nsim_devlink_params,
711 				  ARRAY_SIZE(nsim_devlink_params));
712 err_dl_unregister:
713 	devlink_unregister(devlink);
714 err_resources_unregister:
715 	devlink_resources_unregister(devlink, NULL);
716 err_devlink_free:
717 	devlink_free(devlink);
718 	return ERR_PTR(err);
719 }
720 
721 static void nsim_dev_destroy(struct nsim_dev *nsim_dev)
722 {
723 	struct devlink *devlink = priv_to_devlink(nsim_dev);
724 
725 	nsim_bpf_dev_exit(nsim_dev);
726 	nsim_dev_debugfs_exit(nsim_dev);
727 	nsim_dev_traps_exit(devlink);
728 	nsim_dev_dummy_region_exit(nsim_dev);
729 	devlink_params_unregister(devlink, nsim_devlink_params,
730 				  ARRAY_SIZE(nsim_devlink_params));
731 	devlink_unregister(devlink);
732 	devlink_resources_unregister(devlink, NULL);
733 	mutex_destroy(&nsim_dev->port_list_lock);
734 	devlink_free(devlink);
735 }
736 
737 static int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
738 			       unsigned int port_index)
739 {
740 	struct nsim_dev_port *nsim_dev_port;
741 	struct devlink_port *devlink_port;
742 	int err;
743 
744 	nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL);
745 	if (!nsim_dev_port)
746 		return -ENOMEM;
747 	nsim_dev_port->port_index = port_index;
748 
749 	devlink_port = &nsim_dev_port->devlink_port;
750 	devlink_port_attrs_set(devlink_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
751 			       port_index + 1, 0, 0,
752 			       nsim_dev->switch_id.id,
753 			       nsim_dev->switch_id.id_len);
754 	err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port,
755 				    port_index);
756 	if (err)
757 		goto err_port_free;
758 
759 	err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port);
760 	if (err)
761 		goto err_dl_port_unregister;
762 
763 	nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port);
764 	if (IS_ERR(nsim_dev_port->ns)) {
765 		err = PTR_ERR(nsim_dev_port->ns);
766 		goto err_port_debugfs_exit;
767 	}
768 
769 	devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev);
770 	list_add(&nsim_dev_port->list, &nsim_dev->port_list);
771 
772 	return 0;
773 
774 err_port_debugfs_exit:
775 	nsim_dev_port_debugfs_exit(nsim_dev_port);
776 err_dl_port_unregister:
777 	devlink_port_unregister(devlink_port);
778 err_port_free:
779 	kfree(nsim_dev_port);
780 	return err;
781 }
782 
783 static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
784 {
785 	struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
786 
787 	list_del(&nsim_dev_port->list);
788 	devlink_port_type_clear(devlink_port);
789 	nsim_destroy(nsim_dev_port->ns);
790 	nsim_dev_port_debugfs_exit(nsim_dev_port);
791 	devlink_port_unregister(devlink_port);
792 	kfree(nsim_dev_port);
793 }
794 
795 static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
796 {
797 	struct nsim_dev_port *nsim_dev_port, *tmp;
798 
799 	list_for_each_entry_safe(nsim_dev_port, tmp,
800 				 &nsim_dev->port_list, list)
801 		__nsim_dev_port_del(nsim_dev_port);
802 }
803 
804 int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
805 {
806 	struct nsim_dev *nsim_dev;
807 	int i;
808 	int err;
809 
810 	nsim_dev = nsim_dev_create(nsim_bus_dev, nsim_bus_dev->port_count);
811 	if (IS_ERR(nsim_dev))
812 		return PTR_ERR(nsim_dev);
813 	dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
814 
815 	for (i = 0; i < nsim_bus_dev->port_count; i++) {
816 		err = __nsim_dev_port_add(nsim_dev, i);
817 		if (err)
818 			goto err_port_del_all;
819 	}
820 	return 0;
821 
822 err_port_del_all:
823 	nsim_dev_port_del_all(nsim_dev);
824 	nsim_dev_destroy(nsim_dev);
825 	return err;
826 }
827 
828 void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
829 {
830 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
831 
832 	nsim_dev_port_del_all(nsim_dev);
833 	nsim_dev_destroy(nsim_dev);
834 }
835 
836 static struct nsim_dev_port *
837 __nsim_dev_port_lookup(struct nsim_dev *nsim_dev, unsigned int port_index)
838 {
839 	struct nsim_dev_port *nsim_dev_port;
840 
841 	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
842 		if (nsim_dev_port->port_index == port_index)
843 			return nsim_dev_port;
844 	return NULL;
845 }
846 
847 int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
848 		      unsigned int port_index)
849 {
850 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
851 	int err;
852 
853 	mutex_lock(&nsim_dev->port_list_lock);
854 	if (__nsim_dev_port_lookup(nsim_dev, port_index))
855 		err = -EEXIST;
856 	else
857 		err = __nsim_dev_port_add(nsim_dev, port_index);
858 	mutex_unlock(&nsim_dev->port_list_lock);
859 	return err;
860 }
861 
862 int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
863 		      unsigned int port_index)
864 {
865 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
866 	struct nsim_dev_port *nsim_dev_port;
867 	int err = 0;
868 
869 	mutex_lock(&nsim_dev->port_list_lock);
870 	nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, port_index);
871 	if (!nsim_dev_port)
872 		err = -ENOENT;
873 	else
874 		__nsim_dev_port_del(nsim_dev_port);
875 	mutex_unlock(&nsim_dev->port_list_lock);
876 	return err;
877 }
878 
879 int nsim_dev_init(void)
880 {
881 	nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
882 	if (IS_ERR_OR_NULL(nsim_dev_ddir))
883 		return -ENOMEM;
884 	return 0;
885 }
886 
887 void nsim_dev_exit(void)
888 {
889 	debugfs_remove_recursive(nsim_dev_ddir);
890 }
891