18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2018 Cumulus Networks. All rights reserved. 38c2ecf20Sopenharmony_ci * Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com> 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This software is licensed under the GNU General License Version 2, 68c2ecf20Sopenharmony_ci * June 1991 as shown in the file COPYING in the top-level directory of this 78c2ecf20Sopenharmony_ci * source tree. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" 108c2ecf20Sopenharmony_ci * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, 118c2ecf20Sopenharmony_ci * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 128c2ecf20Sopenharmony_ci * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 138c2ecf20Sopenharmony_ci * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME 148c2ecf20Sopenharmony_ci * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/in6.h> 188c2ecf20Sopenharmony_ci#include <linux/kernel.h> 198c2ecf20Sopenharmony_ci#include <linux/list.h> 208c2ecf20Sopenharmony_ci#include <linux/rhashtable.h> 218c2ecf20Sopenharmony_ci#include <linux/spinlock_types.h> 228c2ecf20Sopenharmony_ci#include <linux/types.h> 238c2ecf20Sopenharmony_ci#include <net/fib_notifier.h> 248c2ecf20Sopenharmony_ci#include <net/ip_fib.h> 258c2ecf20Sopenharmony_ci#include <net/ip6_fib.h> 268c2ecf20Sopenharmony_ci#include <net/fib_rules.h> 278c2ecf20Sopenharmony_ci#include <net/net_namespace.h> 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#include "netdevsim.h" 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistruct nsim_fib_entry { 328c2ecf20Sopenharmony_ci u64 max; 338c2ecf20Sopenharmony_ci u64 num; 348c2ecf20Sopenharmony_ci}; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistruct nsim_per_fib_data { 378c2ecf20Sopenharmony_ci struct nsim_fib_entry fib; 388c2ecf20Sopenharmony_ci struct nsim_fib_entry rules; 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistruct nsim_fib_data { 428c2ecf20Sopenharmony_ci struct notifier_block fib_nb; 438c2ecf20Sopenharmony_ci struct nsim_per_fib_data ipv4; 448c2ecf20Sopenharmony_ci struct nsim_per_fib_data ipv6; 458c2ecf20Sopenharmony_ci struct rhashtable fib_rt_ht; 468c2ecf20Sopenharmony_ci struct list_head fib_rt_list; 478c2ecf20Sopenharmony_ci spinlock_t fib_lock; /* Protects hashtable, list and accounting */ 488c2ecf20Sopenharmony_ci struct devlink *devlink; 498c2ecf20Sopenharmony_ci}; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistruct nsim_fib_rt_key { 528c2ecf20Sopenharmony_ci unsigned char addr[sizeof(struct in6_addr)]; 538c2ecf20Sopenharmony_ci unsigned char prefix_len; 548c2ecf20Sopenharmony_ci int family; 558c2ecf20Sopenharmony_ci u32 tb_id; 568c2ecf20Sopenharmony_ci}; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistruct nsim_fib_rt { 598c2ecf20Sopenharmony_ci struct nsim_fib_rt_key key; 608c2ecf20Sopenharmony_ci struct rhash_head ht_node; 618c2ecf20Sopenharmony_ci struct list_head list; /* Member of fib_rt_list */ 628c2ecf20Sopenharmony_ci}; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistruct nsim_fib4_rt { 658c2ecf20Sopenharmony_ci struct nsim_fib_rt common; 668c2ecf20Sopenharmony_ci struct fib_info *fi; 678c2ecf20Sopenharmony_ci u8 tos; 688c2ecf20Sopenharmony_ci u8 type; 698c2ecf20Sopenharmony_ci}; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistruct nsim_fib6_rt { 728c2ecf20Sopenharmony_ci struct nsim_fib_rt common; 738c2ecf20Sopenharmony_ci struct list_head nh_list; 748c2ecf20Sopenharmony_ci unsigned int nhs; 758c2ecf20Sopenharmony_ci}; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistruct nsim_fib6_rt_nh { 788c2ecf20Sopenharmony_ci struct list_head list; /* Member of nh_list */ 798c2ecf20Sopenharmony_ci struct fib6_info *rt; 808c2ecf20Sopenharmony_ci}; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic const struct rhashtable_params nsim_fib_rt_ht_params = { 838c2ecf20Sopenharmony_ci .key_offset = offsetof(struct nsim_fib_rt, key), 848c2ecf20Sopenharmony_ci .head_offset = offsetof(struct nsim_fib_rt, ht_node), 858c2ecf20Sopenharmony_ci .key_len = sizeof(struct nsim_fib_rt_key), 868c2ecf20Sopenharmony_ci .automatic_shrinking = true, 878c2ecf20Sopenharmony_ci}; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ciu64 nsim_fib_get_val(struct nsim_fib_data *fib_data, 908c2ecf20Sopenharmony_ci enum nsim_resource_id res_id, bool max) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci struct nsim_fib_entry *entry; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci switch (res_id) { 958c2ecf20Sopenharmony_ci case NSIM_RESOURCE_IPV4_FIB: 968c2ecf20Sopenharmony_ci entry = &fib_data->ipv4.fib; 978c2ecf20Sopenharmony_ci break; 988c2ecf20Sopenharmony_ci case NSIM_RESOURCE_IPV4_FIB_RULES: 998c2ecf20Sopenharmony_ci entry = &fib_data->ipv4.rules; 1008c2ecf20Sopenharmony_ci break; 1018c2ecf20Sopenharmony_ci case NSIM_RESOURCE_IPV6_FIB: 1028c2ecf20Sopenharmony_ci entry = &fib_data->ipv6.fib; 1038c2ecf20Sopenharmony_ci break; 1048c2ecf20Sopenharmony_ci case NSIM_RESOURCE_IPV6_FIB_RULES: 1058c2ecf20Sopenharmony_ci entry = &fib_data->ipv6.rules; 1068c2ecf20Sopenharmony_ci break; 1078c2ecf20Sopenharmony_ci default: 1088c2ecf20Sopenharmony_ci return 0; 1098c2ecf20Sopenharmony_ci } 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci return max ? entry->max : entry->num; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic void nsim_fib_set_max(struct nsim_fib_data *fib_data, 1158c2ecf20Sopenharmony_ci enum nsim_resource_id res_id, u64 val) 1168c2ecf20Sopenharmony_ci{ 1178c2ecf20Sopenharmony_ci struct nsim_fib_entry *entry; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci switch (res_id) { 1208c2ecf20Sopenharmony_ci case NSIM_RESOURCE_IPV4_FIB: 1218c2ecf20Sopenharmony_ci entry = &fib_data->ipv4.fib; 1228c2ecf20Sopenharmony_ci break; 1238c2ecf20Sopenharmony_ci case NSIM_RESOURCE_IPV4_FIB_RULES: 1248c2ecf20Sopenharmony_ci entry = &fib_data->ipv4.rules; 1258c2ecf20Sopenharmony_ci break; 1268c2ecf20Sopenharmony_ci case NSIM_RESOURCE_IPV6_FIB: 1278c2ecf20Sopenharmony_ci entry = &fib_data->ipv6.fib; 1288c2ecf20Sopenharmony_ci break; 1298c2ecf20Sopenharmony_ci case NSIM_RESOURCE_IPV6_FIB_RULES: 1308c2ecf20Sopenharmony_ci entry = &fib_data->ipv6.rules; 1318c2ecf20Sopenharmony_ci break; 1328c2ecf20Sopenharmony_ci default: 1338c2ecf20Sopenharmony_ci WARN_ON(1); 1348c2ecf20Sopenharmony_ci return; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci entry->max = val; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add, 1408c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci int err = 0; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci if (add) { 1458c2ecf20Sopenharmony_ci if (entry->num < entry->max) { 1468c2ecf20Sopenharmony_ci entry->num++; 1478c2ecf20Sopenharmony_ci } else { 1488c2ecf20Sopenharmony_ci err = -ENOSPC; 1498c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib rule entries"); 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci } else { 1528c2ecf20Sopenharmony_ci entry->num--; 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci return err; 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic int nsim_fib_rule_event(struct nsim_fib_data *data, 1598c2ecf20Sopenharmony_ci struct fib_notifier_info *info, bool add) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack = info->extack; 1628c2ecf20Sopenharmony_ci int err = 0; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci switch (info->family) { 1658c2ecf20Sopenharmony_ci case AF_INET: 1668c2ecf20Sopenharmony_ci err = nsim_fib_rule_account(&data->ipv4.rules, add, extack); 1678c2ecf20Sopenharmony_ci break; 1688c2ecf20Sopenharmony_ci case AF_INET6: 1698c2ecf20Sopenharmony_ci err = nsim_fib_rule_account(&data->ipv6.rules, add, extack); 1708c2ecf20Sopenharmony_ci break; 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci return err; 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic int nsim_fib_account(struct nsim_fib_entry *entry, bool add, 1778c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci int err = 0; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci if (add) { 1828c2ecf20Sopenharmony_ci if (entry->num < entry->max) { 1838c2ecf20Sopenharmony_ci entry->num++; 1848c2ecf20Sopenharmony_ci } else { 1858c2ecf20Sopenharmony_ci err = -ENOSPC; 1868c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries"); 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci } else { 1898c2ecf20Sopenharmony_ci entry->num--; 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci return err; 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_cistatic void nsim_fib_rt_init(struct nsim_fib_data *data, 1968c2ecf20Sopenharmony_ci struct nsim_fib_rt *fib_rt, const void *addr, 1978c2ecf20Sopenharmony_ci size_t addr_len, unsigned int prefix_len, 1988c2ecf20Sopenharmony_ci int family, u32 tb_id) 1998c2ecf20Sopenharmony_ci{ 2008c2ecf20Sopenharmony_ci memcpy(fib_rt->key.addr, addr, addr_len); 2018c2ecf20Sopenharmony_ci fib_rt->key.prefix_len = prefix_len; 2028c2ecf20Sopenharmony_ci fib_rt->key.family = family; 2038c2ecf20Sopenharmony_ci fib_rt->key.tb_id = tb_id; 2048c2ecf20Sopenharmony_ci list_add(&fib_rt->list, &data->fib_rt_list); 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cistatic void nsim_fib_rt_fini(struct nsim_fib_rt *fib_rt) 2088c2ecf20Sopenharmony_ci{ 2098c2ecf20Sopenharmony_ci list_del(&fib_rt->list); 2108c2ecf20Sopenharmony_ci} 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_cistatic struct nsim_fib_rt *nsim_fib_rt_lookup(struct rhashtable *fib_rt_ht, 2138c2ecf20Sopenharmony_ci const void *addr, size_t addr_len, 2148c2ecf20Sopenharmony_ci unsigned int prefix_len, 2158c2ecf20Sopenharmony_ci int family, u32 tb_id) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci struct nsim_fib_rt_key key; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci memset(&key, 0, sizeof(key)); 2208c2ecf20Sopenharmony_ci memcpy(key.addr, addr, addr_len); 2218c2ecf20Sopenharmony_ci key.prefix_len = prefix_len; 2228c2ecf20Sopenharmony_ci key.family = family; 2238c2ecf20Sopenharmony_ci key.tb_id = tb_id; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci return rhashtable_lookup_fast(fib_rt_ht, &key, nsim_fib_rt_ht_params); 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic struct nsim_fib4_rt * 2298c2ecf20Sopenharmony_cinsim_fib4_rt_create(struct nsim_fib_data *data, 2308c2ecf20Sopenharmony_ci struct fib_entry_notifier_info *fen_info) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci struct nsim_fib4_rt *fib4_rt; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci fib4_rt = kzalloc(sizeof(*fib4_rt), GFP_ATOMIC); 2358c2ecf20Sopenharmony_ci if (!fib4_rt) 2368c2ecf20Sopenharmony_ci return NULL; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci nsim_fib_rt_init(data, &fib4_rt->common, &fen_info->dst, sizeof(u32), 2398c2ecf20Sopenharmony_ci fen_info->dst_len, AF_INET, fen_info->tb_id); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci fib4_rt->fi = fen_info->fi; 2428c2ecf20Sopenharmony_ci fib_info_hold(fib4_rt->fi); 2438c2ecf20Sopenharmony_ci fib4_rt->tos = fen_info->tos; 2448c2ecf20Sopenharmony_ci fib4_rt->type = fen_info->type; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci return fib4_rt; 2478c2ecf20Sopenharmony_ci} 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_cistatic void nsim_fib4_rt_destroy(struct nsim_fib4_rt *fib4_rt) 2508c2ecf20Sopenharmony_ci{ 2518c2ecf20Sopenharmony_ci fib_info_put(fib4_rt->fi); 2528c2ecf20Sopenharmony_ci nsim_fib_rt_fini(&fib4_rt->common); 2538c2ecf20Sopenharmony_ci kfree(fib4_rt); 2548c2ecf20Sopenharmony_ci} 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_cistatic struct nsim_fib4_rt * 2578c2ecf20Sopenharmony_cinsim_fib4_rt_lookup(struct rhashtable *fib_rt_ht, 2588c2ecf20Sopenharmony_ci const struct fib_entry_notifier_info *fen_info) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci struct nsim_fib_rt *fib_rt; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &fen_info->dst, sizeof(u32), 2638c2ecf20Sopenharmony_ci fen_info->dst_len, AF_INET, 2648c2ecf20Sopenharmony_ci fen_info->tb_id); 2658c2ecf20Sopenharmony_ci if (!fib_rt) 2668c2ecf20Sopenharmony_ci return NULL; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci return container_of(fib_rt, struct nsim_fib4_rt, common); 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_cistatic void nsim_fib4_rt_hw_flags_set(struct net *net, 2728c2ecf20Sopenharmony_ci const struct nsim_fib4_rt *fib4_rt, 2738c2ecf20Sopenharmony_ci bool trap) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci u32 *p_dst = (u32 *) fib4_rt->common.key.addr; 2768c2ecf20Sopenharmony_ci int dst_len = fib4_rt->common.key.prefix_len; 2778c2ecf20Sopenharmony_ci struct fib_rt_info fri; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci fri.fi = fib4_rt->fi; 2808c2ecf20Sopenharmony_ci fri.tb_id = fib4_rt->common.key.tb_id; 2818c2ecf20Sopenharmony_ci fri.dst = cpu_to_be32(*p_dst); 2828c2ecf20Sopenharmony_ci fri.dst_len = dst_len; 2838c2ecf20Sopenharmony_ci fri.tos = fib4_rt->tos; 2848c2ecf20Sopenharmony_ci fri.type = fib4_rt->type; 2858c2ecf20Sopenharmony_ci fri.offload = false; 2868c2ecf20Sopenharmony_ci fri.trap = trap; 2878c2ecf20Sopenharmony_ci fib_alias_hw_flags_set(net, &fri); 2888c2ecf20Sopenharmony_ci} 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_cistatic int nsim_fib4_rt_add(struct nsim_fib_data *data, 2918c2ecf20Sopenharmony_ci struct nsim_fib4_rt *fib4_rt, 2928c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 2938c2ecf20Sopenharmony_ci{ 2948c2ecf20Sopenharmony_ci struct net *net = devlink_net(data->devlink); 2958c2ecf20Sopenharmony_ci int err; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci err = nsim_fib_account(&data->ipv4.fib, true, extack); 2988c2ecf20Sopenharmony_ci if (err) 2998c2ecf20Sopenharmony_ci return err; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci err = rhashtable_insert_fast(&data->fib_rt_ht, 3028c2ecf20Sopenharmony_ci &fib4_rt->common.ht_node, 3038c2ecf20Sopenharmony_ci nsim_fib_rt_ht_params); 3048c2ecf20Sopenharmony_ci if (err) { 3058c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Failed to insert IPv4 route"); 3068c2ecf20Sopenharmony_ci goto err_fib_dismiss; 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci nsim_fib4_rt_hw_flags_set(net, fib4_rt, true); 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci return 0; 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_cierr_fib_dismiss: 3148c2ecf20Sopenharmony_ci nsim_fib_account(&data->ipv4.fib, false, extack); 3158c2ecf20Sopenharmony_ci return err; 3168c2ecf20Sopenharmony_ci} 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_cistatic int nsim_fib4_rt_replace(struct nsim_fib_data *data, 3198c2ecf20Sopenharmony_ci struct nsim_fib4_rt *fib4_rt, 3208c2ecf20Sopenharmony_ci struct nsim_fib4_rt *fib4_rt_old, 3218c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 3228c2ecf20Sopenharmony_ci{ 3238c2ecf20Sopenharmony_ci struct net *net = devlink_net(data->devlink); 3248c2ecf20Sopenharmony_ci int err; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci /* We are replacing a route, so no need to change the accounting. */ 3278c2ecf20Sopenharmony_ci err = rhashtable_replace_fast(&data->fib_rt_ht, 3288c2ecf20Sopenharmony_ci &fib4_rt_old->common.ht_node, 3298c2ecf20Sopenharmony_ci &fib4_rt->common.ht_node, 3308c2ecf20Sopenharmony_ci nsim_fib_rt_ht_params); 3318c2ecf20Sopenharmony_ci if (err) { 3328c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Failed to replace IPv4 route"); 3338c2ecf20Sopenharmony_ci return err; 3348c2ecf20Sopenharmony_ci } 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci nsim_fib4_rt_hw_flags_set(net, fib4_rt, true); 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci nsim_fib4_rt_hw_flags_set(net, fib4_rt_old, false); 3398c2ecf20Sopenharmony_ci nsim_fib4_rt_destroy(fib4_rt_old); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci return 0; 3428c2ecf20Sopenharmony_ci} 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_cistatic int nsim_fib4_rt_insert(struct nsim_fib_data *data, 3458c2ecf20Sopenharmony_ci struct fib_entry_notifier_info *fen_info) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack = fen_info->info.extack; 3488c2ecf20Sopenharmony_ci struct nsim_fib4_rt *fib4_rt, *fib4_rt_old; 3498c2ecf20Sopenharmony_ci int err; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci fib4_rt = nsim_fib4_rt_create(data, fen_info); 3528c2ecf20Sopenharmony_ci if (!fib4_rt) 3538c2ecf20Sopenharmony_ci return -ENOMEM; 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci fib4_rt_old = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info); 3568c2ecf20Sopenharmony_ci if (!fib4_rt_old) 3578c2ecf20Sopenharmony_ci err = nsim_fib4_rt_add(data, fib4_rt, extack); 3588c2ecf20Sopenharmony_ci else 3598c2ecf20Sopenharmony_ci err = nsim_fib4_rt_replace(data, fib4_rt, fib4_rt_old, extack); 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci if (err) 3628c2ecf20Sopenharmony_ci nsim_fib4_rt_destroy(fib4_rt); 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci return err; 3658c2ecf20Sopenharmony_ci} 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_cistatic void nsim_fib4_rt_remove(struct nsim_fib_data *data, 3688c2ecf20Sopenharmony_ci const struct fib_entry_notifier_info *fen_info) 3698c2ecf20Sopenharmony_ci{ 3708c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack = fen_info->info.extack; 3718c2ecf20Sopenharmony_ci struct nsim_fib4_rt *fib4_rt; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci fib4_rt = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info); 3748c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(!fib4_rt)) 3758c2ecf20Sopenharmony_ci return; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci rhashtable_remove_fast(&data->fib_rt_ht, &fib4_rt->common.ht_node, 3788c2ecf20Sopenharmony_ci nsim_fib_rt_ht_params); 3798c2ecf20Sopenharmony_ci nsim_fib_account(&data->ipv4.fib, false, extack); 3808c2ecf20Sopenharmony_ci nsim_fib4_rt_destroy(fib4_rt); 3818c2ecf20Sopenharmony_ci} 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_cistatic int nsim_fib4_event(struct nsim_fib_data *data, 3848c2ecf20Sopenharmony_ci struct fib_notifier_info *info, 3858c2ecf20Sopenharmony_ci unsigned long event) 3868c2ecf20Sopenharmony_ci{ 3878c2ecf20Sopenharmony_ci struct fib_entry_notifier_info *fen_info; 3888c2ecf20Sopenharmony_ci int err = 0; 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci fen_info = container_of(info, struct fib_entry_notifier_info, info); 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci if (fen_info->fi->nh) { 3938c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(info->extack, "IPv4 route with nexthop objects is not supported"); 3948c2ecf20Sopenharmony_ci return 0; 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci switch (event) { 3988c2ecf20Sopenharmony_ci case FIB_EVENT_ENTRY_REPLACE: 3998c2ecf20Sopenharmony_ci err = nsim_fib4_rt_insert(data, fen_info); 4008c2ecf20Sopenharmony_ci break; 4018c2ecf20Sopenharmony_ci case FIB_EVENT_ENTRY_DEL: 4028c2ecf20Sopenharmony_ci nsim_fib4_rt_remove(data, fen_info); 4038c2ecf20Sopenharmony_ci break; 4048c2ecf20Sopenharmony_ci default: 4058c2ecf20Sopenharmony_ci break; 4068c2ecf20Sopenharmony_ci } 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci return err; 4098c2ecf20Sopenharmony_ci} 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_cistatic struct nsim_fib6_rt_nh * 4128c2ecf20Sopenharmony_cinsim_fib6_rt_nh_find(const struct nsim_fib6_rt *fib6_rt, 4138c2ecf20Sopenharmony_ci const struct fib6_info *rt) 4148c2ecf20Sopenharmony_ci{ 4158c2ecf20Sopenharmony_ci struct nsim_fib6_rt_nh *fib6_rt_nh; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list) { 4188c2ecf20Sopenharmony_ci if (fib6_rt_nh->rt == rt) 4198c2ecf20Sopenharmony_ci return fib6_rt_nh; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci return NULL; 4238c2ecf20Sopenharmony_ci} 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_cistatic int nsim_fib6_rt_nh_add(struct nsim_fib6_rt *fib6_rt, 4268c2ecf20Sopenharmony_ci struct fib6_info *rt) 4278c2ecf20Sopenharmony_ci{ 4288c2ecf20Sopenharmony_ci struct nsim_fib6_rt_nh *fib6_rt_nh; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci fib6_rt_nh = kzalloc(sizeof(*fib6_rt_nh), GFP_ATOMIC); 4318c2ecf20Sopenharmony_ci if (!fib6_rt_nh) 4328c2ecf20Sopenharmony_ci return -ENOMEM; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci fib6_info_hold(rt); 4358c2ecf20Sopenharmony_ci fib6_rt_nh->rt = rt; 4368c2ecf20Sopenharmony_ci list_add_tail(&fib6_rt_nh->list, &fib6_rt->nh_list); 4378c2ecf20Sopenharmony_ci fib6_rt->nhs++; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci return 0; 4408c2ecf20Sopenharmony_ci} 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_cistatic void nsim_fib6_rt_nh_del(struct nsim_fib6_rt *fib6_rt, 4438c2ecf20Sopenharmony_ci const struct fib6_info *rt) 4448c2ecf20Sopenharmony_ci{ 4458c2ecf20Sopenharmony_ci struct nsim_fib6_rt_nh *fib6_rt_nh; 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci fib6_rt_nh = nsim_fib6_rt_nh_find(fib6_rt, rt); 4488c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(!fib6_rt_nh)) 4498c2ecf20Sopenharmony_ci return; 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci fib6_rt->nhs--; 4528c2ecf20Sopenharmony_ci list_del(&fib6_rt_nh->list); 4538c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 4548c2ecf20Sopenharmony_ci fib6_info_release(fib6_rt_nh->rt); 4558c2ecf20Sopenharmony_ci#endif 4568c2ecf20Sopenharmony_ci kfree(fib6_rt_nh); 4578c2ecf20Sopenharmony_ci} 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_cistatic struct nsim_fib6_rt * 4608c2ecf20Sopenharmony_cinsim_fib6_rt_create(struct nsim_fib_data *data, 4618c2ecf20Sopenharmony_ci struct fib6_entry_notifier_info *fen6_info) 4628c2ecf20Sopenharmony_ci{ 4638c2ecf20Sopenharmony_ci struct fib6_info *iter, *rt = fen6_info->rt; 4648c2ecf20Sopenharmony_ci struct nsim_fib6_rt *fib6_rt; 4658c2ecf20Sopenharmony_ci int i = 0; 4668c2ecf20Sopenharmony_ci int err; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci fib6_rt = kzalloc(sizeof(*fib6_rt), GFP_ATOMIC); 4698c2ecf20Sopenharmony_ci if (!fib6_rt) 4708c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci nsim_fib_rt_init(data, &fib6_rt->common, &rt->fib6_dst.addr, 4738c2ecf20Sopenharmony_ci sizeof(rt->fib6_dst.addr), rt->fib6_dst.plen, AF_INET6, 4748c2ecf20Sopenharmony_ci rt->fib6_table->tb6_id); 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci /* We consider a multipath IPv6 route as one entry, but it can be made 4778c2ecf20Sopenharmony_ci * up from several fib6_info structs (one for each nexthop), so we 4788c2ecf20Sopenharmony_ci * add them all to the same list under the entry. 4798c2ecf20Sopenharmony_ci */ 4808c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&fib6_rt->nh_list); 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci err = nsim_fib6_rt_nh_add(fib6_rt, rt); 4838c2ecf20Sopenharmony_ci if (err) 4848c2ecf20Sopenharmony_ci goto err_fib_rt_fini; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci if (!fen6_info->nsiblings) 4878c2ecf20Sopenharmony_ci return fib6_rt; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 4908c2ecf20Sopenharmony_ci if (i == fen6_info->nsiblings) 4918c2ecf20Sopenharmony_ci break; 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci err = nsim_fib6_rt_nh_add(fib6_rt, iter); 4948c2ecf20Sopenharmony_ci if (err) 4958c2ecf20Sopenharmony_ci goto err_fib6_rt_nh_del; 4968c2ecf20Sopenharmony_ci i++; 4978c2ecf20Sopenharmony_ci } 4988c2ecf20Sopenharmony_ci WARN_ON_ONCE(i != fen6_info->nsiblings); 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci return fib6_rt; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_cierr_fib6_rt_nh_del: 5038c2ecf20Sopenharmony_ci list_for_each_entry_continue_reverse(iter, &rt->fib6_siblings, 5048c2ecf20Sopenharmony_ci fib6_siblings) 5058c2ecf20Sopenharmony_ci nsim_fib6_rt_nh_del(fib6_rt, iter); 5068c2ecf20Sopenharmony_ci nsim_fib6_rt_nh_del(fib6_rt, rt); 5078c2ecf20Sopenharmony_cierr_fib_rt_fini: 5088c2ecf20Sopenharmony_ci nsim_fib_rt_fini(&fib6_rt->common); 5098c2ecf20Sopenharmony_ci kfree(fib6_rt); 5108c2ecf20Sopenharmony_ci return ERR_PTR(err); 5118c2ecf20Sopenharmony_ci} 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_cistatic void nsim_fib6_rt_destroy(struct nsim_fib6_rt *fib6_rt) 5148c2ecf20Sopenharmony_ci{ 5158c2ecf20Sopenharmony_ci struct nsim_fib6_rt_nh *iter, *tmp; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci list_for_each_entry_safe(iter, tmp, &fib6_rt->nh_list, list) 5188c2ecf20Sopenharmony_ci nsim_fib6_rt_nh_del(fib6_rt, iter->rt); 5198c2ecf20Sopenharmony_ci WARN_ON_ONCE(!list_empty(&fib6_rt->nh_list)); 5208c2ecf20Sopenharmony_ci nsim_fib_rt_fini(&fib6_rt->common); 5218c2ecf20Sopenharmony_ci kfree(fib6_rt); 5228c2ecf20Sopenharmony_ci} 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_cistatic struct nsim_fib6_rt * 5258c2ecf20Sopenharmony_cinsim_fib6_rt_lookup(struct rhashtable *fib_rt_ht, const struct fib6_info *rt) 5268c2ecf20Sopenharmony_ci{ 5278c2ecf20Sopenharmony_ci struct nsim_fib_rt *fib_rt; 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &rt->fib6_dst.addr, 5308c2ecf20Sopenharmony_ci sizeof(rt->fib6_dst.addr), 5318c2ecf20Sopenharmony_ci rt->fib6_dst.plen, AF_INET6, 5328c2ecf20Sopenharmony_ci rt->fib6_table->tb6_id); 5338c2ecf20Sopenharmony_ci if (!fib_rt) 5348c2ecf20Sopenharmony_ci return NULL; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci return container_of(fib_rt, struct nsim_fib6_rt, common); 5378c2ecf20Sopenharmony_ci} 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_cistatic int nsim_fib6_rt_append(struct nsim_fib_data *data, 5408c2ecf20Sopenharmony_ci struct fib6_entry_notifier_info *fen6_info) 5418c2ecf20Sopenharmony_ci{ 5428c2ecf20Sopenharmony_ci struct fib6_info *iter, *rt = fen6_info->rt; 5438c2ecf20Sopenharmony_ci struct nsim_fib6_rt *fib6_rt; 5448c2ecf20Sopenharmony_ci int i = 0; 5458c2ecf20Sopenharmony_ci int err; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt); 5488c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(!fib6_rt)) 5498c2ecf20Sopenharmony_ci return -EINVAL; 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci err = nsim_fib6_rt_nh_add(fib6_rt, rt); 5528c2ecf20Sopenharmony_ci if (err) 5538c2ecf20Sopenharmony_ci return err; 5548c2ecf20Sopenharmony_ci rt->trap = true; 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci if (!fen6_info->nsiblings) 5578c2ecf20Sopenharmony_ci return 0; 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) { 5608c2ecf20Sopenharmony_ci if (i == fen6_info->nsiblings) 5618c2ecf20Sopenharmony_ci break; 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci err = nsim_fib6_rt_nh_add(fib6_rt, iter); 5648c2ecf20Sopenharmony_ci if (err) 5658c2ecf20Sopenharmony_ci goto err_fib6_rt_nh_del; 5668c2ecf20Sopenharmony_ci iter->trap = true; 5678c2ecf20Sopenharmony_ci i++; 5688c2ecf20Sopenharmony_ci } 5698c2ecf20Sopenharmony_ci WARN_ON_ONCE(i != fen6_info->nsiblings); 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci return 0; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_cierr_fib6_rt_nh_del: 5748c2ecf20Sopenharmony_ci list_for_each_entry_continue_reverse(iter, &rt->fib6_siblings, 5758c2ecf20Sopenharmony_ci fib6_siblings) { 5768c2ecf20Sopenharmony_ci iter->trap = false; 5778c2ecf20Sopenharmony_ci nsim_fib6_rt_nh_del(fib6_rt, iter); 5788c2ecf20Sopenharmony_ci } 5798c2ecf20Sopenharmony_ci rt->trap = false; 5808c2ecf20Sopenharmony_ci nsim_fib6_rt_nh_del(fib6_rt, rt); 5818c2ecf20Sopenharmony_ci return err; 5828c2ecf20Sopenharmony_ci} 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_cistatic void nsim_fib6_rt_hw_flags_set(const struct nsim_fib6_rt *fib6_rt, 5858c2ecf20Sopenharmony_ci bool trap) 5868c2ecf20Sopenharmony_ci{ 5878c2ecf20Sopenharmony_ci struct nsim_fib6_rt_nh *fib6_rt_nh; 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_ci list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list) 5908c2ecf20Sopenharmony_ci fib6_info_hw_flags_set(fib6_rt_nh->rt, false, trap); 5918c2ecf20Sopenharmony_ci} 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_cistatic int nsim_fib6_rt_add(struct nsim_fib_data *data, 5948c2ecf20Sopenharmony_ci struct nsim_fib6_rt *fib6_rt, 5958c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 5968c2ecf20Sopenharmony_ci{ 5978c2ecf20Sopenharmony_ci int err; 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci err = nsim_fib_account(&data->ipv6.fib, true, extack); 6008c2ecf20Sopenharmony_ci if (err) 6018c2ecf20Sopenharmony_ci return err; 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ci err = rhashtable_insert_fast(&data->fib_rt_ht, 6048c2ecf20Sopenharmony_ci &fib6_rt->common.ht_node, 6058c2ecf20Sopenharmony_ci nsim_fib_rt_ht_params); 6068c2ecf20Sopenharmony_ci if (err) { 6078c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Failed to insert IPv6 route"); 6088c2ecf20Sopenharmony_ci goto err_fib_dismiss; 6098c2ecf20Sopenharmony_ci } 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci nsim_fib6_rt_hw_flags_set(fib6_rt, true); 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci return 0; 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_cierr_fib_dismiss: 6168c2ecf20Sopenharmony_ci nsim_fib_account(&data->ipv6.fib, false, extack); 6178c2ecf20Sopenharmony_ci return err; 6188c2ecf20Sopenharmony_ci} 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_cistatic int nsim_fib6_rt_replace(struct nsim_fib_data *data, 6218c2ecf20Sopenharmony_ci struct nsim_fib6_rt *fib6_rt, 6228c2ecf20Sopenharmony_ci struct nsim_fib6_rt *fib6_rt_old, 6238c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 6248c2ecf20Sopenharmony_ci{ 6258c2ecf20Sopenharmony_ci int err; 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci /* We are replacing a route, so no need to change the accounting. */ 6288c2ecf20Sopenharmony_ci err = rhashtable_replace_fast(&data->fib_rt_ht, 6298c2ecf20Sopenharmony_ci &fib6_rt_old->common.ht_node, 6308c2ecf20Sopenharmony_ci &fib6_rt->common.ht_node, 6318c2ecf20Sopenharmony_ci nsim_fib_rt_ht_params); 6328c2ecf20Sopenharmony_ci if (err) { 6338c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Failed to replace IPv6 route"); 6348c2ecf20Sopenharmony_ci return err; 6358c2ecf20Sopenharmony_ci } 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci nsim_fib6_rt_hw_flags_set(fib6_rt, true); 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci nsim_fib6_rt_hw_flags_set(fib6_rt_old, false); 6408c2ecf20Sopenharmony_ci nsim_fib6_rt_destroy(fib6_rt_old); 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci return 0; 6438c2ecf20Sopenharmony_ci} 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_cistatic int nsim_fib6_rt_insert(struct nsim_fib_data *data, 6468c2ecf20Sopenharmony_ci struct fib6_entry_notifier_info *fen6_info) 6478c2ecf20Sopenharmony_ci{ 6488c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack = fen6_info->info.extack; 6498c2ecf20Sopenharmony_ci struct nsim_fib6_rt *fib6_rt, *fib6_rt_old; 6508c2ecf20Sopenharmony_ci int err; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci fib6_rt = nsim_fib6_rt_create(data, fen6_info); 6538c2ecf20Sopenharmony_ci if (IS_ERR(fib6_rt)) 6548c2ecf20Sopenharmony_ci return PTR_ERR(fib6_rt); 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci fib6_rt_old = nsim_fib6_rt_lookup(&data->fib_rt_ht, fen6_info->rt); 6578c2ecf20Sopenharmony_ci if (!fib6_rt_old) 6588c2ecf20Sopenharmony_ci err = nsim_fib6_rt_add(data, fib6_rt, extack); 6598c2ecf20Sopenharmony_ci else 6608c2ecf20Sopenharmony_ci err = nsim_fib6_rt_replace(data, fib6_rt, fib6_rt_old, extack); 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci if (err) 6638c2ecf20Sopenharmony_ci nsim_fib6_rt_destroy(fib6_rt); 6648c2ecf20Sopenharmony_ci 6658c2ecf20Sopenharmony_ci return err; 6668c2ecf20Sopenharmony_ci} 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_cistatic void 6698c2ecf20Sopenharmony_cinsim_fib6_rt_remove(struct nsim_fib_data *data, 6708c2ecf20Sopenharmony_ci const struct fib6_entry_notifier_info *fen6_info) 6718c2ecf20Sopenharmony_ci{ 6728c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack = fen6_info->info.extack; 6738c2ecf20Sopenharmony_ci struct nsim_fib6_rt *fib6_rt; 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci /* Multipath routes are first added to the FIB trie and only then 6768c2ecf20Sopenharmony_ci * notified. If we vetoed the addition, we will get a delete 6778c2ecf20Sopenharmony_ci * notification for a route we do not have. Therefore, do not warn if 6788c2ecf20Sopenharmony_ci * route was not found. 6798c2ecf20Sopenharmony_ci */ 6808c2ecf20Sopenharmony_ci fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, fen6_info->rt); 6818c2ecf20Sopenharmony_ci if (!fib6_rt) 6828c2ecf20Sopenharmony_ci return; 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ci /* If not all the nexthops are deleted, then only reduce the nexthop 6858c2ecf20Sopenharmony_ci * group. 6868c2ecf20Sopenharmony_ci */ 6878c2ecf20Sopenharmony_ci if (fen6_info->nsiblings + 1 != fib6_rt->nhs) { 6888c2ecf20Sopenharmony_ci nsim_fib6_rt_nh_del(fib6_rt, fen6_info->rt); 6898c2ecf20Sopenharmony_ci return; 6908c2ecf20Sopenharmony_ci } 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci rhashtable_remove_fast(&data->fib_rt_ht, &fib6_rt->common.ht_node, 6938c2ecf20Sopenharmony_ci nsim_fib_rt_ht_params); 6948c2ecf20Sopenharmony_ci nsim_fib_account(&data->ipv6.fib, false, extack); 6958c2ecf20Sopenharmony_ci nsim_fib6_rt_destroy(fib6_rt); 6968c2ecf20Sopenharmony_ci} 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_cistatic int nsim_fib6_event(struct nsim_fib_data *data, 6998c2ecf20Sopenharmony_ci struct fib_notifier_info *info, 7008c2ecf20Sopenharmony_ci unsigned long event) 7018c2ecf20Sopenharmony_ci{ 7028c2ecf20Sopenharmony_ci struct fib6_entry_notifier_info *fen6_info; 7038c2ecf20Sopenharmony_ci int err = 0; 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci fen6_info = container_of(info, struct fib6_entry_notifier_info, info); 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci if (fen6_info->rt->nh) { 7088c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(info->extack, "IPv6 route with nexthop objects is not supported"); 7098c2ecf20Sopenharmony_ci return 0; 7108c2ecf20Sopenharmony_ci } 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci if (fen6_info->rt->fib6_src.plen) { 7138c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(info->extack, "IPv6 source-specific route is not supported"); 7148c2ecf20Sopenharmony_ci return 0; 7158c2ecf20Sopenharmony_ci } 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci switch (event) { 7188c2ecf20Sopenharmony_ci case FIB_EVENT_ENTRY_REPLACE: 7198c2ecf20Sopenharmony_ci err = nsim_fib6_rt_insert(data, fen6_info); 7208c2ecf20Sopenharmony_ci break; 7218c2ecf20Sopenharmony_ci case FIB_EVENT_ENTRY_APPEND: 7228c2ecf20Sopenharmony_ci err = nsim_fib6_rt_append(data, fen6_info); 7238c2ecf20Sopenharmony_ci break; 7248c2ecf20Sopenharmony_ci case FIB_EVENT_ENTRY_DEL: 7258c2ecf20Sopenharmony_ci nsim_fib6_rt_remove(data, fen6_info); 7268c2ecf20Sopenharmony_ci break; 7278c2ecf20Sopenharmony_ci default: 7288c2ecf20Sopenharmony_ci break; 7298c2ecf20Sopenharmony_ci } 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_ci return err; 7328c2ecf20Sopenharmony_ci} 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_cistatic int nsim_fib_event(struct nsim_fib_data *data, 7358c2ecf20Sopenharmony_ci struct fib_notifier_info *info, unsigned long event) 7368c2ecf20Sopenharmony_ci{ 7378c2ecf20Sopenharmony_ci int err = 0; 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci switch (info->family) { 7408c2ecf20Sopenharmony_ci case AF_INET: 7418c2ecf20Sopenharmony_ci err = nsim_fib4_event(data, info, event); 7428c2ecf20Sopenharmony_ci break; 7438c2ecf20Sopenharmony_ci case AF_INET6: 7448c2ecf20Sopenharmony_ci err = nsim_fib6_event(data, info, event); 7458c2ecf20Sopenharmony_ci break; 7468c2ecf20Sopenharmony_ci } 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci return err; 7498c2ecf20Sopenharmony_ci} 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_cistatic int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event, 7528c2ecf20Sopenharmony_ci void *ptr) 7538c2ecf20Sopenharmony_ci{ 7548c2ecf20Sopenharmony_ci struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data, 7558c2ecf20Sopenharmony_ci fib_nb); 7568c2ecf20Sopenharmony_ci struct fib_notifier_info *info = ptr; 7578c2ecf20Sopenharmony_ci int err = 0; 7588c2ecf20Sopenharmony_ci 7598c2ecf20Sopenharmony_ci /* IPv6 routes can be added via RAs from softIRQ. */ 7608c2ecf20Sopenharmony_ci spin_lock_bh(&data->fib_lock); 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_ci switch (event) { 7638c2ecf20Sopenharmony_ci case FIB_EVENT_RULE_ADD: 7648c2ecf20Sopenharmony_ci case FIB_EVENT_RULE_DEL: 7658c2ecf20Sopenharmony_ci err = nsim_fib_rule_event(data, info, 7668c2ecf20Sopenharmony_ci event == FIB_EVENT_RULE_ADD); 7678c2ecf20Sopenharmony_ci break; 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci case FIB_EVENT_ENTRY_REPLACE: 7708c2ecf20Sopenharmony_ci case FIB_EVENT_ENTRY_APPEND: 7718c2ecf20Sopenharmony_ci case FIB_EVENT_ENTRY_DEL: 7728c2ecf20Sopenharmony_ci err = nsim_fib_event(data, info, event); 7738c2ecf20Sopenharmony_ci break; 7748c2ecf20Sopenharmony_ci } 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci spin_unlock_bh(&data->fib_lock); 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci return notifier_from_errno(err); 7798c2ecf20Sopenharmony_ci} 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_cistatic void nsim_fib4_rt_free(struct nsim_fib_rt *fib_rt, 7828c2ecf20Sopenharmony_ci struct nsim_fib_data *data) 7838c2ecf20Sopenharmony_ci{ 7848c2ecf20Sopenharmony_ci struct devlink *devlink = data->devlink; 7858c2ecf20Sopenharmony_ci struct nsim_fib4_rt *fib4_rt; 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ci fib4_rt = container_of(fib_rt, struct nsim_fib4_rt, common); 7888c2ecf20Sopenharmony_ci nsim_fib4_rt_hw_flags_set(devlink_net(devlink), fib4_rt, false); 7898c2ecf20Sopenharmony_ci nsim_fib_account(&data->ipv4.fib, false, NULL); 7908c2ecf20Sopenharmony_ci nsim_fib4_rt_destroy(fib4_rt); 7918c2ecf20Sopenharmony_ci} 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_cistatic void nsim_fib6_rt_free(struct nsim_fib_rt *fib_rt, 7948c2ecf20Sopenharmony_ci struct nsim_fib_data *data) 7958c2ecf20Sopenharmony_ci{ 7968c2ecf20Sopenharmony_ci struct nsim_fib6_rt *fib6_rt; 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci fib6_rt = container_of(fib_rt, struct nsim_fib6_rt, common); 7998c2ecf20Sopenharmony_ci nsim_fib6_rt_hw_flags_set(fib6_rt, false); 8008c2ecf20Sopenharmony_ci nsim_fib_account(&data->ipv6.fib, false, NULL); 8018c2ecf20Sopenharmony_ci nsim_fib6_rt_destroy(fib6_rt); 8028c2ecf20Sopenharmony_ci} 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_cistatic void nsim_fib_rt_free(void *ptr, void *arg) 8058c2ecf20Sopenharmony_ci{ 8068c2ecf20Sopenharmony_ci struct nsim_fib_rt *fib_rt = ptr; 8078c2ecf20Sopenharmony_ci struct nsim_fib_data *data = arg; 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_ci switch (fib_rt->key.family) { 8108c2ecf20Sopenharmony_ci case AF_INET: 8118c2ecf20Sopenharmony_ci nsim_fib4_rt_free(fib_rt, data); 8128c2ecf20Sopenharmony_ci break; 8138c2ecf20Sopenharmony_ci case AF_INET6: 8148c2ecf20Sopenharmony_ci nsim_fib6_rt_free(fib_rt, data); 8158c2ecf20Sopenharmony_ci break; 8168c2ecf20Sopenharmony_ci default: 8178c2ecf20Sopenharmony_ci WARN_ON_ONCE(1); 8188c2ecf20Sopenharmony_ci } 8198c2ecf20Sopenharmony_ci} 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci/* inconsistent dump, trying again */ 8228c2ecf20Sopenharmony_cistatic void nsim_fib_dump_inconsistent(struct notifier_block *nb) 8238c2ecf20Sopenharmony_ci{ 8248c2ecf20Sopenharmony_ci struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data, 8258c2ecf20Sopenharmony_ci fib_nb); 8268c2ecf20Sopenharmony_ci struct nsim_fib_rt *fib_rt, *fib_rt_tmp; 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci /* The notifier block is still not registered, so we do not need to 8298c2ecf20Sopenharmony_ci * take any locks here. 8308c2ecf20Sopenharmony_ci */ 8318c2ecf20Sopenharmony_ci list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) { 8328c2ecf20Sopenharmony_ci rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node, 8338c2ecf20Sopenharmony_ci nsim_fib_rt_ht_params); 8348c2ecf20Sopenharmony_ci nsim_fib_rt_free(fib_rt, data); 8358c2ecf20Sopenharmony_ci } 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci data->ipv4.rules.num = 0ULL; 8388c2ecf20Sopenharmony_ci data->ipv6.rules.num = 0ULL; 8398c2ecf20Sopenharmony_ci} 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_cistatic u64 nsim_fib_ipv4_resource_occ_get(void *priv) 8428c2ecf20Sopenharmony_ci{ 8438c2ecf20Sopenharmony_ci struct nsim_fib_data *data = priv; 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB, false); 8468c2ecf20Sopenharmony_ci} 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_cistatic u64 nsim_fib_ipv4_rules_res_occ_get(void *priv) 8498c2ecf20Sopenharmony_ci{ 8508c2ecf20Sopenharmony_ci struct nsim_fib_data *data = priv; 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ci return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB_RULES, false); 8538c2ecf20Sopenharmony_ci} 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_cistatic u64 nsim_fib_ipv6_resource_occ_get(void *priv) 8568c2ecf20Sopenharmony_ci{ 8578c2ecf20Sopenharmony_ci struct nsim_fib_data *data = priv; 8588c2ecf20Sopenharmony_ci 8598c2ecf20Sopenharmony_ci return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB, false); 8608c2ecf20Sopenharmony_ci} 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_cistatic u64 nsim_fib_ipv6_rules_res_occ_get(void *priv) 8638c2ecf20Sopenharmony_ci{ 8648c2ecf20Sopenharmony_ci struct nsim_fib_data *data = priv; 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB_RULES, false); 8678c2ecf20Sopenharmony_ci} 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_cistatic void nsim_fib_set_max_all(struct nsim_fib_data *data, 8708c2ecf20Sopenharmony_ci struct devlink *devlink) 8718c2ecf20Sopenharmony_ci{ 8728c2ecf20Sopenharmony_ci enum nsim_resource_id res_ids[] = { 8738c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES, 8748c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES 8758c2ecf20Sopenharmony_ci }; 8768c2ecf20Sopenharmony_ci int i; 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(res_ids); i++) { 8798c2ecf20Sopenharmony_ci int err; 8808c2ecf20Sopenharmony_ci u64 val; 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ci err = devlink_resource_size_get(devlink, res_ids[i], &val); 8838c2ecf20Sopenharmony_ci if (err) 8848c2ecf20Sopenharmony_ci val = (u64) -1; 8858c2ecf20Sopenharmony_ci nsim_fib_set_max(data, res_ids[i], val); 8868c2ecf20Sopenharmony_ci } 8878c2ecf20Sopenharmony_ci} 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_cistruct nsim_fib_data *nsim_fib_create(struct devlink *devlink, 8908c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 8918c2ecf20Sopenharmony_ci{ 8928c2ecf20Sopenharmony_ci struct nsim_fib_data *data; 8938c2ecf20Sopenharmony_ci int err; 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci data = kzalloc(sizeof(*data), GFP_KERNEL); 8968c2ecf20Sopenharmony_ci if (!data) 8978c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 8988c2ecf20Sopenharmony_ci data->devlink = devlink; 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_ci spin_lock_init(&data->fib_lock); 9018c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&data->fib_rt_list); 9028c2ecf20Sopenharmony_ci err = rhashtable_init(&data->fib_rt_ht, &nsim_fib_rt_ht_params); 9038c2ecf20Sopenharmony_ci if (err) 9048c2ecf20Sopenharmony_ci goto err_data_free; 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci nsim_fib_set_max_all(data, devlink); 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci data->fib_nb.notifier_call = nsim_fib_event_nb; 9098c2ecf20Sopenharmony_ci err = register_fib_notifier(devlink_net(devlink), &data->fib_nb, 9108c2ecf20Sopenharmony_ci nsim_fib_dump_inconsistent, extack); 9118c2ecf20Sopenharmony_ci if (err) { 9128c2ecf20Sopenharmony_ci pr_err("Failed to register fib notifier\n"); 9138c2ecf20Sopenharmony_ci goto err_rhashtable_destroy; 9148c2ecf20Sopenharmony_ci } 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_ci devlink_resource_occ_get_register(devlink, 9178c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV4_FIB, 9188c2ecf20Sopenharmony_ci nsim_fib_ipv4_resource_occ_get, 9198c2ecf20Sopenharmony_ci data); 9208c2ecf20Sopenharmony_ci devlink_resource_occ_get_register(devlink, 9218c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV4_FIB_RULES, 9228c2ecf20Sopenharmony_ci nsim_fib_ipv4_rules_res_occ_get, 9238c2ecf20Sopenharmony_ci data); 9248c2ecf20Sopenharmony_ci devlink_resource_occ_get_register(devlink, 9258c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV6_FIB, 9268c2ecf20Sopenharmony_ci nsim_fib_ipv6_resource_occ_get, 9278c2ecf20Sopenharmony_ci data); 9288c2ecf20Sopenharmony_ci devlink_resource_occ_get_register(devlink, 9298c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV6_FIB_RULES, 9308c2ecf20Sopenharmony_ci nsim_fib_ipv6_rules_res_occ_get, 9318c2ecf20Sopenharmony_ci data); 9328c2ecf20Sopenharmony_ci return data; 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_cierr_rhashtable_destroy: 9358c2ecf20Sopenharmony_ci rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free, 9368c2ecf20Sopenharmony_ci data); 9378c2ecf20Sopenharmony_cierr_data_free: 9388c2ecf20Sopenharmony_ci kfree(data); 9398c2ecf20Sopenharmony_ci return ERR_PTR(err); 9408c2ecf20Sopenharmony_ci} 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_civoid nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data) 9438c2ecf20Sopenharmony_ci{ 9448c2ecf20Sopenharmony_ci devlink_resource_occ_get_unregister(devlink, 9458c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV6_FIB_RULES); 9468c2ecf20Sopenharmony_ci devlink_resource_occ_get_unregister(devlink, 9478c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV6_FIB); 9488c2ecf20Sopenharmony_ci devlink_resource_occ_get_unregister(devlink, 9498c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV4_FIB_RULES); 9508c2ecf20Sopenharmony_ci devlink_resource_occ_get_unregister(devlink, 9518c2ecf20Sopenharmony_ci NSIM_RESOURCE_IPV4_FIB); 9528c2ecf20Sopenharmony_ci unregister_fib_notifier(devlink_net(devlink), &data->fib_nb); 9538c2ecf20Sopenharmony_ci rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free, 9548c2ecf20Sopenharmony_ci data); 9558c2ecf20Sopenharmony_ci WARN_ON_ONCE(!list_empty(&data->fib_rt_list)); 9568c2ecf20Sopenharmony_ci kfree(data); 9578c2ecf20Sopenharmony_ci} 958