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