2 * Copyright (c) 2018 Cumulus Networks. All rights reserved.
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 work_struct fib_flush_work;
58 struct list_head fib_event_queue;
59 spinlock_t fib_event_queue_lock; /* Protects fib event queue list */
60 struct mutex nh_lock; /* Protects NH HT */
62 bool fail_route_offload;
63 bool fail_res_nexthop_group_replace;
64 bool fail_nexthop_bucket_replace;
65 bool fail_route_delete;
68 struct nsim_fib_rt_key {
69 unsigned char addr[sizeof(struct in6_addr)];
70 unsigned char prefix_len;
76 struct nsim_fib_rt_key key;
77 struct rhash_head ht_node;
78 struct list_head list; /* Member of fib_rt_list */
82 struct nsim_fib_rt common;
89 struct nsim_fib_rt common;
90 struct list_head nh_list;
94 struct nsim_fib6_rt_nh {
95 struct list_head list; /* Member of nh_list */
99 struct nsim_fib6_event {
100 struct fib6_info **rt_arr;
104 struct nsim_fib_event {
105 struct list_head list; /* node in fib queue */
107 struct fib_entry_notifier_info fen_info;
108 struct nsim_fib6_event fib6_event;
110 struct nsim_fib_data *data;
115 static const struct rhashtable_params nsim_fib_rt_ht_params = {
116 .key_offset = offsetof(struct nsim_fib_rt, key),
117 .head_offset = offsetof(struct nsim_fib_rt, ht_node),
118 .key_len = sizeof(struct nsim_fib_rt_key),
119 .automatic_shrinking = true,
122 struct nsim_nexthop {
123 struct rhash_head ht_node;
129 static const struct rhashtable_params nsim_nexthop_ht_params = {
130 .key_offset = offsetof(struct nsim_nexthop, id),
131 .head_offset = offsetof(struct nsim_nexthop, ht_node),
132 .key_len = sizeof(u32),
133 .automatic_shrinking = true,
136 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
137 enum nsim_resource_id res_id, bool max)
139 struct nsim_fib_entry *entry;
142 case NSIM_RESOURCE_IPV4_FIB:
143 entry = &fib_data->ipv4.fib;
145 case NSIM_RESOURCE_IPV4_FIB_RULES:
146 entry = &fib_data->ipv4.rules;
148 case NSIM_RESOURCE_IPV6_FIB:
149 entry = &fib_data->ipv6.fib;
151 case NSIM_RESOURCE_IPV6_FIB_RULES:
152 entry = &fib_data->ipv6.rules;
154 case NSIM_RESOURCE_NEXTHOPS:
155 entry = &fib_data->nexthops;
161 return max ? entry->max : atomic64_read(&entry->num);
164 static void nsim_fib_set_max(struct nsim_fib_data *fib_data,
165 enum nsim_resource_id res_id, u64 val)
167 struct nsim_fib_entry *entry;
170 case NSIM_RESOURCE_IPV4_FIB:
171 entry = &fib_data->ipv4.fib;
173 case NSIM_RESOURCE_IPV4_FIB_RULES:
174 entry = &fib_data->ipv4.rules;
176 case NSIM_RESOURCE_IPV6_FIB:
177 entry = &fib_data->ipv6.fib;
179 case NSIM_RESOURCE_IPV6_FIB_RULES:
180 entry = &fib_data->ipv6.rules;
182 case NSIM_RESOURCE_NEXTHOPS:
183 entry = &fib_data->nexthops;
192 static int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add,
193 struct netlink_ext_ack *extack)
198 if (!atomic64_add_unless(&entry->num, 1, entry->max)) {
200 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib rule entries");
203 atomic64_dec_if_positive(&entry->num);
209 static int nsim_fib_rule_event(struct nsim_fib_data *data,
210 struct fib_notifier_info *info, bool add)
212 struct netlink_ext_ack *extack = info->extack;
215 switch (info->family) {
217 err = nsim_fib_rule_account(&data->ipv4.rules, add, extack);
220 err = nsim_fib_rule_account(&data->ipv6.rules, add, extack);
227 static int nsim_fib_account(struct nsim_fib_entry *entry, bool add)
232 if (!atomic64_add_unless(&entry->num, 1, entry->max))
235 atomic64_dec_if_positive(&entry->num);
241 static void nsim_fib_rt_init(struct nsim_fib_data *data,
242 struct nsim_fib_rt *fib_rt, const void *addr,
243 size_t addr_len, unsigned int prefix_len,
244 int family, u32 tb_id)
246 memcpy(fib_rt->key.addr, addr, addr_len);
247 fib_rt->key.prefix_len = prefix_len;
248 fib_rt->key.family = family;
249 fib_rt->key.tb_id = tb_id;
250 list_add(&fib_rt->list, &data->fib_rt_list);
253 static void nsim_fib_rt_fini(struct nsim_fib_rt *fib_rt)
255 list_del(&fib_rt->list);
258 static struct nsim_fib_rt *nsim_fib_rt_lookup(struct rhashtable *fib_rt_ht,
259 const void *addr, size_t addr_len,
260 unsigned int prefix_len,
261 int family, u32 tb_id)
263 struct nsim_fib_rt_key key;
265 memset(&key, 0, sizeof(key));
266 memcpy(key.addr, addr, addr_len);
267 key.prefix_len = prefix_len;
271 return rhashtable_lookup_fast(fib_rt_ht, &key, nsim_fib_rt_ht_params);
274 static struct nsim_fib4_rt *
275 nsim_fib4_rt_create(struct nsim_fib_data *data,
276 struct fib_entry_notifier_info *fen_info)
278 struct nsim_fib4_rt *fib4_rt;
280 fib4_rt = kzalloc(sizeof(*fib4_rt), GFP_KERNEL);
284 nsim_fib_rt_init(data, &fib4_rt->common, &fen_info->dst, sizeof(u32),
285 fen_info->dst_len, AF_INET, fen_info->tb_id);
287 fib4_rt->fi = fen_info->fi;
288 fib_info_hold(fib4_rt->fi);
289 fib4_rt->dscp = fen_info->dscp;
290 fib4_rt->type = fen_info->type;
295 static void nsim_fib4_rt_destroy(struct nsim_fib4_rt *fib4_rt)
297 fib_info_put(fib4_rt->fi);
298 nsim_fib_rt_fini(&fib4_rt->common);
302 static struct nsim_fib4_rt *
303 nsim_fib4_rt_lookup(struct rhashtable *fib_rt_ht,
304 const struct fib_entry_notifier_info *fen_info)
306 struct nsim_fib_rt *fib_rt;
308 fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &fen_info->dst, sizeof(u32),
309 fen_info->dst_len, AF_INET,
314 return container_of(fib_rt, struct nsim_fib4_rt, common);
318 nsim_fib4_rt_offload_failed_flag_set(struct net *net,
319 struct fib_entry_notifier_info *fen_info)
321 u32 *p_dst = (u32 *)&fen_info->dst;
322 struct fib_rt_info fri;
324 fri.fi = fen_info->fi;
325 fri.tb_id = fen_info->tb_id;
326 fri.dst = cpu_to_be32(*p_dst);
327 fri.dst_len = fen_info->dst_len;
328 fri.dscp = fen_info->dscp;
329 fri.type = fen_info->type;
332 fri.offload_failed = true;
333 fib_alias_hw_flags_set(net, &fri);
336 static void nsim_fib4_rt_hw_flags_set(struct net *net,
337 const struct nsim_fib4_rt *fib4_rt,
340 u32 *p_dst = (u32 *) fib4_rt->common.key.addr;
341 int dst_len = fib4_rt->common.key.prefix_len;
342 struct fib_rt_info fri;
344 fri.fi = fib4_rt->fi;
345 fri.tb_id = fib4_rt->common.key.tb_id;
346 fri.dst = cpu_to_be32(*p_dst);
347 fri.dst_len = dst_len;
348 fri.dscp = fib4_rt->dscp;
349 fri.type = fib4_rt->type;
352 fri.offload_failed = false;
353 fib_alias_hw_flags_set(net, &fri);
356 static int nsim_fib4_rt_add(struct nsim_fib_data *data,
357 struct nsim_fib4_rt *fib4_rt)
359 struct net *net = devlink_net(data->devlink);
362 err = rhashtable_insert_fast(&data->fib_rt_ht,
363 &fib4_rt->common.ht_node,
364 nsim_fib_rt_ht_params);
366 goto err_fib_dismiss;
368 /* Simulate hardware programming latency. */
370 nsim_fib4_rt_hw_flags_set(net, fib4_rt, true);
375 /* Drop the accounting that was increased from the notification
376 * context when FIB_EVENT_ENTRY_REPLACE was triggered.
378 nsim_fib_account(&data->ipv4.fib, false);
382 static int nsim_fib4_rt_replace(struct nsim_fib_data *data,
383 struct nsim_fib4_rt *fib4_rt,
384 struct nsim_fib4_rt *fib4_rt_old)
386 struct net *net = devlink_net(data->devlink);
389 /* We are replacing a route, so need to remove the accounting which
390 * was increased when FIB_EVENT_ENTRY_REPLACE was triggered.
392 err = nsim_fib_account(&data->ipv4.fib, false);
395 err = rhashtable_replace_fast(&data->fib_rt_ht,
396 &fib4_rt_old->common.ht_node,
397 &fib4_rt->common.ht_node,
398 nsim_fib_rt_ht_params);
403 nsim_fib4_rt_hw_flags_set(net, fib4_rt, true);
405 nsim_fib4_rt_hw_flags_set(net, fib4_rt_old, false);
406 nsim_fib4_rt_destroy(fib4_rt_old);
411 static int nsim_fib4_rt_insert(struct nsim_fib_data *data,
412 struct fib_entry_notifier_info *fen_info)
414 struct nsim_fib4_rt *fib4_rt, *fib4_rt_old;
417 if (data->fail_route_offload) {
418 /* For testing purposes, user set debugfs fail_route_offload
419 * value to true. Simulate hardware programming latency and then
426 fib4_rt = nsim_fib4_rt_create(data, fen_info);
430 fib4_rt_old = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info);
432 err = nsim_fib4_rt_add(data, fib4_rt);
434 err = nsim_fib4_rt_replace(data, fib4_rt, fib4_rt_old);
437 nsim_fib4_rt_destroy(fib4_rt);
442 static void nsim_fib4_rt_remove(struct nsim_fib_data *data,
443 const struct fib_entry_notifier_info *fen_info)
445 struct nsim_fib4_rt *fib4_rt;
447 fib4_rt = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info);
451 rhashtable_remove_fast(&data->fib_rt_ht, &fib4_rt->common.ht_node,
452 nsim_fib_rt_ht_params);
453 nsim_fib4_rt_destroy(fib4_rt);
456 static int nsim_fib4_event(struct nsim_fib_data *data,
457 struct fib_entry_notifier_info *fen_info,
463 case FIB_EVENT_ENTRY_REPLACE:
464 err = nsim_fib4_rt_insert(data, fen_info);
466 struct net *net = devlink_net(data->devlink);
468 nsim_fib4_rt_offload_failed_flag_set(net, fen_info);
471 case FIB_EVENT_ENTRY_DEL:
472 nsim_fib4_rt_remove(data, fen_info);
481 static struct nsim_fib6_rt_nh *
482 nsim_fib6_rt_nh_find(const struct nsim_fib6_rt *fib6_rt,
483 const struct fib6_info *rt)
485 struct nsim_fib6_rt_nh *fib6_rt_nh;
487 list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list) {
488 if (fib6_rt_nh->rt == rt)
495 static int nsim_fib6_rt_nh_add(struct nsim_fib6_rt *fib6_rt,
496 struct fib6_info *rt)
498 struct nsim_fib6_rt_nh *fib6_rt_nh;
500 fib6_rt_nh = kzalloc(sizeof(*fib6_rt_nh), GFP_KERNEL);
506 list_add_tail(&fib6_rt_nh->list, &fib6_rt->nh_list);
512 #if IS_ENABLED(CONFIG_IPV6)
513 static void nsim_rt6_release(struct fib6_info *rt)
515 fib6_info_release(rt);
518 static void nsim_rt6_release(struct fib6_info *rt)
523 static void nsim_fib6_rt_nh_del(struct nsim_fib6_rt *fib6_rt,
524 const struct fib6_info *rt)
526 struct nsim_fib6_rt_nh *fib6_rt_nh;
528 fib6_rt_nh = nsim_fib6_rt_nh_find(fib6_rt, rt);
533 list_del(&fib6_rt_nh->list);
534 nsim_rt6_release(fib6_rt_nh->rt);
538 static struct nsim_fib6_rt *
539 nsim_fib6_rt_create(struct nsim_fib_data *data,
540 struct fib6_info **rt_arr, unsigned int nrt6)
542 struct fib6_info *rt = rt_arr[0];
543 struct nsim_fib6_rt *fib6_rt;
547 fib6_rt = kzalloc(sizeof(*fib6_rt), GFP_KERNEL);
549 return ERR_PTR(-ENOMEM);
551 nsim_fib_rt_init(data, &fib6_rt->common, &rt->fib6_dst.addr,
552 sizeof(rt->fib6_dst.addr), rt->fib6_dst.plen, AF_INET6,
553 rt->fib6_table->tb6_id);
555 /* We consider a multipath IPv6 route as one entry, but it can be made
556 * up from several fib6_info structs (one for each nexthop), so we
557 * add them all to the same list under the entry.
559 INIT_LIST_HEAD(&fib6_rt->nh_list);
561 for (i = 0; i < nrt6; i++) {
562 err = nsim_fib6_rt_nh_add(fib6_rt, rt_arr[i]);
564 goto err_fib6_rt_nh_del;
570 for (i--; i >= 0; i--) {
571 nsim_fib6_rt_nh_del(fib6_rt, rt_arr[i]);
573 nsim_fib_rt_fini(&fib6_rt->common);
578 static void nsim_fib6_rt_destroy(struct nsim_fib6_rt *fib6_rt)
580 struct nsim_fib6_rt_nh *iter, *tmp;
582 list_for_each_entry_safe(iter, tmp, &fib6_rt->nh_list, list)
583 nsim_fib6_rt_nh_del(fib6_rt, iter->rt);
584 WARN_ON_ONCE(!list_empty(&fib6_rt->nh_list));
585 nsim_fib_rt_fini(&fib6_rt->common);
589 static struct nsim_fib6_rt *
590 nsim_fib6_rt_lookup(struct rhashtable *fib_rt_ht, const struct fib6_info *rt)
592 struct nsim_fib_rt *fib_rt;
594 fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &rt->fib6_dst.addr,
595 sizeof(rt->fib6_dst.addr),
596 rt->fib6_dst.plen, AF_INET6,
597 rt->fib6_table->tb6_id);
601 return container_of(fib_rt, struct nsim_fib6_rt, common);
604 static int nsim_fib6_rt_append(struct nsim_fib_data *data,
605 struct nsim_fib6_event *fib6_event)
607 struct fib6_info *rt = fib6_event->rt_arr[0];
608 struct nsim_fib6_rt *fib6_rt;
611 if (data->fail_route_offload) {
612 /* For testing purposes, user set debugfs fail_route_offload
613 * value to true. Simulate hardware programming latency and then
620 fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
624 for (i = 0; i < fib6_event->nrt6; i++) {
625 err = nsim_fib6_rt_nh_add(fib6_rt, fib6_event->rt_arr[i]);
627 goto err_fib6_rt_nh_del;
629 WRITE_ONCE(fib6_event->rt_arr[i]->trap, true);
635 for (i--; i >= 0; i--) {
636 WRITE_ONCE(fib6_event->rt_arr[i]->trap, false);
637 nsim_fib6_rt_nh_del(fib6_rt, fib6_event->rt_arr[i]);
642 #if IS_ENABLED(CONFIG_IPV6)
643 static void nsim_fib6_rt_offload_failed_flag_set(struct nsim_fib_data *data,
644 struct fib6_info **rt_arr,
648 struct net *net = devlink_net(data->devlink);
651 for (i = 0; i < nrt6; i++)
652 fib6_info_hw_flags_set(net, rt_arr[i], false, false, true);
655 static void nsim_fib6_rt_offload_failed_flag_set(struct nsim_fib_data *data,
656 struct fib6_info **rt_arr,
662 #if IS_ENABLED(CONFIG_IPV6)
663 static void nsim_fib6_rt_hw_flags_set(struct nsim_fib_data *data,
664 const struct nsim_fib6_rt *fib6_rt,
667 struct net *net = devlink_net(data->devlink);
668 struct nsim_fib6_rt_nh *fib6_rt_nh;
670 list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list)
671 fib6_info_hw_flags_set(net, fib6_rt_nh->rt, false, trap, false);
674 static void nsim_fib6_rt_hw_flags_set(struct nsim_fib_data *data,
675 const struct nsim_fib6_rt *fib6_rt,
681 static int nsim_fib6_rt_add(struct nsim_fib_data *data,
682 struct nsim_fib6_rt *fib6_rt)
686 err = rhashtable_insert_fast(&data->fib_rt_ht,
687 &fib6_rt->common.ht_node,
688 nsim_fib_rt_ht_params);
691 goto err_fib_dismiss;
694 nsim_fib6_rt_hw_flags_set(data, fib6_rt, true);
699 /* Drop the accounting that was increased from the notification
700 * context when FIB_EVENT_ENTRY_REPLACE was triggered.
702 nsim_fib_account(&data->ipv6.fib, false);
706 static int nsim_fib6_rt_replace(struct nsim_fib_data *data,
707 struct nsim_fib6_rt *fib6_rt,
708 struct nsim_fib6_rt *fib6_rt_old)
712 /* We are replacing a route, so need to remove the accounting which
713 * was increased when FIB_EVENT_ENTRY_REPLACE was triggered.
715 err = nsim_fib_account(&data->ipv6.fib, false);
719 err = rhashtable_replace_fast(&data->fib_rt_ht,
720 &fib6_rt_old->common.ht_node,
721 &fib6_rt->common.ht_node,
722 nsim_fib_rt_ht_params);
728 nsim_fib6_rt_hw_flags_set(data, fib6_rt, true);
730 nsim_fib6_rt_hw_flags_set(data, fib6_rt_old, false);
731 nsim_fib6_rt_destroy(fib6_rt_old);
736 static int nsim_fib6_rt_insert(struct nsim_fib_data *data,
737 struct nsim_fib6_event *fib6_event)
739 struct fib6_info *rt = fib6_event->rt_arr[0];
740 struct nsim_fib6_rt *fib6_rt, *fib6_rt_old;
743 if (data->fail_route_offload) {
744 /* For testing purposes, user set debugfs fail_route_offload
745 * value to true. Simulate hardware programming latency and then
752 fib6_rt = nsim_fib6_rt_create(data, fib6_event->rt_arr,
755 return PTR_ERR(fib6_rt);
757 fib6_rt_old = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
759 err = nsim_fib6_rt_add(data, fib6_rt);
761 err = nsim_fib6_rt_replace(data, fib6_rt, fib6_rt_old);
764 nsim_fib6_rt_destroy(fib6_rt);
769 static void nsim_fib6_rt_remove(struct nsim_fib_data *data,
770 struct nsim_fib6_event *fib6_event)
772 struct fib6_info *rt = fib6_event->rt_arr[0];
773 struct nsim_fib6_rt *fib6_rt;
776 /* Multipath routes are first added to the FIB trie and only then
777 * notified. If we vetoed the addition, we will get a delete
778 * notification for a route we do not have. Therefore, do not warn if
779 * route was not found.
781 fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
785 /* If not all the nexthops are deleted, then only reduce the nexthop
788 if (fib6_event->nrt6 != fib6_rt->nhs) {
789 for (i = 0; i < fib6_event->nrt6; i++)
790 nsim_fib6_rt_nh_del(fib6_rt, fib6_event->rt_arr[i]);
794 rhashtable_remove_fast(&data->fib_rt_ht, &fib6_rt->common.ht_node,
795 nsim_fib_rt_ht_params);
796 nsim_fib6_rt_destroy(fib6_rt);
799 static int nsim_fib6_event_init(struct nsim_fib6_event *fib6_event,
800 struct fib6_entry_notifier_info *fen6_info)
802 struct fib6_info *rt = fen6_info->rt;
803 struct fib6_info **rt_arr;
804 struct fib6_info *iter;
808 nrt6 = fen6_info->nsiblings + 1;
810 rt_arr = kcalloc(nrt6, sizeof(struct fib6_info *), GFP_ATOMIC);
814 fib6_event->rt_arr = rt_arr;
815 fib6_event->nrt6 = nrt6;
820 if (!fen6_info->nsiblings)
823 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
824 if (i == fen6_info->nsiblings)
827 rt_arr[i + 1] = iter;
828 fib6_info_hold(iter);
831 WARN_ON_ONCE(i != fen6_info->nsiblings);
836 static void nsim_fib6_event_fini(struct nsim_fib6_event *fib6_event)
840 for (i = 0; i < fib6_event->nrt6; i++)
841 nsim_rt6_release(fib6_event->rt_arr[i]);
842 kfree(fib6_event->rt_arr);
845 static int nsim_fib6_event(struct nsim_fib_data *data,
846 struct nsim_fib6_event *fib6_event,
851 if (fib6_event->rt_arr[0]->fib6_src.plen)
855 case FIB_EVENT_ENTRY_REPLACE:
856 err = nsim_fib6_rt_insert(data, fib6_event);
858 goto err_rt_offload_failed_flag_set;
860 case FIB_EVENT_ENTRY_APPEND:
861 err = nsim_fib6_rt_append(data, fib6_event);
863 goto err_rt_offload_failed_flag_set;
865 case FIB_EVENT_ENTRY_DEL:
866 nsim_fib6_rt_remove(data, fib6_event);
874 err_rt_offload_failed_flag_set:
875 nsim_fib6_rt_offload_failed_flag_set(data, fib6_event->rt_arr,
880 static void nsim_fib_event(struct nsim_fib_event *fib_event)
882 switch (fib_event->family) {
884 nsim_fib4_event(fib_event->data, &fib_event->fen_info,
886 fib_info_put(fib_event->fen_info.fi);
889 nsim_fib6_event(fib_event->data, &fib_event->fib6_event,
891 nsim_fib6_event_fini(&fib_event->fib6_event);
896 static int nsim_fib4_prepare_event(struct fib_notifier_info *info,
897 struct nsim_fib_event *fib_event,
900 struct nsim_fib_data *data = fib_event->data;
901 struct fib_entry_notifier_info *fen_info;
902 struct netlink_ext_ack *extack;
905 fen_info = container_of(info, struct fib_entry_notifier_info,
907 fib_event->fen_info = *fen_info;
908 extack = info->extack;
911 case FIB_EVENT_ENTRY_REPLACE:
912 err = nsim_fib_account(&data->ipv4.fib, true);
914 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
918 case FIB_EVENT_ENTRY_DEL:
919 if (data->fail_route_delete) {
920 NL_SET_ERR_MSG_MOD(extack, "Failed to process route deletion");
923 nsim_fib_account(&data->ipv4.fib, false);
927 /* Take reference on fib_info to prevent it from being
928 * freed while event is queued. Release it afterwards.
930 fib_info_hold(fib_event->fen_info.fi);
935 static int nsim_fib6_prepare_event(struct fib_notifier_info *info,
936 struct nsim_fib_event *fib_event,
939 struct nsim_fib_data *data = fib_event->data;
940 struct fib6_entry_notifier_info *fen6_info;
941 struct netlink_ext_ack *extack;
944 fen6_info = container_of(info, struct fib6_entry_notifier_info,
947 err = nsim_fib6_event_init(&fib_event->fib6_event, fen6_info);
951 extack = info->extack;
953 case FIB_EVENT_ENTRY_REPLACE:
954 err = nsim_fib_account(&data->ipv6.fib, true);
956 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
957 goto err_fib6_event_fini;
960 case FIB_EVENT_ENTRY_DEL:
961 if (data->fail_route_delete) {
963 NL_SET_ERR_MSG_MOD(extack, "Failed to process route deletion");
964 goto err_fib6_event_fini;
966 nsim_fib_account(&data->ipv6.fib, false);
973 nsim_fib6_event_fini(&fib_event->fib6_event);
977 static int nsim_fib_event_schedule_work(struct nsim_fib_data *data,
978 struct fib_notifier_info *info,
981 struct nsim_fib_event *fib_event;
984 if (info->family != AF_INET && info->family != AF_INET6)
985 /* netdevsim does not support 'RTNL_FAMILY_IP6MR' and
986 * 'RTNL_FAMILY_IPMR' and should ignore them.
990 fib_event = kzalloc(sizeof(*fib_event), GFP_ATOMIC);
992 goto err_fib_event_alloc;
994 fib_event->data = data;
995 fib_event->event = event;
996 fib_event->family = info->family;
998 switch (info->family) {
1000 err = nsim_fib4_prepare_event(info, fib_event, event);
1003 err = nsim_fib6_prepare_event(info, fib_event, event);
1008 goto err_fib_prepare_event;
1010 /* Enqueue the event and trigger the work */
1011 spin_lock_bh(&data->fib_event_queue_lock);
1012 list_add_tail(&fib_event->list, &data->fib_event_queue);
1013 spin_unlock_bh(&data->fib_event_queue_lock);
1014 schedule_work(&data->fib_event_work);
1018 err_fib_prepare_event:
1020 err_fib_event_alloc:
1021 if (event == FIB_EVENT_ENTRY_DEL)
1022 schedule_work(&data->fib_flush_work);
1026 static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event,
1029 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1031 struct fib_notifier_info *info = ptr;
1035 case FIB_EVENT_RULE_ADD:
1036 case FIB_EVENT_RULE_DEL:
1037 err = nsim_fib_rule_event(data, info,
1038 event == FIB_EVENT_RULE_ADD);
1039 return notifier_from_errno(err);
1040 case FIB_EVENT_ENTRY_REPLACE:
1041 case FIB_EVENT_ENTRY_APPEND:
1042 case FIB_EVENT_ENTRY_DEL:
1043 return nsim_fib_event_schedule_work(data, info, event);
1049 static void nsim_fib4_rt_free(struct nsim_fib_rt *fib_rt,
1050 struct nsim_fib_data *data)
1052 struct devlink *devlink = data->devlink;
1053 struct nsim_fib4_rt *fib4_rt;
1055 fib4_rt = container_of(fib_rt, struct nsim_fib4_rt, common);
1056 nsim_fib4_rt_hw_flags_set(devlink_net(devlink), fib4_rt, false);
1057 nsim_fib_account(&data->ipv4.fib, false);
1058 nsim_fib4_rt_destroy(fib4_rt);
1061 static void nsim_fib6_rt_free(struct nsim_fib_rt *fib_rt,
1062 struct nsim_fib_data *data)
1064 struct nsim_fib6_rt *fib6_rt;
1066 fib6_rt = container_of(fib_rt, struct nsim_fib6_rt, common);
1067 nsim_fib6_rt_hw_flags_set(data, fib6_rt, false);
1068 nsim_fib_account(&data->ipv6.fib, false);
1069 nsim_fib6_rt_destroy(fib6_rt);
1072 static void nsim_fib_rt_free(void *ptr, void *arg)
1074 struct nsim_fib_rt *fib_rt = ptr;
1075 struct nsim_fib_data *data = arg;
1077 switch (fib_rt->key.family) {
1079 nsim_fib4_rt_free(fib_rt, data);
1082 nsim_fib6_rt_free(fib_rt, data);
1089 /* inconsistent dump, trying again */
1090 static void nsim_fib_dump_inconsistent(struct notifier_block *nb)
1092 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1094 struct nsim_fib_rt *fib_rt, *fib_rt_tmp;
1096 /* Flush the work to make sure there is no race with notifications. */
1097 flush_work(&data->fib_event_work);
1099 /* The notifier block is still not registered, so we do not need to
1100 * take any locks here.
1102 list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) {
1103 rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node,
1104 nsim_fib_rt_ht_params);
1105 nsim_fib_rt_free(fib_rt, data);
1108 atomic64_set(&data->ipv4.rules.num, 0ULL);
1109 atomic64_set(&data->ipv6.rules.num, 0ULL);
1112 static struct nsim_nexthop *nsim_nexthop_create(struct nsim_fib_data *data,
1113 struct nh_notifier_info *info)
1115 struct nsim_nexthop *nexthop;
1119 nexthop = kzalloc(sizeof(*nexthop), GFP_KERNEL);
1121 return ERR_PTR(-ENOMEM);
1123 nexthop->id = info->id;
1125 /* Determine the number of nexthop entries the new nexthop will
1129 switch (info->type) {
1130 case NH_NOTIFIER_INFO_TYPE_SINGLE:
1133 case NH_NOTIFIER_INFO_TYPE_GRP:
1134 for (i = 0; i < info->nh_grp->num_nh; i++)
1135 occ += info->nh_grp->nh_entries[i].weight;
1137 case NH_NOTIFIER_INFO_TYPE_RES_TABLE:
1138 occ = info->nh_res_table->num_nh_buckets;
1139 nexthop->is_resilient = true;
1142 NL_SET_ERR_MSG_MOD(info->extack, "Unsupported nexthop type");
1144 return ERR_PTR(-EOPNOTSUPP);
1151 static void nsim_nexthop_destroy(struct nsim_nexthop *nexthop)
1156 static int nsim_nexthop_account(struct nsim_fib_data *data, u64 occ,
1157 bool add, struct netlink_ext_ack *extack)
1162 for (i = 0; i < occ; i++)
1163 if (!atomic64_add_unless(&data->nexthops.num, 1,
1164 data->nexthops.max)) {
1166 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported nexthops");
1167 goto err_num_decrease;
1170 if (WARN_ON(occ > atomic64_read(&data->nexthops.num)))
1172 atomic64_sub(occ, &data->nexthops.num);
1178 atomic64_sub(i, &data->nexthops.num);
1183 static void nsim_nexthop_hw_flags_set(struct net *net,
1184 const struct nsim_nexthop *nexthop,
1189 nexthop_set_hw_flags(net, nexthop->id, false, trap);
1191 if (!nexthop->is_resilient)
1194 for (i = 0; i < nexthop->occ; i++)
1195 nexthop_bucket_set_hw_flags(net, nexthop->id, i, false, trap);
1198 static int nsim_nexthop_add(struct nsim_fib_data *data,
1199 struct nsim_nexthop *nexthop,
1200 struct netlink_ext_ack *extack)
1202 struct net *net = devlink_net(data->devlink);
1205 err = nsim_nexthop_account(data, nexthop->occ, true, extack);
1209 err = rhashtable_insert_fast(&data->nexthop_ht, &nexthop->ht_node,
1210 nsim_nexthop_ht_params);
1212 NL_SET_ERR_MSG_MOD(extack, "Failed to insert nexthop");
1213 goto err_nexthop_dismiss;
1216 nsim_nexthop_hw_flags_set(net, nexthop, true);
1220 err_nexthop_dismiss:
1221 nsim_nexthop_account(data, nexthop->occ, false, extack);
1225 static int nsim_nexthop_replace(struct nsim_fib_data *data,
1226 struct nsim_nexthop *nexthop,
1227 struct nsim_nexthop *nexthop_old,
1228 struct netlink_ext_ack *extack)
1230 struct net *net = devlink_net(data->devlink);
1233 err = nsim_nexthop_account(data, nexthop->occ, true, extack);
1237 err = rhashtable_replace_fast(&data->nexthop_ht,
1238 &nexthop_old->ht_node, &nexthop->ht_node,
1239 nsim_nexthop_ht_params);
1241 NL_SET_ERR_MSG_MOD(extack, "Failed to replace nexthop");
1242 goto err_nexthop_dismiss;
1245 nsim_nexthop_hw_flags_set(net, nexthop, true);
1246 nsim_nexthop_account(data, nexthop_old->occ, false, extack);
1247 nsim_nexthop_destroy(nexthop_old);
1251 err_nexthop_dismiss:
1252 nsim_nexthop_account(data, nexthop->occ, false, extack);
1256 static int nsim_nexthop_insert(struct nsim_fib_data *data,
1257 struct nh_notifier_info *info)
1259 struct nsim_nexthop *nexthop, *nexthop_old;
1262 nexthop = nsim_nexthop_create(data, info);
1263 if (IS_ERR(nexthop))
1264 return PTR_ERR(nexthop);
1266 nexthop_old = rhashtable_lookup_fast(&data->nexthop_ht, &info->id,
1267 nsim_nexthop_ht_params);
1269 err = nsim_nexthop_add(data, nexthop, info->extack);
1271 err = nsim_nexthop_replace(data, nexthop, nexthop_old,
1275 nsim_nexthop_destroy(nexthop);
1280 static void nsim_nexthop_remove(struct nsim_fib_data *data,
1281 struct nh_notifier_info *info)
1283 struct nsim_nexthop *nexthop;
1285 nexthop = rhashtable_lookup_fast(&data->nexthop_ht, &info->id,
1286 nsim_nexthop_ht_params);
1290 rhashtable_remove_fast(&data->nexthop_ht, &nexthop->ht_node,
1291 nsim_nexthop_ht_params);
1292 nsim_nexthop_account(data, nexthop->occ, false, info->extack);
1293 nsim_nexthop_destroy(nexthop);
1296 static int nsim_nexthop_res_table_pre_replace(struct nsim_fib_data *data,
1297 struct nh_notifier_info *info)
1299 if (data->fail_res_nexthop_group_replace) {
1300 NL_SET_ERR_MSG_MOD(info->extack, "Failed to replace a resilient nexthop group");
1307 static int nsim_nexthop_bucket_replace(struct nsim_fib_data *data,
1308 struct nh_notifier_info *info)
1310 if (data->fail_nexthop_bucket_replace) {
1311 NL_SET_ERR_MSG_MOD(info->extack, "Failed to replace nexthop bucket");
1315 nexthop_bucket_set_hw_flags(info->net, info->id,
1316 info->nh_res_bucket->bucket_index,
1322 static int nsim_nexthop_event_nb(struct notifier_block *nb, unsigned long event,
1325 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1327 struct nh_notifier_info *info = ptr;
1330 mutex_lock(&data->nh_lock);
1332 case NEXTHOP_EVENT_REPLACE:
1333 err = nsim_nexthop_insert(data, info);
1335 case NEXTHOP_EVENT_DEL:
1336 nsim_nexthop_remove(data, info);
1338 case NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE:
1339 err = nsim_nexthop_res_table_pre_replace(data, info);
1341 case NEXTHOP_EVENT_BUCKET_REPLACE:
1342 err = nsim_nexthop_bucket_replace(data, info);
1348 mutex_unlock(&data->nh_lock);
1349 return notifier_from_errno(err);
1352 static void nsim_nexthop_free(void *ptr, void *arg)
1354 struct nsim_nexthop *nexthop = ptr;
1355 struct nsim_fib_data *data = arg;
1358 net = devlink_net(data->devlink);
1359 nsim_nexthop_hw_flags_set(net, nexthop, false);
1360 nsim_nexthop_account(data, nexthop->occ, false, NULL);
1361 nsim_nexthop_destroy(nexthop);
1364 static ssize_t nsim_nexthop_bucket_activity_write(struct file *file,
1365 const char __user *user_buf,
1366 size_t size, loff_t *ppos)
1368 struct nsim_fib_data *data = file->private_data;
1369 struct net *net = devlink_net(data->devlink);
1370 struct nsim_nexthop *nexthop;
1371 unsigned long *activity;
1380 if (size > sizeof(buf) - 1)
1382 if (copy_from_user(buf, user_buf, size))
1386 if (sscanf(buf, "%u %hu", &nhid, &bucket_index) != 2)
1391 nexthop = rhashtable_lookup_fast(&data->nexthop_ht, &nhid,
1392 nsim_nexthop_ht_params);
1393 if (!nexthop || !nexthop->is_resilient ||
1394 bucket_index >= nexthop->occ) {
1399 activity = bitmap_zalloc(nexthop->occ, GFP_KERNEL);
1405 bitmap_set(activity, bucket_index, 1);
1406 nexthop_res_grp_activity_update(net, nhid, nexthop->occ, activity);
1407 bitmap_free(activity);
1416 static const struct file_operations nsim_nexthop_bucket_activity_fops = {
1417 .open = simple_open,
1418 .write = nsim_nexthop_bucket_activity_write,
1419 .owner = THIS_MODULE,
1422 static u64 nsim_fib_ipv4_resource_occ_get(void *priv)
1424 struct nsim_fib_data *data = priv;
1426 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB, false);
1429 static u64 nsim_fib_ipv4_rules_res_occ_get(void *priv)
1431 struct nsim_fib_data *data = priv;
1433 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB_RULES, false);
1436 static u64 nsim_fib_ipv6_resource_occ_get(void *priv)
1438 struct nsim_fib_data *data = priv;
1440 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB, false);
1443 static u64 nsim_fib_ipv6_rules_res_occ_get(void *priv)
1445 struct nsim_fib_data *data = priv;
1447 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB_RULES, false);
1450 static u64 nsim_fib_nexthops_res_occ_get(void *priv)
1452 struct nsim_fib_data *data = priv;
1454 return nsim_fib_get_val(data, NSIM_RESOURCE_NEXTHOPS, false);
1457 static void nsim_fib_set_max_all(struct nsim_fib_data *data,
1458 struct devlink *devlink)
1460 static const enum nsim_resource_id res_ids[] = {
1461 NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES,
1462 NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES,
1463 NSIM_RESOURCE_NEXTHOPS,
1467 for (i = 0; i < ARRAY_SIZE(res_ids); i++) {
1471 err = devl_resource_size_get(devlink, res_ids[i], &val);
1474 nsim_fib_set_max(data, res_ids[i], val);
1478 static void nsim_fib_event_work(struct work_struct *work)
1480 struct nsim_fib_data *data = container_of(work, struct nsim_fib_data,
1482 struct nsim_fib_event *fib_event, *next_fib_event;
1484 LIST_HEAD(fib_event_queue);
1486 spin_lock_bh(&data->fib_event_queue_lock);
1487 list_splice_init(&data->fib_event_queue, &fib_event_queue);
1488 spin_unlock_bh(&data->fib_event_queue_lock);
1490 mutex_lock(&data->fib_lock);
1491 list_for_each_entry_safe(fib_event, next_fib_event, &fib_event_queue,
1493 nsim_fib_event(fib_event);
1494 list_del(&fib_event->list);
1498 mutex_unlock(&data->fib_lock);
1501 static void nsim_fib_flush_work(struct work_struct *work)
1503 struct nsim_fib_data *data = container_of(work, struct nsim_fib_data,
1505 struct nsim_fib_rt *fib_rt, *fib_rt_tmp;
1507 /* Process pending work. */
1508 flush_work(&data->fib_event_work);
1510 mutex_lock(&data->fib_lock);
1511 list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) {
1512 rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node,
1513 nsim_fib_rt_ht_params);
1514 nsim_fib_rt_free(fib_rt, data);
1516 mutex_unlock(&data->fib_lock);
1520 nsim_fib_debugfs_init(struct nsim_fib_data *data, struct nsim_dev *nsim_dev)
1522 data->ddir = debugfs_create_dir("fib", nsim_dev->ddir);
1523 if (IS_ERR(data->ddir))
1524 return PTR_ERR(data->ddir);
1526 data->fail_route_offload = false;
1527 debugfs_create_bool("fail_route_offload", 0600, data->ddir,
1528 &data->fail_route_offload);
1530 data->fail_res_nexthop_group_replace = false;
1531 debugfs_create_bool("fail_res_nexthop_group_replace", 0600, data->ddir,
1532 &data->fail_res_nexthop_group_replace);
1534 data->fail_nexthop_bucket_replace = false;
1535 debugfs_create_bool("fail_nexthop_bucket_replace", 0600, data->ddir,
1536 &data->fail_nexthop_bucket_replace);
1538 debugfs_create_file("nexthop_bucket_activity", 0200, data->ddir,
1539 data, &nsim_nexthop_bucket_activity_fops);
1541 data->fail_route_delete = false;
1542 debugfs_create_bool("fail_route_delete", 0600, data->ddir,
1543 &data->fail_route_delete);
1547 static void nsim_fib_debugfs_exit(struct nsim_fib_data *data)
1549 debugfs_remove_recursive(data->ddir);
1552 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
1553 struct netlink_ext_ack *extack)
1555 struct nsim_fib_data *data;
1556 struct nsim_dev *nsim_dev;
1559 data = kzalloc(sizeof(*data), GFP_KERNEL);
1561 return ERR_PTR(-ENOMEM);
1562 data->devlink = devlink;
1564 nsim_dev = devlink_priv(devlink);
1565 err = nsim_fib_debugfs_init(data, nsim_dev);
1569 mutex_init(&data->nh_lock);
1570 err = rhashtable_init(&data->nexthop_ht, &nsim_nexthop_ht_params);
1572 goto err_debugfs_exit;
1574 mutex_init(&data->fib_lock);
1575 INIT_LIST_HEAD(&data->fib_rt_list);
1576 err = rhashtable_init(&data->fib_rt_ht, &nsim_fib_rt_ht_params);
1578 goto err_rhashtable_nexthop_destroy;
1580 INIT_WORK(&data->fib_event_work, nsim_fib_event_work);
1581 INIT_WORK(&data->fib_flush_work, nsim_fib_flush_work);
1582 INIT_LIST_HEAD(&data->fib_event_queue);
1583 spin_lock_init(&data->fib_event_queue_lock);
1585 nsim_fib_set_max_all(data, devlink);
1587 data->nexthop_nb.notifier_call = nsim_nexthop_event_nb;
1588 err = register_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb,
1591 pr_err("Failed to register nexthop notifier\n");
1592 goto err_rhashtable_fib_destroy;
1595 data->fib_nb.notifier_call = nsim_fib_event_nb;
1596 err = register_fib_notifier(devlink_net(devlink), &data->fib_nb,
1597 nsim_fib_dump_inconsistent, extack);
1599 pr_err("Failed to register fib notifier\n");
1600 goto err_nexthop_nb_unregister;
1603 devl_resource_occ_get_register(devlink,
1604 NSIM_RESOURCE_IPV4_FIB,
1605 nsim_fib_ipv4_resource_occ_get,
1607 devl_resource_occ_get_register(devlink,
1608 NSIM_RESOURCE_IPV4_FIB_RULES,
1609 nsim_fib_ipv4_rules_res_occ_get,
1611 devl_resource_occ_get_register(devlink,
1612 NSIM_RESOURCE_IPV6_FIB,
1613 nsim_fib_ipv6_resource_occ_get,
1615 devl_resource_occ_get_register(devlink,
1616 NSIM_RESOURCE_IPV6_FIB_RULES,
1617 nsim_fib_ipv6_rules_res_occ_get,
1619 devl_resource_occ_get_register(devlink,
1620 NSIM_RESOURCE_NEXTHOPS,
1621 nsim_fib_nexthops_res_occ_get,
1625 err_nexthop_nb_unregister:
1626 unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb);
1627 err_rhashtable_fib_destroy:
1628 cancel_work_sync(&data->fib_flush_work);
1629 flush_work(&data->fib_event_work);
1630 rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free,
1632 err_rhashtable_nexthop_destroy:
1633 rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
1635 mutex_destroy(&data->fib_lock);
1637 mutex_destroy(&data->nh_lock);
1638 nsim_fib_debugfs_exit(data);
1641 return ERR_PTR(err);
1644 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data)
1646 devl_resource_occ_get_unregister(devlink,
1647 NSIM_RESOURCE_NEXTHOPS);
1648 devl_resource_occ_get_unregister(devlink,
1649 NSIM_RESOURCE_IPV6_FIB_RULES);
1650 devl_resource_occ_get_unregister(devlink,
1651 NSIM_RESOURCE_IPV6_FIB);
1652 devl_resource_occ_get_unregister(devlink,
1653 NSIM_RESOURCE_IPV4_FIB_RULES);
1654 devl_resource_occ_get_unregister(devlink,
1655 NSIM_RESOURCE_IPV4_FIB);
1656 unregister_fib_notifier(devlink_net(devlink), &data->fib_nb);
1657 unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb);
1658 cancel_work_sync(&data->fib_flush_work);
1659 flush_work(&data->fib_event_work);
1660 rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free,
1662 rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
1664 WARN_ON_ONCE(!list_empty(&data->fib_event_queue));
1665 WARN_ON_ONCE(!list_empty(&data->fib_rt_list));
1666 mutex_destroy(&data->fib_lock);
1667 mutex_destroy(&data->nh_lock);
1668 nsim_fib_debugfs_exit(data);