dev.c 25 KB

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