2 * Copyright (c) 2018 Cumulus Networks. All rights reserved.
3 * Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com>
5 * This software is licensed under the GNU General License Version 2,
6 * June 1991 as shown in the file COPYING in the top-level directory of this
9 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
10 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
11 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
13 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
14 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
17 #include <linux/bitmap.h>
18 #include <linux/in6.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/rhashtable.h>
22 #include <linux/spinlock_types.h>
23 #include <linux/types.h>
24 #include <net/fib_notifier.h>
25 #include <net/inet_dscp.h>
26 #include <net/ip_fib.h>
27 #include <net/ip6_fib.h>
28 #include <net/fib_rules.h>
29 #include <net/net_namespace.h>
30 #include <net/nexthop.h>
31 #include <linux/debugfs.h>
33 #include "netdevsim.h"
35 struct nsim_fib_entry {
40 struct nsim_per_fib_data {
41 struct nsim_fib_entry fib;
42 struct nsim_fib_entry rules;
45 struct nsim_fib_data {
46 struct notifier_block fib_nb;
47 struct nsim_per_fib_data ipv4;
48 struct nsim_per_fib_data ipv6;
49 struct nsim_fib_entry nexthops;
50 struct rhashtable fib_rt_ht;
51 struct list_head fib_rt_list;
52 struct mutex fib_lock; /* Protects FIB HT and list */
53 struct notifier_block nexthop_nb;
54 struct rhashtable nexthop_ht;
55 struct devlink *devlink;
56 struct work_struct fib_event_work;
57 struct list_head fib_event_queue;
58 spinlock_t fib_event_queue_lock; /* Protects fib event queue list */
59 struct mutex nh_lock; /* Protects NH HT */
61 bool fail_route_offload;
62 bool fail_res_nexthop_group_replace;
63 bool fail_nexthop_bucket_replace;
66 struct nsim_fib_rt_key {
67 unsigned char addr[sizeof(struct in6_addr)];
68 unsigned char prefix_len;
74 struct nsim_fib_rt_key key;
75 struct rhash_head ht_node;
76 struct list_head list; /* Member of fib_rt_list */
80 struct nsim_fib_rt common;
87 struct nsim_fib_rt common;
88 struct list_head nh_list;
92 struct nsim_fib6_rt_nh {
93 struct list_head list; /* Member of nh_list */
97 struct nsim_fib6_event {
98 struct fib6_info **rt_arr;
102 struct nsim_fib_event {
103 struct list_head list; /* node in fib queue */
105 struct fib_entry_notifier_info fen_info;
106 struct nsim_fib6_event fib6_event;
108 struct nsim_fib_data *data;
113 static const struct rhashtable_params nsim_fib_rt_ht_params = {
114 .key_offset = offsetof(struct nsim_fib_rt, key),
115 .head_offset = offsetof(struct nsim_fib_rt, ht_node),
116 .key_len = sizeof(struct nsim_fib_rt_key),
117 .automatic_shrinking = true,
120 struct nsim_nexthop {
121 struct rhash_head ht_node;
127 static const struct rhashtable_params nsim_nexthop_ht_params = {
128 .key_offset = offsetof(struct nsim_nexthop, id),
129 .head_offset = offsetof(struct nsim_nexthop, ht_node),
130 .key_len = sizeof(u32),
131 .automatic_shrinking = true,
134 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
135 enum nsim_resource_id res_id, bool max)
137 struct nsim_fib_entry *entry;
140 case NSIM_RESOURCE_IPV4_FIB:
141 entry = &fib_data->ipv4.fib;
143 case NSIM_RESOURCE_IPV4_FIB_RULES:
144 entry = &fib_data->ipv4.rules;
146 case NSIM_RESOURCE_IPV6_FIB:
147 entry = &fib_data->ipv6.fib;
149 case NSIM_RESOURCE_IPV6_FIB_RULES:
150 entry = &fib_data->ipv6.rules;
152 case NSIM_RESOURCE_NEXTHOPS:
153 entry = &fib_data->nexthops;
159 return max ? entry->max : atomic64_read(&entry->num);
162 static void nsim_fib_set_max(struct nsim_fib_data *fib_data,
163 enum nsim_resource_id res_id, u64 val)
165 struct nsim_fib_entry *entry;
168 case NSIM_RESOURCE_IPV4_FIB:
169 entry = &fib_data->ipv4.fib;
171 case NSIM_RESOURCE_IPV4_FIB_RULES:
172 entry = &fib_data->ipv4.rules;
174 case NSIM_RESOURCE_IPV6_FIB:
175 entry = &fib_data->ipv6.fib;
177 case NSIM_RESOURCE_IPV6_FIB_RULES:
178 entry = &fib_data->ipv6.rules;
180 case NSIM_RESOURCE_NEXTHOPS:
181 entry = &fib_data->nexthops;
190 static int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add,
191 struct netlink_ext_ack *extack)
196 if (!atomic64_add_unless(&entry->num, 1, entry->max)) {
198 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib rule entries");
201 atomic64_dec_if_positive(&entry->num);
207 static int nsim_fib_rule_event(struct nsim_fib_data *data,
208 struct fib_notifier_info *info, bool add)
210 struct netlink_ext_ack *extack = info->extack;
213 switch (info->family) {
215 err = nsim_fib_rule_account(&data->ipv4.rules, add, extack);
218 err = nsim_fib_rule_account(&data->ipv6.rules, add, extack);
225 static int nsim_fib_account(struct nsim_fib_entry *entry, bool add)
230 if (!atomic64_add_unless(&entry->num, 1, entry->max))
233 atomic64_dec_if_positive(&entry->num);
239 static void nsim_fib_rt_init(struct nsim_fib_data *data,
240 struct nsim_fib_rt *fib_rt, const void *addr,
241 size_t addr_len, unsigned int prefix_len,
242 int family, u32 tb_id)
244 memcpy(fib_rt->key.addr, addr, addr_len);
245 fib_rt->key.prefix_len = prefix_len;
246 fib_rt->key.family = family;
247 fib_rt->key.tb_id = tb_id;
248 list_add(&fib_rt->list, &data->fib_rt_list);
251 static void nsim_fib_rt_fini(struct nsim_fib_rt *fib_rt)
253 list_del(&fib_rt->list);
256 static struct nsim_fib_rt *nsim_fib_rt_lookup(struct rhashtable *fib_rt_ht,
257 const void *addr, size_t addr_len,
258 unsigned int prefix_len,
259 int family, u32 tb_id)
261 struct nsim_fib_rt_key key;
263 memset(&key, 0, sizeof(key));
264 memcpy(key.addr, addr, addr_len);
265 key.prefix_len = prefix_len;
269 return rhashtable_lookup_fast(fib_rt_ht, &key, nsim_fib_rt_ht_params);
272 static struct nsim_fib4_rt *
273 nsim_fib4_rt_create(struct nsim_fib_data *data,
274 struct fib_entry_notifier_info *fen_info)
276 struct nsim_fib4_rt *fib4_rt;
278 fib4_rt = kzalloc(sizeof(*fib4_rt), GFP_KERNEL);
282 nsim_fib_rt_init(data, &fib4_rt->common, &fen_info->dst, sizeof(u32),
283 fen_info->dst_len, AF_INET, fen_info->tb_id);
285 fib4_rt->fi = fen_info->fi;
286 fib_info_hold(fib4_rt->fi);
287 fib4_rt->dscp = fen_info->dscp;
288 fib4_rt->type = fen_info->type;
293 static void nsim_fib4_rt_destroy(struct nsim_fib4_rt *fib4_rt)
295 fib_info_put(fib4_rt->fi);
296 nsim_fib_rt_fini(&fib4_rt->common);
300 static struct nsim_fib4_rt *
301 nsim_fib4_rt_lookup(struct rhashtable *fib_rt_ht,
302 const struct fib_entry_notifier_info *fen_info)
304 struct nsim_fib_rt *fib_rt;
306 fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &fen_info->dst, sizeof(u32),
307 fen_info->dst_len, AF_INET,
312 return container_of(fib_rt, struct nsim_fib4_rt, common);
316 nsim_fib4_rt_offload_failed_flag_set(struct net *net,
317 struct fib_entry_notifier_info *fen_info)
319 u32 *p_dst = (u32 *)&fen_info->dst;
320 struct fib_rt_info fri;
322 fri.fi = fen_info->fi;
323 fri.tb_id = fen_info->tb_id;
324 fri.dst = cpu_to_be32(*p_dst);
325 fri.dst_len = fen_info->dst_len;
326 fri.dscp = fen_info->dscp;
327 fri.type = fen_info->type;
330 fri.offload_failed = true;
331 fib_alias_hw_flags_set(net, &fri);
334 static void nsim_fib4_rt_hw_flags_set(struct net *net,
335 const struct nsim_fib4_rt *fib4_rt,
338 u32 *p_dst = (u32 *) fib4_rt->common.key.addr;
339 int dst_len = fib4_rt->common.key.prefix_len;
340 struct fib_rt_info fri;
342 fri.fi = fib4_rt->fi;
343 fri.tb_id = fib4_rt->common.key.tb_id;
344 fri.dst = cpu_to_be32(*p_dst);
345 fri.dst_len = dst_len;
346 fri.dscp = fib4_rt->dscp;
347 fri.type = fib4_rt->type;
350 fri.offload_failed = false;
351 fib_alias_hw_flags_set(net, &fri);
354 static int nsim_fib4_rt_add(struct nsim_fib_data *data,
355 struct nsim_fib4_rt *fib4_rt)
357 struct net *net = devlink_net(data->devlink);
360 err = rhashtable_insert_fast(&data->fib_rt_ht,
361 &fib4_rt->common.ht_node,
362 nsim_fib_rt_ht_params);
364 goto err_fib_dismiss;
366 /* Simulate hardware programming latency. */
368 nsim_fib4_rt_hw_flags_set(net, fib4_rt, true);
373 /* Drop the accounting that was increased from the notification
374 * context when FIB_EVENT_ENTRY_REPLACE was triggered.
376 nsim_fib_account(&data->ipv4.fib, false);
380 static int nsim_fib4_rt_replace(struct nsim_fib_data *data,
381 struct nsim_fib4_rt *fib4_rt,
382 struct nsim_fib4_rt *fib4_rt_old)
384 struct net *net = devlink_net(data->devlink);
387 /* We are replacing a route, so need to remove the accounting which
388 * was increased when FIB_EVENT_ENTRY_REPLACE was triggered.
390 err = nsim_fib_account(&data->ipv4.fib, false);
393 err = rhashtable_replace_fast(&data->fib_rt_ht,
394 &fib4_rt_old->common.ht_node,
395 &fib4_rt->common.ht_node,
396 nsim_fib_rt_ht_params);
401 nsim_fib4_rt_hw_flags_set(net, fib4_rt, true);
403 nsim_fib4_rt_hw_flags_set(net, fib4_rt_old, false);
404 nsim_fib4_rt_destroy(fib4_rt_old);
409 static int nsim_fib4_rt_insert(struct nsim_fib_data *data,
410 struct fib_entry_notifier_info *fen_info)
412 struct nsim_fib4_rt *fib4_rt, *fib4_rt_old;
415 if (data->fail_route_offload) {
416 /* For testing purposes, user set debugfs fail_route_offload
417 * value to true. Simulate hardware programming latency and then
424 fib4_rt = nsim_fib4_rt_create(data, fen_info);
428 fib4_rt_old = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info);
430 err = nsim_fib4_rt_add(data, fib4_rt);
432 err = nsim_fib4_rt_replace(data, fib4_rt, fib4_rt_old);
435 nsim_fib4_rt_destroy(fib4_rt);
440 static void nsim_fib4_rt_remove(struct nsim_fib_data *data,
441 const struct fib_entry_notifier_info *fen_info)
443 struct nsim_fib4_rt *fib4_rt;
445 fib4_rt = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info);
449 rhashtable_remove_fast(&data->fib_rt_ht, &fib4_rt->common.ht_node,
450 nsim_fib_rt_ht_params);
451 nsim_fib4_rt_destroy(fib4_rt);
454 static int nsim_fib4_event(struct nsim_fib_data *data,
455 struct fib_entry_notifier_info *fen_info,
461 case FIB_EVENT_ENTRY_REPLACE:
462 err = nsim_fib4_rt_insert(data, fen_info);
464 struct net *net = devlink_net(data->devlink);
466 nsim_fib4_rt_offload_failed_flag_set(net, fen_info);
469 case FIB_EVENT_ENTRY_DEL:
470 nsim_fib4_rt_remove(data, fen_info);
479 static struct nsim_fib6_rt_nh *
480 nsim_fib6_rt_nh_find(const struct nsim_fib6_rt *fib6_rt,
481 const struct fib6_info *rt)
483 struct nsim_fib6_rt_nh *fib6_rt_nh;
485 list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list) {
486 if (fib6_rt_nh->rt == rt)
493 static int nsim_fib6_rt_nh_add(struct nsim_fib6_rt *fib6_rt,
494 struct fib6_info *rt)
496 struct nsim_fib6_rt_nh *fib6_rt_nh;
498 fib6_rt_nh = kzalloc(sizeof(*fib6_rt_nh), GFP_KERNEL);
504 list_add_tail(&fib6_rt_nh->list, &fib6_rt->nh_list);
510 #if IS_ENABLED(CONFIG_IPV6)
511 static void nsim_rt6_release(struct fib6_info *rt)
513 fib6_info_release(rt);
516 static void nsim_rt6_release(struct fib6_info *rt)
521 static void nsim_fib6_rt_nh_del(struct nsim_fib6_rt *fib6_rt,
522 const struct fib6_info *rt)
524 struct nsim_fib6_rt_nh *fib6_rt_nh;
526 fib6_rt_nh = nsim_fib6_rt_nh_find(fib6_rt, rt);
531 list_del(&fib6_rt_nh->list);
532 nsim_rt6_release(fib6_rt_nh->rt);
536 static struct nsim_fib6_rt *
537 nsim_fib6_rt_create(struct nsim_fib_data *data,
538 struct fib6_info **rt_arr, unsigned int nrt6)
540 struct fib6_info *rt = rt_arr[0];
541 struct nsim_fib6_rt *fib6_rt;
545 fib6_rt = kzalloc(sizeof(*fib6_rt), GFP_KERNEL);
547 return ERR_PTR(-ENOMEM);
549 nsim_fib_rt_init(data, &fib6_rt->common, &rt->fib6_dst.addr,
550 sizeof(rt->fib6_dst.addr), rt->fib6_dst.plen, AF_INET6,
551 rt->fib6_table->tb6_id);
553 /* We consider a multipath IPv6 route as one entry, but it can be made
554 * up from several fib6_info structs (one for each nexthop), so we
555 * add them all to the same list under the entry.
557 INIT_LIST_HEAD(&fib6_rt->nh_list);
559 for (i = 0; i < nrt6; i++) {
560 err = nsim_fib6_rt_nh_add(fib6_rt, rt_arr[i]);
562 goto err_fib6_rt_nh_del;
568 for (i--; i >= 0; i--) {
569 nsim_fib6_rt_nh_del(fib6_rt, rt_arr[i]);
571 nsim_fib_rt_fini(&fib6_rt->common);
576 static void nsim_fib6_rt_destroy(struct nsim_fib6_rt *fib6_rt)
578 struct nsim_fib6_rt_nh *iter, *tmp;
580 list_for_each_entry_safe(iter, tmp, &fib6_rt->nh_list, list)
581 nsim_fib6_rt_nh_del(fib6_rt, iter->rt);
582 WARN_ON_ONCE(!list_empty(&fib6_rt->nh_list));
583 nsim_fib_rt_fini(&fib6_rt->common);
587 static struct nsim_fib6_rt *
588 nsim_fib6_rt_lookup(struct rhashtable *fib_rt_ht, const struct fib6_info *rt)
590 struct nsim_fib_rt *fib_rt;
592 fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &rt->fib6_dst.addr,
593 sizeof(rt->fib6_dst.addr),
594 rt->fib6_dst.plen, AF_INET6,
595 rt->fib6_table->tb6_id);
599 return container_of(fib_rt, struct nsim_fib6_rt, common);
602 static int nsim_fib6_rt_append(struct nsim_fib_data *data,
603 struct nsim_fib6_event *fib6_event)
605 struct fib6_info *rt = fib6_event->rt_arr[0];
606 struct nsim_fib6_rt *fib6_rt;
609 if (data->fail_route_offload) {
610 /* For testing purposes, user set debugfs fail_route_offload
611 * value to true. Simulate hardware programming latency and then
618 fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
622 for (i = 0; i < fib6_event->nrt6; i++) {
623 err = nsim_fib6_rt_nh_add(fib6_rt, fib6_event->rt_arr[i]);
625 goto err_fib6_rt_nh_del;
627 WRITE_ONCE(fib6_event->rt_arr[i]->trap, true);
633 for (i--; i >= 0; i--) {
634 WRITE_ONCE(fib6_event->rt_arr[i]->trap, false);
635 nsim_fib6_rt_nh_del(fib6_rt, fib6_event->rt_arr[i]);
640 #if IS_ENABLED(CONFIG_IPV6)
641 static void nsim_fib6_rt_offload_failed_flag_set(struct nsim_fib_data *data,
642 struct fib6_info **rt_arr,
646 struct net *net = devlink_net(data->devlink);
649 for (i = 0; i < nrt6; i++)
650 fib6_info_hw_flags_set(net, rt_arr[i], false, false, true);
653 static void nsim_fib6_rt_offload_failed_flag_set(struct nsim_fib_data *data,
654 struct fib6_info **rt_arr,
660 #if IS_ENABLED(CONFIG_IPV6)
661 static void nsim_fib6_rt_hw_flags_set(struct nsim_fib_data *data,
662 const struct nsim_fib6_rt *fib6_rt,
665 struct net *net = devlink_net(data->devlink);
666 struct nsim_fib6_rt_nh *fib6_rt_nh;
668 list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list)
669 fib6_info_hw_flags_set(net, fib6_rt_nh->rt, false, trap, false);
672 static void nsim_fib6_rt_hw_flags_set(struct nsim_fib_data *data,
673 const struct nsim_fib6_rt *fib6_rt,
679 static int nsim_fib6_rt_add(struct nsim_fib_data *data,
680 struct nsim_fib6_rt *fib6_rt)
684 err = rhashtable_insert_fast(&data->fib_rt_ht,
685 &fib6_rt->common.ht_node,
686 nsim_fib_rt_ht_params);
689 goto err_fib_dismiss;
692 nsim_fib6_rt_hw_flags_set(data, fib6_rt, true);
697 /* Drop the accounting that was increased from the notification
698 * context when FIB_EVENT_ENTRY_REPLACE was triggered.
700 nsim_fib_account(&data->ipv6.fib, false);
704 static int nsim_fib6_rt_replace(struct nsim_fib_data *data,
705 struct nsim_fib6_rt *fib6_rt,
706 struct nsim_fib6_rt *fib6_rt_old)
710 /* We are replacing a route, so need to remove the accounting which
711 * was increased when FIB_EVENT_ENTRY_REPLACE was triggered.
713 err = nsim_fib_account(&data->ipv6.fib, false);
717 err = rhashtable_replace_fast(&data->fib_rt_ht,
718 &fib6_rt_old->common.ht_node,
719 &fib6_rt->common.ht_node,
720 nsim_fib_rt_ht_params);
726 nsim_fib6_rt_hw_flags_set(data, fib6_rt, true);
728 nsim_fib6_rt_hw_flags_set(data, fib6_rt_old, false);
729 nsim_fib6_rt_destroy(fib6_rt_old);
734 static int nsim_fib6_rt_insert(struct nsim_fib_data *data,
735 struct nsim_fib6_event *fib6_event)
737 struct fib6_info *rt = fib6_event->rt_arr[0];
738 struct nsim_fib6_rt *fib6_rt, *fib6_rt_old;
741 if (data->fail_route_offload) {
742 /* For testing purposes, user set debugfs fail_route_offload
743 * value to true. Simulate hardware programming latency and then
750 fib6_rt = nsim_fib6_rt_create(data, fib6_event->rt_arr,
753 return PTR_ERR(fib6_rt);
755 fib6_rt_old = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
757 err = nsim_fib6_rt_add(data, fib6_rt);
759 err = nsim_fib6_rt_replace(data, fib6_rt, fib6_rt_old);
762 nsim_fib6_rt_destroy(fib6_rt);
767 static void nsim_fib6_rt_remove(struct nsim_fib_data *data,
768 struct nsim_fib6_event *fib6_event)
770 struct fib6_info *rt = fib6_event->rt_arr[0];
771 struct nsim_fib6_rt *fib6_rt;
774 /* Multipath routes are first added to the FIB trie and only then
775 * notified. If we vetoed the addition, we will get a delete
776 * notification for a route we do not have. Therefore, do not warn if
777 * route was not found.
779 fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
783 /* If not all the nexthops are deleted, then only reduce the nexthop
786 if (fib6_event->nrt6 != fib6_rt->nhs) {
787 for (i = 0; i < fib6_event->nrt6; i++)
788 nsim_fib6_rt_nh_del(fib6_rt, fib6_event->rt_arr[i]);
792 rhashtable_remove_fast(&data->fib_rt_ht, &fib6_rt->common.ht_node,
793 nsim_fib_rt_ht_params);
794 nsim_fib6_rt_destroy(fib6_rt);
797 static int nsim_fib6_event_init(struct nsim_fib6_event *fib6_event,
798 struct fib6_entry_notifier_info *fen6_info)
800 struct fib6_info *rt = fen6_info->rt;
801 struct fib6_info **rt_arr;
802 struct fib6_info *iter;
806 nrt6 = fen6_info->nsiblings + 1;
808 rt_arr = kcalloc(nrt6, sizeof(struct fib6_info *), GFP_ATOMIC);
812 fib6_event->rt_arr = rt_arr;
813 fib6_event->nrt6 = nrt6;
818 if (!fen6_info->nsiblings)
821 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
822 if (i == fen6_info->nsiblings)
825 rt_arr[i + 1] = iter;
826 fib6_info_hold(iter);
829 WARN_ON_ONCE(i != fen6_info->nsiblings);
834 static void nsim_fib6_event_fini(struct nsim_fib6_event *fib6_event)
838 for (i = 0; i < fib6_event->nrt6; i++)
839 nsim_rt6_release(fib6_event->rt_arr[i]);
840 kfree(fib6_event->rt_arr);
843 static int nsim_fib6_event(struct nsim_fib_data *data,
844 struct nsim_fib6_event *fib6_event,
849 if (fib6_event->rt_arr[0]->fib6_src.plen)
853 case FIB_EVENT_ENTRY_REPLACE:
854 err = nsim_fib6_rt_insert(data, fib6_event);
856 goto err_rt_offload_failed_flag_set;
858 case FIB_EVENT_ENTRY_APPEND:
859 err = nsim_fib6_rt_append(data, fib6_event);
861 goto err_rt_offload_failed_flag_set;
863 case FIB_EVENT_ENTRY_DEL:
864 nsim_fib6_rt_remove(data, fib6_event);
872 err_rt_offload_failed_flag_set:
873 nsim_fib6_rt_offload_failed_flag_set(data, fib6_event->rt_arr,
878 static void nsim_fib_event(struct nsim_fib_event *fib_event)
880 switch (fib_event->family) {
882 nsim_fib4_event(fib_event->data, &fib_event->fen_info,
884 fib_info_put(fib_event->fen_info.fi);
887 nsim_fib6_event(fib_event->data, &fib_event->fib6_event,
889 nsim_fib6_event_fini(&fib_event->fib6_event);
894 static int nsim_fib4_prepare_event(struct fib_notifier_info *info,
895 struct nsim_fib_event *fib_event,
898 struct nsim_fib_data *data = fib_event->data;
899 struct fib_entry_notifier_info *fen_info;
900 struct netlink_ext_ack *extack;
903 fen_info = container_of(info, struct fib_entry_notifier_info,
905 fib_event->fen_info = *fen_info;
906 extack = info->extack;
909 case FIB_EVENT_ENTRY_REPLACE:
910 err = nsim_fib_account(&data->ipv4.fib, true);
912 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
916 case FIB_EVENT_ENTRY_DEL:
917 nsim_fib_account(&data->ipv4.fib, false);
921 /* Take reference on fib_info to prevent it from being
922 * freed while event is queued. Release it afterwards.
924 fib_info_hold(fib_event->fen_info.fi);
929 static int nsim_fib6_prepare_event(struct fib_notifier_info *info,
930 struct nsim_fib_event *fib_event,
933 struct nsim_fib_data *data = fib_event->data;
934 struct fib6_entry_notifier_info *fen6_info;
935 struct netlink_ext_ack *extack;
938 fen6_info = container_of(info, struct fib6_entry_notifier_info,
941 err = nsim_fib6_event_init(&fib_event->fib6_event, fen6_info);
945 extack = info->extack;
947 case FIB_EVENT_ENTRY_REPLACE:
948 err = nsim_fib_account(&data->ipv6.fib, true);
950 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
951 goto err_fib6_event_fini;
954 case FIB_EVENT_ENTRY_DEL:
955 nsim_fib_account(&data->ipv6.fib, false);
962 nsim_fib6_event_fini(&fib_event->fib6_event);
966 static int nsim_fib_event_schedule_work(struct nsim_fib_data *data,
967 struct fib_notifier_info *info,
970 struct nsim_fib_event *fib_event;
973 if (info->family != AF_INET && info->family != AF_INET6)
974 /* netdevsim does not support 'RTNL_FAMILY_IP6MR' and
975 * 'RTNL_FAMILY_IPMR' and should ignore them.
979 fib_event = kzalloc(sizeof(*fib_event), GFP_ATOMIC);
983 fib_event->data = data;
984 fib_event->event = event;
985 fib_event->family = info->family;
987 switch (info->family) {
989 err = nsim_fib4_prepare_event(info, fib_event, event);
992 err = nsim_fib6_prepare_event(info, fib_event, event);
997 goto err_fib_prepare_event;
999 /* Enqueue the event and trigger the work */
1000 spin_lock_bh(&data->fib_event_queue_lock);
1001 list_add_tail(&fib_event->list, &data->fib_event_queue);
1002 spin_unlock_bh(&data->fib_event_queue_lock);
1003 schedule_work(&data->fib_event_work);
1007 err_fib_prepare_event:
1012 static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event,
1015 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1017 struct fib_notifier_info *info = ptr;
1021 case FIB_EVENT_RULE_ADD:
1022 case FIB_EVENT_RULE_DEL:
1023 err = nsim_fib_rule_event(data, info,
1024 event == FIB_EVENT_RULE_ADD);
1025 return notifier_from_errno(err);
1026 case FIB_EVENT_ENTRY_REPLACE:
1027 case FIB_EVENT_ENTRY_APPEND:
1028 case FIB_EVENT_ENTRY_DEL:
1029 return nsim_fib_event_schedule_work(data, info, event);
1035 static void nsim_fib4_rt_free(struct nsim_fib_rt *fib_rt,
1036 struct nsim_fib_data *data)
1038 struct devlink *devlink = data->devlink;
1039 struct nsim_fib4_rt *fib4_rt;
1041 fib4_rt = container_of(fib_rt, struct nsim_fib4_rt, common);
1042 nsim_fib4_rt_hw_flags_set(devlink_net(devlink), fib4_rt, false);
1043 nsim_fib_account(&data->ipv4.fib, false);
1044 nsim_fib4_rt_destroy(fib4_rt);
1047 static void nsim_fib6_rt_free(struct nsim_fib_rt *fib_rt,
1048 struct nsim_fib_data *data)
1050 struct nsim_fib6_rt *fib6_rt;
1052 fib6_rt = container_of(fib_rt, struct nsim_fib6_rt, common);
1053 nsim_fib6_rt_hw_flags_set(data, fib6_rt, false);
1054 nsim_fib_account(&data->ipv6.fib, false);
1055 nsim_fib6_rt_destroy(fib6_rt);
1058 static void nsim_fib_rt_free(void *ptr, void *arg)
1060 struct nsim_fib_rt *fib_rt = ptr;
1061 struct nsim_fib_data *data = arg;
1063 switch (fib_rt->key.family) {
1065 nsim_fib4_rt_free(fib_rt, data);
1068 nsim_fib6_rt_free(fib_rt, data);
1075 /* inconsistent dump, trying again */
1076 static void nsim_fib_dump_inconsistent(struct notifier_block *nb)
1078 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1080 struct nsim_fib_rt *fib_rt, *fib_rt_tmp;
1082 /* Flush the work to make sure there is no race with notifications. */
1083 flush_work(&data->fib_event_work);
1085 /* The notifier block is still not registered, so we do not need to
1086 * take any locks here.
1088 list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) {
1089 rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node,
1090 nsim_fib_rt_ht_params);
1091 nsim_fib_rt_free(fib_rt, data);
1094 atomic64_set(&data->ipv4.rules.num, 0ULL);
1095 atomic64_set(&data->ipv6.rules.num, 0ULL);
1098 static struct nsim_nexthop *nsim_nexthop_create(struct nsim_fib_data *data,
1099 struct nh_notifier_info *info)
1101 struct nsim_nexthop *nexthop;
1105 nexthop = kzalloc(sizeof(*nexthop), GFP_KERNEL);
1107 return ERR_PTR(-ENOMEM);
1109 nexthop->id = info->id;
1111 /* Determine the number of nexthop entries the new nexthop will
1115 switch (info->type) {
1116 case NH_NOTIFIER_INFO_TYPE_SINGLE:
1119 case NH_NOTIFIER_INFO_TYPE_GRP:
1120 for (i = 0; i < info->nh_grp->num_nh; i++)
1121 occ += info->nh_grp->nh_entries[i].weight;
1123 case NH_NOTIFIER_INFO_TYPE_RES_TABLE:
1124 occ = info->nh_res_table->num_nh_buckets;
1125 nexthop->is_resilient = true;
1128 NL_SET_ERR_MSG_MOD(info->extack, "Unsupported nexthop type");
1130 return ERR_PTR(-EOPNOTSUPP);
1137 static void nsim_nexthop_destroy(struct nsim_nexthop *nexthop)
1142 static int nsim_nexthop_account(struct nsim_fib_data *data, u64 occ,
1143 bool add, struct netlink_ext_ack *extack)
1148 for (i = 0; i < occ; i++)
1149 if (!atomic64_add_unless(&data->nexthops.num, 1,
1150 data->nexthops.max)) {
1152 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported nexthops");
1153 goto err_num_decrease;
1156 if (WARN_ON(occ > atomic64_read(&data->nexthops.num)))
1158 atomic64_sub(occ, &data->nexthops.num);
1164 atomic64_sub(i, &data->nexthops.num);
1169 static void nsim_nexthop_hw_flags_set(struct net *net,
1170 const struct nsim_nexthop *nexthop,
1175 nexthop_set_hw_flags(net, nexthop->id, false, trap);
1177 if (!nexthop->is_resilient)
1180 for (i = 0; i < nexthop->occ; i++)
1181 nexthop_bucket_set_hw_flags(net, nexthop->id, i, false, trap);
1184 static int nsim_nexthop_add(struct nsim_fib_data *data,
1185 struct nsim_nexthop *nexthop,
1186 struct netlink_ext_ack *extack)
1188 struct net *net = devlink_net(data->devlink);
1191 err = nsim_nexthop_account(data, nexthop->occ, true, extack);
1195 err = rhashtable_insert_fast(&data->nexthop_ht, &nexthop->ht_node,
1196 nsim_nexthop_ht_params);
1198 NL_SET_ERR_MSG_MOD(extack, "Failed to insert nexthop");
1199 goto err_nexthop_dismiss;
1202 nsim_nexthop_hw_flags_set(net, nexthop, true);
1206 err_nexthop_dismiss:
1207 nsim_nexthop_account(data, nexthop->occ, false, extack);
1211 static int nsim_nexthop_replace(struct nsim_fib_data *data,
1212 struct nsim_nexthop *nexthop,
1213 struct nsim_nexthop *nexthop_old,
1214 struct netlink_ext_ack *extack)
1216 struct net *net = devlink_net(data->devlink);
1219 err = nsim_nexthop_account(data, nexthop->occ, true, extack);
1223 err = rhashtable_replace_fast(&data->nexthop_ht,
1224 &nexthop_old->ht_node, &nexthop->ht_node,
1225 nsim_nexthop_ht_params);
1227 NL_SET_ERR_MSG_MOD(extack, "Failed to replace nexthop");
1228 goto err_nexthop_dismiss;
1231 nsim_nexthop_hw_flags_set(net, nexthop, true);
1232 nsim_nexthop_account(data, nexthop_old->occ, false, extack);
1233 nsim_nexthop_destroy(nexthop_old);
1237 err_nexthop_dismiss:
1238 nsim_nexthop_account(data, nexthop->occ, false, extack);
1242 static int nsim_nexthop_insert(struct nsim_fib_data *data,
1243 struct nh_notifier_info *info)
1245 struct nsim_nexthop *nexthop, *nexthop_old;
1248 nexthop = nsim_nexthop_create(data, info);
1249 if (IS_ERR(nexthop))
1250 return PTR_ERR(nexthop);
1252 nexthop_old = rhashtable_lookup_fast(&data->nexthop_ht, &info->id,
1253 nsim_nexthop_ht_params);
1255 err = nsim_nexthop_add(data, nexthop, info->extack);
1257 err = nsim_nexthop_replace(data, nexthop, nexthop_old,
1261 nsim_nexthop_destroy(nexthop);
1266 static void nsim_nexthop_remove(struct nsim_fib_data *data,
1267 struct nh_notifier_info *info)
1269 struct nsim_nexthop *nexthop;
1271 nexthop = rhashtable_lookup_fast(&data->nexthop_ht, &info->id,
1272 nsim_nexthop_ht_params);
1276 rhashtable_remove_fast(&data->nexthop_ht, &nexthop->ht_node,
1277 nsim_nexthop_ht_params);
1278 nsim_nexthop_account(data, nexthop->occ, false, info->extack);
1279 nsim_nexthop_destroy(nexthop);
1282 static int nsim_nexthop_res_table_pre_replace(struct nsim_fib_data *data,
1283 struct nh_notifier_info *info)
1285 if (data->fail_res_nexthop_group_replace) {
1286 NL_SET_ERR_MSG_MOD(info->extack, "Failed to replace a resilient nexthop group");
1293 static int nsim_nexthop_bucket_replace(struct nsim_fib_data *data,
1294 struct nh_notifier_info *info)
1296 if (data->fail_nexthop_bucket_replace) {
1297 NL_SET_ERR_MSG_MOD(info->extack, "Failed to replace nexthop bucket");
1301 nexthop_bucket_set_hw_flags(info->net, info->id,
1302 info->nh_res_bucket->bucket_index,
1308 static int nsim_nexthop_event_nb(struct notifier_block *nb, unsigned long event,
1311 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1313 struct nh_notifier_info *info = ptr;
1316 mutex_lock(&data->nh_lock);
1318 case NEXTHOP_EVENT_REPLACE:
1319 err = nsim_nexthop_insert(data, info);
1321 case NEXTHOP_EVENT_DEL:
1322 nsim_nexthop_remove(data, info);
1324 case NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE:
1325 err = nsim_nexthop_res_table_pre_replace(data, info);
1327 case NEXTHOP_EVENT_BUCKET_REPLACE:
1328 err = nsim_nexthop_bucket_replace(data, info);
1334 mutex_unlock(&data->nh_lock);
1335 return notifier_from_errno(err);
1338 static void nsim_nexthop_free(void *ptr, void *arg)
1340 struct nsim_nexthop *nexthop = ptr;
1341 struct nsim_fib_data *data = arg;
1344 net = devlink_net(data->devlink);
1345 nsim_nexthop_hw_flags_set(net, nexthop, false);
1346 nsim_nexthop_account(data, nexthop->occ, false, NULL);
1347 nsim_nexthop_destroy(nexthop);
1350 static ssize_t nsim_nexthop_bucket_activity_write(struct file *file,
1351 const char __user *user_buf,
1352 size_t size, loff_t *ppos)
1354 struct nsim_fib_data *data = file->private_data;
1355 struct net *net = devlink_net(data->devlink);
1356 struct nsim_nexthop *nexthop;
1357 unsigned long *activity;
1366 if (size > sizeof(buf))
1368 if (copy_from_user(buf, user_buf, size))
1370 if (sscanf(buf, "%u %hu", &nhid, &bucket_index) != 2)
1375 nexthop = rhashtable_lookup_fast(&data->nexthop_ht, &nhid,
1376 nsim_nexthop_ht_params);
1377 if (!nexthop || !nexthop->is_resilient ||
1378 bucket_index >= nexthop->occ) {
1383 activity = bitmap_zalloc(nexthop->occ, GFP_KERNEL);
1389 bitmap_set(activity, bucket_index, 1);
1390 nexthop_res_grp_activity_update(net, nhid, nexthop->occ, activity);
1391 bitmap_free(activity);
1400 static const struct file_operations nsim_nexthop_bucket_activity_fops = {
1401 .open = simple_open,
1402 .write = nsim_nexthop_bucket_activity_write,
1403 .llseek = no_llseek,
1404 .owner = THIS_MODULE,
1407 static u64 nsim_fib_ipv4_resource_occ_get(void *priv)
1409 struct nsim_fib_data *data = priv;
1411 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB, false);
1414 static u64 nsim_fib_ipv4_rules_res_occ_get(void *priv)
1416 struct nsim_fib_data *data = priv;
1418 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB_RULES, false);
1421 static u64 nsim_fib_ipv6_resource_occ_get(void *priv)
1423 struct nsim_fib_data *data = priv;
1425 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB, false);
1428 static u64 nsim_fib_ipv6_rules_res_occ_get(void *priv)
1430 struct nsim_fib_data *data = priv;
1432 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB_RULES, false);
1435 static u64 nsim_fib_nexthops_res_occ_get(void *priv)
1437 struct nsim_fib_data *data = priv;
1439 return nsim_fib_get_val(data, NSIM_RESOURCE_NEXTHOPS, false);
1442 static void nsim_fib_set_max_all(struct nsim_fib_data *data,
1443 struct devlink *devlink)
1445 static const enum nsim_resource_id res_ids[] = {
1446 NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES,
1447 NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES,
1448 NSIM_RESOURCE_NEXTHOPS,
1452 for (i = 0; i < ARRAY_SIZE(res_ids); i++) {
1456 err = devlink_resource_size_get(devlink, res_ids[i], &val);
1459 nsim_fib_set_max(data, res_ids[i], val);
1463 static void nsim_fib_event_work(struct work_struct *work)
1465 struct nsim_fib_data *data = container_of(work, struct nsim_fib_data,
1467 struct nsim_fib_event *fib_event, *next_fib_event;
1469 LIST_HEAD(fib_event_queue);
1471 spin_lock_bh(&data->fib_event_queue_lock);
1472 list_splice_init(&data->fib_event_queue, &fib_event_queue);
1473 spin_unlock_bh(&data->fib_event_queue_lock);
1475 mutex_lock(&data->fib_lock);
1476 list_for_each_entry_safe(fib_event, next_fib_event, &fib_event_queue,
1478 nsim_fib_event(fib_event);
1479 list_del(&fib_event->list);
1483 mutex_unlock(&data->fib_lock);
1487 nsim_fib_debugfs_init(struct nsim_fib_data *data, struct nsim_dev *nsim_dev)
1489 data->ddir = debugfs_create_dir("fib", nsim_dev->ddir);
1490 if (IS_ERR(data->ddir))
1491 return PTR_ERR(data->ddir);
1493 data->fail_route_offload = false;
1494 debugfs_create_bool("fail_route_offload", 0600, data->ddir,
1495 &data->fail_route_offload);
1497 data->fail_res_nexthop_group_replace = false;
1498 debugfs_create_bool("fail_res_nexthop_group_replace", 0600, data->ddir,
1499 &data->fail_res_nexthop_group_replace);
1501 data->fail_nexthop_bucket_replace = false;
1502 debugfs_create_bool("fail_nexthop_bucket_replace", 0600, data->ddir,
1503 &data->fail_nexthop_bucket_replace);
1505 debugfs_create_file("nexthop_bucket_activity", 0200, data->ddir,
1506 data, &nsim_nexthop_bucket_activity_fops);
1510 static void nsim_fib_debugfs_exit(struct nsim_fib_data *data)
1512 debugfs_remove_recursive(data->ddir);
1515 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
1516 struct netlink_ext_ack *extack)
1518 struct nsim_fib_data *data;
1519 struct nsim_dev *nsim_dev;
1522 data = kzalloc(sizeof(*data), GFP_KERNEL);
1524 return ERR_PTR(-ENOMEM);
1525 data->devlink = devlink;
1527 nsim_dev = devlink_priv(devlink);
1528 err = nsim_fib_debugfs_init(data, nsim_dev);
1532 mutex_init(&data->nh_lock);
1533 err = rhashtable_init(&data->nexthop_ht, &nsim_nexthop_ht_params);
1535 goto err_debugfs_exit;
1537 mutex_init(&data->fib_lock);
1538 INIT_LIST_HEAD(&data->fib_rt_list);
1539 err = rhashtable_init(&data->fib_rt_ht, &nsim_fib_rt_ht_params);
1541 goto err_rhashtable_nexthop_destroy;
1543 INIT_WORK(&data->fib_event_work, nsim_fib_event_work);
1544 INIT_LIST_HEAD(&data->fib_event_queue);
1545 spin_lock_init(&data->fib_event_queue_lock);
1547 nsim_fib_set_max_all(data, devlink);
1549 data->nexthop_nb.notifier_call = nsim_nexthop_event_nb;
1550 err = register_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb,
1553 pr_err("Failed to register nexthop notifier\n");
1554 goto err_rhashtable_fib_destroy;
1557 data->fib_nb.notifier_call = nsim_fib_event_nb;
1558 err = register_fib_notifier(devlink_net(devlink), &data->fib_nb,
1559 nsim_fib_dump_inconsistent, extack);
1561 pr_err("Failed to register fib notifier\n");
1562 goto err_nexthop_nb_unregister;
1565 devlink_resource_occ_get_register(devlink,
1566 NSIM_RESOURCE_IPV4_FIB,
1567 nsim_fib_ipv4_resource_occ_get,
1569 devlink_resource_occ_get_register(devlink,
1570 NSIM_RESOURCE_IPV4_FIB_RULES,
1571 nsim_fib_ipv4_rules_res_occ_get,
1573 devlink_resource_occ_get_register(devlink,
1574 NSIM_RESOURCE_IPV6_FIB,
1575 nsim_fib_ipv6_resource_occ_get,
1577 devlink_resource_occ_get_register(devlink,
1578 NSIM_RESOURCE_IPV6_FIB_RULES,
1579 nsim_fib_ipv6_rules_res_occ_get,
1581 devlink_resource_occ_get_register(devlink,
1582 NSIM_RESOURCE_NEXTHOPS,
1583 nsim_fib_nexthops_res_occ_get,
1587 err_nexthop_nb_unregister:
1588 unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb);
1589 err_rhashtable_fib_destroy:
1590 flush_work(&data->fib_event_work);
1591 rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free,
1593 err_rhashtable_nexthop_destroy:
1594 rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
1596 mutex_destroy(&data->fib_lock);
1598 mutex_destroy(&data->nh_lock);
1599 nsim_fib_debugfs_exit(data);
1602 return ERR_PTR(err);
1605 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data)
1607 devlink_resource_occ_get_unregister(devlink,
1608 NSIM_RESOURCE_NEXTHOPS);
1609 devlink_resource_occ_get_unregister(devlink,
1610 NSIM_RESOURCE_IPV6_FIB_RULES);
1611 devlink_resource_occ_get_unregister(devlink,
1612 NSIM_RESOURCE_IPV6_FIB);
1613 devlink_resource_occ_get_unregister(devlink,
1614 NSIM_RESOURCE_IPV4_FIB_RULES);
1615 devlink_resource_occ_get_unregister(devlink,
1616 NSIM_RESOURCE_IPV4_FIB);
1617 unregister_fib_notifier(devlink_net(devlink), &data->fib_nb);
1618 unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb);
1619 flush_work(&data->fib_event_work);
1620 rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free,
1622 rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
1624 WARN_ON_ONCE(!list_empty(&data->fib_event_queue));
1625 WARN_ON_ONCE(!list_empty(&data->fib_rt_list));
1626 mutex_destroy(&data->fib_lock);
1627 mutex_destroy(&data->nh_lock);
1628 nsim_fib_debugfs_exit(data);