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/in6.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/rhashtable.h>
21 #include <linux/spinlock_types.h>
22 #include <linux/types.h>
23 #include <net/fib_notifier.h>
24 #include <net/ip_fib.h>
25 #include <net/ip6_fib.h>
26 #include <net/fib_rules.h>
27 #include <net/net_namespace.h>
28 #include <net/nexthop.h>
30 #include "netdevsim.h"
32 struct nsim_fib_entry {
37 struct nsim_per_fib_data {
38 struct nsim_fib_entry fib;
39 struct nsim_fib_entry rules;
42 struct nsim_fib_data {
43 struct notifier_block fib_nb;
44 struct nsim_per_fib_data ipv4;
45 struct nsim_per_fib_data ipv6;
46 struct nsim_fib_entry nexthops;
47 struct rhashtable fib_rt_ht;
48 struct list_head fib_rt_list;
49 struct mutex fib_lock; /* Protects hashtable and list */
50 struct notifier_block nexthop_nb;
51 struct rhashtable nexthop_ht;
52 struct devlink *devlink;
53 struct work_struct fib_event_work;
54 struct list_head fib_event_queue;
55 spinlock_t fib_event_queue_lock; /* Protects fib event queue list */
58 struct nsim_fib_rt_key {
59 unsigned char addr[sizeof(struct in6_addr)];
60 unsigned char prefix_len;
66 struct nsim_fib_rt_key key;
67 struct rhash_head ht_node;
68 struct list_head list; /* Member of fib_rt_list */
72 struct nsim_fib_rt common;
79 struct nsim_fib_rt common;
80 struct list_head nh_list;
84 struct nsim_fib6_rt_nh {
85 struct list_head list; /* Member of nh_list */
89 struct nsim_fib6_event {
90 struct fib6_info **rt_arr;
94 struct nsim_fib_event {
95 struct list_head list; /* node in fib queue */
97 struct fib_entry_notifier_info fen_info;
98 struct nsim_fib6_event fib6_event;
100 struct nsim_fib_data *data;
105 static const struct rhashtable_params nsim_fib_rt_ht_params = {
106 .key_offset = offsetof(struct nsim_fib_rt, key),
107 .head_offset = offsetof(struct nsim_fib_rt, ht_node),
108 .key_len = sizeof(struct nsim_fib_rt_key),
109 .automatic_shrinking = true,
112 struct nsim_nexthop {
113 struct rhash_head ht_node;
118 static const struct rhashtable_params nsim_nexthop_ht_params = {
119 .key_offset = offsetof(struct nsim_nexthop, id),
120 .head_offset = offsetof(struct nsim_nexthop, ht_node),
121 .key_len = sizeof(u32),
122 .automatic_shrinking = true,
125 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
126 enum nsim_resource_id res_id, bool max)
128 struct nsim_fib_entry *entry;
131 case NSIM_RESOURCE_IPV4_FIB:
132 entry = &fib_data->ipv4.fib;
134 case NSIM_RESOURCE_IPV4_FIB_RULES:
135 entry = &fib_data->ipv4.rules;
137 case NSIM_RESOURCE_IPV6_FIB:
138 entry = &fib_data->ipv6.fib;
140 case NSIM_RESOURCE_IPV6_FIB_RULES:
141 entry = &fib_data->ipv6.rules;
143 case NSIM_RESOURCE_NEXTHOPS:
144 entry = &fib_data->nexthops;
150 return max ? entry->max : atomic64_read(&entry->num);
153 static void nsim_fib_set_max(struct nsim_fib_data *fib_data,
154 enum nsim_resource_id res_id, u64 val)
156 struct nsim_fib_entry *entry;
159 case NSIM_RESOURCE_IPV4_FIB:
160 entry = &fib_data->ipv4.fib;
162 case NSIM_RESOURCE_IPV4_FIB_RULES:
163 entry = &fib_data->ipv4.rules;
165 case NSIM_RESOURCE_IPV6_FIB:
166 entry = &fib_data->ipv6.fib;
168 case NSIM_RESOURCE_IPV6_FIB_RULES:
169 entry = &fib_data->ipv6.rules;
171 case NSIM_RESOURCE_NEXTHOPS:
172 entry = &fib_data->nexthops;
181 static int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add,
182 struct netlink_ext_ack *extack)
187 if (!atomic64_add_unless(&entry->num, 1, entry->max)) {
189 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib rule entries");
192 atomic64_dec_if_positive(&entry->num);
198 static int nsim_fib_rule_event(struct nsim_fib_data *data,
199 struct fib_notifier_info *info, bool add)
201 struct netlink_ext_ack *extack = info->extack;
204 switch (info->family) {
206 err = nsim_fib_rule_account(&data->ipv4.rules, add, extack);
209 err = nsim_fib_rule_account(&data->ipv6.rules, add, extack);
216 static int nsim_fib_account(struct nsim_fib_entry *entry, bool add)
221 if (!atomic64_add_unless(&entry->num, 1, entry->max))
224 atomic64_dec_if_positive(&entry->num);
230 static void nsim_fib_rt_init(struct nsim_fib_data *data,
231 struct nsim_fib_rt *fib_rt, const void *addr,
232 size_t addr_len, unsigned int prefix_len,
233 int family, u32 tb_id)
235 memcpy(fib_rt->key.addr, addr, addr_len);
236 fib_rt->key.prefix_len = prefix_len;
237 fib_rt->key.family = family;
238 fib_rt->key.tb_id = tb_id;
239 list_add(&fib_rt->list, &data->fib_rt_list);
242 static void nsim_fib_rt_fini(struct nsim_fib_rt *fib_rt)
244 list_del(&fib_rt->list);
247 static struct nsim_fib_rt *nsim_fib_rt_lookup(struct rhashtable *fib_rt_ht,
248 const void *addr, size_t addr_len,
249 unsigned int prefix_len,
250 int family, u32 tb_id)
252 struct nsim_fib_rt_key key;
254 memset(&key, 0, sizeof(key));
255 memcpy(key.addr, addr, addr_len);
256 key.prefix_len = prefix_len;
260 return rhashtable_lookup_fast(fib_rt_ht, &key, nsim_fib_rt_ht_params);
263 static struct nsim_fib4_rt *
264 nsim_fib4_rt_create(struct nsim_fib_data *data,
265 struct fib_entry_notifier_info *fen_info)
267 struct nsim_fib4_rt *fib4_rt;
269 fib4_rt = kzalloc(sizeof(*fib4_rt), GFP_KERNEL);
273 nsim_fib_rt_init(data, &fib4_rt->common, &fen_info->dst, sizeof(u32),
274 fen_info->dst_len, AF_INET, fen_info->tb_id);
276 fib4_rt->fi = fen_info->fi;
277 fib_info_hold(fib4_rt->fi);
278 fib4_rt->tos = fen_info->tos;
279 fib4_rt->type = fen_info->type;
284 static void nsim_fib4_rt_destroy(struct nsim_fib4_rt *fib4_rt)
286 fib_info_put(fib4_rt->fi);
287 nsim_fib_rt_fini(&fib4_rt->common);
291 static struct nsim_fib4_rt *
292 nsim_fib4_rt_lookup(struct rhashtable *fib_rt_ht,
293 const struct fib_entry_notifier_info *fen_info)
295 struct nsim_fib_rt *fib_rt;
297 fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &fen_info->dst, sizeof(u32),
298 fen_info->dst_len, AF_INET,
303 return container_of(fib_rt, struct nsim_fib4_rt, common);
306 static void nsim_fib4_rt_hw_flags_set(struct net *net,
307 const struct nsim_fib4_rt *fib4_rt,
310 u32 *p_dst = (u32 *) fib4_rt->common.key.addr;
311 int dst_len = fib4_rt->common.key.prefix_len;
312 struct fib_rt_info fri;
314 fri.fi = fib4_rt->fi;
315 fri.tb_id = fib4_rt->common.key.tb_id;
316 fri.dst = cpu_to_be32(*p_dst);
317 fri.dst_len = dst_len;
318 fri.tos = fib4_rt->tos;
319 fri.type = fib4_rt->type;
322 fib_alias_hw_flags_set(net, &fri);
325 static int nsim_fib4_rt_add(struct nsim_fib_data *data,
326 struct nsim_fib4_rt *fib4_rt)
328 struct net *net = devlink_net(data->devlink);
331 err = rhashtable_insert_fast(&data->fib_rt_ht,
332 &fib4_rt->common.ht_node,
333 nsim_fib_rt_ht_params);
335 goto err_fib_dismiss;
337 /* Simulate hardware programming latency. */
339 nsim_fib4_rt_hw_flags_set(net, fib4_rt, true);
344 /* Drop the accounting that was increased from the notification
345 * context when FIB_EVENT_ENTRY_REPLACE was triggered.
347 nsim_fib_account(&data->ipv4.fib, false);
351 static int nsim_fib4_rt_replace(struct nsim_fib_data *data,
352 struct nsim_fib4_rt *fib4_rt,
353 struct nsim_fib4_rt *fib4_rt_old)
355 struct net *net = devlink_net(data->devlink);
358 /* We are replacing a route, so need to remove the accounting which
359 * was increased when FIB_EVENT_ENTRY_REPLACE was triggered.
361 err = nsim_fib_account(&data->ipv4.fib, false);
364 err = rhashtable_replace_fast(&data->fib_rt_ht,
365 &fib4_rt_old->common.ht_node,
366 &fib4_rt->common.ht_node,
367 nsim_fib_rt_ht_params);
372 nsim_fib4_rt_hw_flags_set(net, fib4_rt, true);
374 nsim_fib4_rt_hw_flags_set(net, fib4_rt_old, false);
375 nsim_fib4_rt_destroy(fib4_rt_old);
380 static int nsim_fib4_rt_insert(struct nsim_fib_data *data,
381 struct fib_entry_notifier_info *fen_info)
383 struct nsim_fib4_rt *fib4_rt, *fib4_rt_old;
386 fib4_rt = nsim_fib4_rt_create(data, fen_info);
390 fib4_rt_old = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info);
392 err = nsim_fib4_rt_add(data, fib4_rt);
394 err = nsim_fib4_rt_replace(data, fib4_rt, fib4_rt_old);
397 nsim_fib4_rt_destroy(fib4_rt);
402 static void nsim_fib4_rt_remove(struct nsim_fib_data *data,
403 const struct fib_entry_notifier_info *fen_info)
405 struct nsim_fib4_rt *fib4_rt;
407 fib4_rt = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info);
408 if (WARN_ON_ONCE(!fib4_rt))
411 rhashtable_remove_fast(&data->fib_rt_ht, &fib4_rt->common.ht_node,
412 nsim_fib_rt_ht_params);
413 nsim_fib4_rt_destroy(fib4_rt);
416 static int nsim_fib4_event(struct nsim_fib_data *data,
417 struct fib_entry_notifier_info *fen_info,
423 case FIB_EVENT_ENTRY_REPLACE:
424 err = nsim_fib4_rt_insert(data, fen_info);
426 case FIB_EVENT_ENTRY_DEL:
427 nsim_fib4_rt_remove(data, fen_info);
436 static struct nsim_fib6_rt_nh *
437 nsim_fib6_rt_nh_find(const struct nsim_fib6_rt *fib6_rt,
438 const struct fib6_info *rt)
440 struct nsim_fib6_rt_nh *fib6_rt_nh;
442 list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list) {
443 if (fib6_rt_nh->rt == rt)
450 static int nsim_fib6_rt_nh_add(struct nsim_fib6_rt *fib6_rt,
451 struct fib6_info *rt)
453 struct nsim_fib6_rt_nh *fib6_rt_nh;
455 fib6_rt_nh = kzalloc(sizeof(*fib6_rt_nh), GFP_KERNEL);
461 list_add_tail(&fib6_rt_nh->list, &fib6_rt->nh_list);
467 #if IS_ENABLED(CONFIG_IPV6)
468 static void nsim_rt6_release(struct fib6_info *rt)
470 fib6_info_release(rt);
473 static void nsim_rt6_release(struct fib6_info *rt)
478 static void nsim_fib6_rt_nh_del(struct nsim_fib6_rt *fib6_rt,
479 const struct fib6_info *rt)
481 struct nsim_fib6_rt_nh *fib6_rt_nh;
483 fib6_rt_nh = nsim_fib6_rt_nh_find(fib6_rt, rt);
484 if (WARN_ON_ONCE(!fib6_rt_nh))
488 list_del(&fib6_rt_nh->list);
489 nsim_rt6_release(fib6_rt_nh->rt);
493 static struct nsim_fib6_rt *
494 nsim_fib6_rt_create(struct nsim_fib_data *data,
495 struct fib6_info **rt_arr, unsigned int nrt6)
497 struct fib6_info *rt = rt_arr[0];
498 struct nsim_fib6_rt *fib6_rt;
502 fib6_rt = kzalloc(sizeof(*fib6_rt), GFP_KERNEL);
504 return ERR_PTR(-ENOMEM);
506 nsim_fib_rt_init(data, &fib6_rt->common, &rt->fib6_dst.addr,
507 sizeof(rt->fib6_dst.addr), rt->fib6_dst.plen, AF_INET6,
508 rt->fib6_table->tb6_id);
510 /* We consider a multipath IPv6 route as one entry, but it can be made
511 * up from several fib6_info structs (one for each nexthop), so we
512 * add them all to the same list under the entry.
514 INIT_LIST_HEAD(&fib6_rt->nh_list);
516 for (i = 0; i < nrt6; i++) {
517 err = nsim_fib6_rt_nh_add(fib6_rt, rt_arr[i]);
519 goto err_fib6_rt_nh_del;
525 for (i--; i >= 0; i--) {
526 nsim_fib6_rt_nh_del(fib6_rt, rt_arr[i]);
528 nsim_fib_rt_fini(&fib6_rt->common);
533 static void nsim_fib6_rt_destroy(struct nsim_fib6_rt *fib6_rt)
535 struct nsim_fib6_rt_nh *iter, *tmp;
537 list_for_each_entry_safe(iter, tmp, &fib6_rt->nh_list, list)
538 nsim_fib6_rt_nh_del(fib6_rt, iter->rt);
539 WARN_ON_ONCE(!list_empty(&fib6_rt->nh_list));
540 nsim_fib_rt_fini(&fib6_rt->common);
544 static struct nsim_fib6_rt *
545 nsim_fib6_rt_lookup(struct rhashtable *fib_rt_ht, const struct fib6_info *rt)
547 struct nsim_fib_rt *fib_rt;
549 fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &rt->fib6_dst.addr,
550 sizeof(rt->fib6_dst.addr),
551 rt->fib6_dst.plen, AF_INET6,
552 rt->fib6_table->tb6_id);
556 return container_of(fib_rt, struct nsim_fib6_rt, common);
559 static int nsim_fib6_rt_append(struct nsim_fib_data *data,
560 struct nsim_fib6_event *fib6_event)
562 struct fib6_info *rt = fib6_event->rt_arr[0];
563 struct nsim_fib6_rt *fib6_rt;
566 fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
567 if (WARN_ON_ONCE(!fib6_rt))
570 for (i = 0; i < fib6_event->nrt6; i++) {
571 err = nsim_fib6_rt_nh_add(fib6_rt, fib6_event->rt_arr[i]);
573 goto err_fib6_rt_nh_del;
575 fib6_event->rt_arr[i]->trap = true;
581 for (i--; i >= 0; i--) {
582 fib6_event->rt_arr[i]->trap = false;
583 nsim_fib6_rt_nh_del(fib6_rt, fib6_event->rt_arr[i]);
588 static void nsim_fib6_rt_hw_flags_set(struct nsim_fib_data *data,
589 const struct nsim_fib6_rt *fib6_rt,
592 struct net *net = devlink_net(data->devlink);
593 struct nsim_fib6_rt_nh *fib6_rt_nh;
595 list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list)
596 fib6_info_hw_flags_set(net, fib6_rt_nh->rt, false, trap);
599 static int nsim_fib6_rt_add(struct nsim_fib_data *data,
600 struct nsim_fib6_rt *fib6_rt)
604 err = rhashtable_insert_fast(&data->fib_rt_ht,
605 &fib6_rt->common.ht_node,
606 nsim_fib_rt_ht_params);
609 goto err_fib_dismiss;
612 nsim_fib6_rt_hw_flags_set(data, fib6_rt, true);
617 /* Drop the accounting that was increased from the notification
618 * context when FIB_EVENT_ENTRY_REPLACE was triggered.
620 nsim_fib_account(&data->ipv6.fib, false);
624 static int nsim_fib6_rt_replace(struct nsim_fib_data *data,
625 struct nsim_fib6_rt *fib6_rt,
626 struct nsim_fib6_rt *fib6_rt_old)
630 /* We are replacing a route, so need to remove the accounting which
631 * was increased when FIB_EVENT_ENTRY_REPLACE was triggered.
633 err = nsim_fib_account(&data->ipv6.fib, false);
637 err = rhashtable_replace_fast(&data->fib_rt_ht,
638 &fib6_rt_old->common.ht_node,
639 &fib6_rt->common.ht_node,
640 nsim_fib_rt_ht_params);
646 nsim_fib6_rt_hw_flags_set(data, fib6_rt, true);
648 nsim_fib6_rt_hw_flags_set(data, fib6_rt_old, false);
649 nsim_fib6_rt_destroy(fib6_rt_old);
654 static int nsim_fib6_rt_insert(struct nsim_fib_data *data,
655 struct nsim_fib6_event *fib6_event)
657 struct fib6_info *rt = fib6_event->rt_arr[0];
658 struct nsim_fib6_rt *fib6_rt, *fib6_rt_old;
661 fib6_rt = nsim_fib6_rt_create(data, fib6_event->rt_arr,
664 return PTR_ERR(fib6_rt);
666 fib6_rt_old = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
668 err = nsim_fib6_rt_add(data, fib6_rt);
670 err = nsim_fib6_rt_replace(data, fib6_rt, fib6_rt_old);
673 nsim_fib6_rt_destroy(fib6_rt);
678 static void nsim_fib6_rt_remove(struct nsim_fib_data *data,
679 struct nsim_fib6_event *fib6_event)
681 struct fib6_info *rt = fib6_event->rt_arr[0];
682 struct nsim_fib6_rt *fib6_rt;
685 /* Multipath routes are first added to the FIB trie and only then
686 * notified. If we vetoed the addition, we will get a delete
687 * notification for a route we do not have. Therefore, do not warn if
688 * route was not found.
690 fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
694 /* If not all the nexthops are deleted, then only reduce the nexthop
697 if (fib6_event->nrt6 != fib6_rt->nhs) {
698 for (i = 0; i < fib6_event->nrt6; i++)
699 nsim_fib6_rt_nh_del(fib6_rt, fib6_event->rt_arr[i]);
703 rhashtable_remove_fast(&data->fib_rt_ht, &fib6_rt->common.ht_node,
704 nsim_fib_rt_ht_params);
705 nsim_fib6_rt_destroy(fib6_rt);
708 static int nsim_fib6_event_init(struct nsim_fib6_event *fib6_event,
709 struct fib6_entry_notifier_info *fen6_info)
711 struct fib6_info *rt = fen6_info->rt;
712 struct fib6_info **rt_arr;
713 struct fib6_info *iter;
717 nrt6 = fen6_info->nsiblings + 1;
719 rt_arr = kcalloc(nrt6, sizeof(struct fib6_info *), GFP_ATOMIC);
723 fib6_event->rt_arr = rt_arr;
724 fib6_event->nrt6 = nrt6;
729 if (!fen6_info->nsiblings)
732 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
733 if (i == fen6_info->nsiblings)
736 rt_arr[i + 1] = iter;
737 fib6_info_hold(iter);
740 WARN_ON_ONCE(i != fen6_info->nsiblings);
745 static void nsim_fib6_event_fini(struct nsim_fib6_event *fib6_event)
749 for (i = 0; i < fib6_event->nrt6; i++)
750 nsim_rt6_release(fib6_event->rt_arr[i]);
751 kfree(fib6_event->rt_arr);
754 static int nsim_fib6_event(struct nsim_fib_data *data,
755 struct nsim_fib6_event *fib6_event,
760 if (fib6_event->rt_arr[0]->fib6_src.plen)
764 case FIB_EVENT_ENTRY_REPLACE:
765 err = nsim_fib6_rt_insert(data, fib6_event);
767 case FIB_EVENT_ENTRY_APPEND:
768 err = nsim_fib6_rt_append(data, fib6_event);
770 case FIB_EVENT_ENTRY_DEL:
771 nsim_fib6_rt_remove(data, fib6_event);
780 static int nsim_fib_event(struct nsim_fib_event *fib_event)
784 switch (fib_event->family) {
786 nsim_fib4_event(fib_event->data, &fib_event->fen_info,
788 fib_info_put(fib_event->fen_info.fi);
791 nsim_fib6_event(fib_event->data, &fib_event->fib6_event,
793 nsim_fib6_event_fini(&fib_event->fib6_event);
800 static int nsim_fib4_prepare_event(struct fib_notifier_info *info,
801 struct nsim_fib_event *fib_event,
804 struct nsim_fib_data *data = fib_event->data;
805 struct fib_entry_notifier_info *fen_info;
806 struct netlink_ext_ack *extack;
809 fen_info = container_of(info, struct fib_entry_notifier_info,
811 fib_event->fen_info = *fen_info;
812 extack = info->extack;
815 case FIB_EVENT_ENTRY_REPLACE:
816 err = nsim_fib_account(&data->ipv4.fib, true);
818 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
822 case FIB_EVENT_ENTRY_DEL:
823 nsim_fib_account(&data->ipv4.fib, false);
827 /* Take reference on fib_info to prevent it from being
828 * freed while event is queued. Release it afterwards.
830 fib_info_hold(fib_event->fen_info.fi);
835 static int nsim_fib6_prepare_event(struct fib_notifier_info *info,
836 struct nsim_fib_event *fib_event,
839 struct nsim_fib_data *data = fib_event->data;
840 struct fib6_entry_notifier_info *fen6_info;
841 struct netlink_ext_ack *extack;
844 fen6_info = container_of(info, struct fib6_entry_notifier_info,
847 err = nsim_fib6_event_init(&fib_event->fib6_event, fen6_info);
851 extack = info->extack;
853 case FIB_EVENT_ENTRY_REPLACE:
854 err = nsim_fib_account(&data->ipv6.fib, true);
856 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
857 goto err_fib6_event_fini;
860 case FIB_EVENT_ENTRY_DEL:
861 nsim_fib_account(&data->ipv6.fib, false);
868 nsim_fib6_event_fini(&fib_event->fib6_event);
872 static int nsim_fib_event_schedule_work(struct nsim_fib_data *data,
873 struct fib_notifier_info *info,
876 struct nsim_fib_event *fib_event;
879 if (info->family != AF_INET && info->family != AF_INET6)
880 /* netdevsim does not support 'RTNL_FAMILY_IP6MR' and
881 * 'RTNL_FAMILY_IPMR' and should ignore them.
885 fib_event = kzalloc(sizeof(*fib_event), GFP_ATOMIC);
889 fib_event->data = data;
890 fib_event->event = event;
891 fib_event->family = info->family;
893 switch (info->family) {
895 err = nsim_fib4_prepare_event(info, fib_event, event);
898 err = nsim_fib6_prepare_event(info, fib_event, event);
903 goto err_fib_prepare_event;
905 /* Enqueue the event and trigger the work */
906 spin_lock_bh(&data->fib_event_queue_lock);
907 list_add_tail(&fib_event->list, &data->fib_event_queue);
908 spin_unlock_bh(&data->fib_event_queue_lock);
909 schedule_work(&data->fib_event_work);
913 err_fib_prepare_event:
918 static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event,
921 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
923 struct fib_notifier_info *info = ptr;
927 case FIB_EVENT_RULE_ADD:
928 case FIB_EVENT_RULE_DEL:
929 err = nsim_fib_rule_event(data, info,
930 event == FIB_EVENT_RULE_ADD);
931 return notifier_from_errno(err);
932 case FIB_EVENT_ENTRY_REPLACE:
933 case FIB_EVENT_ENTRY_APPEND:
934 case FIB_EVENT_ENTRY_DEL:
935 return nsim_fib_event_schedule_work(data, info, event);
941 static void nsim_fib4_rt_free(struct nsim_fib_rt *fib_rt,
942 struct nsim_fib_data *data)
944 struct devlink *devlink = data->devlink;
945 struct nsim_fib4_rt *fib4_rt;
947 fib4_rt = container_of(fib_rt, struct nsim_fib4_rt, common);
948 nsim_fib4_rt_hw_flags_set(devlink_net(devlink), fib4_rt, false);
949 nsim_fib_account(&data->ipv4.fib, false);
950 nsim_fib4_rt_destroy(fib4_rt);
953 static void nsim_fib6_rt_free(struct nsim_fib_rt *fib_rt,
954 struct nsim_fib_data *data)
956 struct nsim_fib6_rt *fib6_rt;
958 fib6_rt = container_of(fib_rt, struct nsim_fib6_rt, common);
959 nsim_fib6_rt_hw_flags_set(data, fib6_rt, false);
960 nsim_fib_account(&data->ipv6.fib, false);
961 nsim_fib6_rt_destroy(fib6_rt);
964 static void nsim_fib_rt_free(void *ptr, void *arg)
966 struct nsim_fib_rt *fib_rt = ptr;
967 struct nsim_fib_data *data = arg;
969 switch (fib_rt->key.family) {
971 nsim_fib4_rt_free(fib_rt, data);
974 nsim_fib6_rt_free(fib_rt, data);
981 /* inconsistent dump, trying again */
982 static void nsim_fib_dump_inconsistent(struct notifier_block *nb)
984 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
986 struct nsim_fib_rt *fib_rt, *fib_rt_tmp;
988 /* Flush the work to make sure there is no race with notifications. */
989 flush_work(&data->fib_event_work);
991 /* The notifier block is still not registered, so we do not need to
992 * take any locks here.
994 list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) {
995 rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node,
996 nsim_fib_rt_ht_params);
997 nsim_fib_rt_free(fib_rt, data);
1000 atomic64_set(&data->ipv4.rules.num, 0ULL);
1001 atomic64_set(&data->ipv6.rules.num, 0ULL);
1004 static struct nsim_nexthop *nsim_nexthop_create(struct nsim_fib_data *data,
1005 struct nh_notifier_info *info)
1007 struct nsim_nexthop *nexthop;
1011 nexthop = kzalloc(sizeof(*nexthop), GFP_KERNEL);
1013 return ERR_PTR(-ENOMEM);
1015 nexthop->id = info->id;
1017 /* Determine the number of nexthop entries the new nexthop will
1021 switch (info->type) {
1022 case NH_NOTIFIER_INFO_TYPE_SINGLE:
1025 case NH_NOTIFIER_INFO_TYPE_GRP:
1026 for (i = 0; i < info->nh_grp->num_nh; i++)
1027 occ += info->nh_grp->nh_entries[i].weight;
1030 NL_SET_ERR_MSG_MOD(info->extack, "Unsupported nexthop type");
1032 return ERR_PTR(-EOPNOTSUPP);
1039 static void nsim_nexthop_destroy(struct nsim_nexthop *nexthop)
1044 static int nsim_nexthop_account(struct nsim_fib_data *data, u64 occ,
1045 bool add, struct netlink_ext_ack *extack)
1050 for (i = 0; i < occ; i++)
1051 if (!atomic64_add_unless(&data->nexthops.num, 1,
1052 data->nexthops.max)) {
1054 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported nexthops");
1055 goto err_num_decrease;
1058 if (WARN_ON(occ > atomic64_read(&data->nexthops.num)))
1060 atomic64_sub(occ, &data->nexthops.num);
1066 atomic64_sub(i, &data->nexthops.num);
1071 static int nsim_nexthop_add(struct nsim_fib_data *data,
1072 struct nsim_nexthop *nexthop,
1073 struct netlink_ext_ack *extack)
1075 struct net *net = devlink_net(data->devlink);
1078 err = nsim_nexthop_account(data, nexthop->occ, true, extack);
1082 err = rhashtable_insert_fast(&data->nexthop_ht, &nexthop->ht_node,
1083 nsim_nexthop_ht_params);
1085 NL_SET_ERR_MSG_MOD(extack, "Failed to insert nexthop");
1086 goto err_nexthop_dismiss;
1089 nexthop_set_hw_flags(net, nexthop->id, false, true);
1093 err_nexthop_dismiss:
1094 nsim_nexthop_account(data, nexthop->occ, false, extack);
1098 static int nsim_nexthop_replace(struct nsim_fib_data *data,
1099 struct nsim_nexthop *nexthop,
1100 struct nsim_nexthop *nexthop_old,
1101 struct netlink_ext_ack *extack)
1103 struct net *net = devlink_net(data->devlink);
1106 err = nsim_nexthop_account(data, nexthop->occ, true, extack);
1110 err = rhashtable_replace_fast(&data->nexthop_ht,
1111 &nexthop_old->ht_node, &nexthop->ht_node,
1112 nsim_nexthop_ht_params);
1114 NL_SET_ERR_MSG_MOD(extack, "Failed to replace nexthop");
1115 goto err_nexthop_dismiss;
1118 nexthop_set_hw_flags(net, nexthop->id, false, true);
1119 nsim_nexthop_account(data, nexthop_old->occ, false, extack);
1120 nsim_nexthop_destroy(nexthop_old);
1124 err_nexthop_dismiss:
1125 nsim_nexthop_account(data, nexthop->occ, false, extack);
1129 static int nsim_nexthop_insert(struct nsim_fib_data *data,
1130 struct nh_notifier_info *info)
1132 struct nsim_nexthop *nexthop, *nexthop_old;
1135 nexthop = nsim_nexthop_create(data, info);
1136 if (IS_ERR(nexthop))
1137 return PTR_ERR(nexthop);
1139 nexthop_old = rhashtable_lookup_fast(&data->nexthop_ht, &info->id,
1140 nsim_nexthop_ht_params);
1142 err = nsim_nexthop_add(data, nexthop, info->extack);
1144 err = nsim_nexthop_replace(data, nexthop, nexthop_old,
1148 nsim_nexthop_destroy(nexthop);
1153 static void nsim_nexthop_remove(struct nsim_fib_data *data,
1154 struct nh_notifier_info *info)
1156 struct nsim_nexthop *nexthop;
1158 nexthop = rhashtable_lookup_fast(&data->nexthop_ht, &info->id,
1159 nsim_nexthop_ht_params);
1163 rhashtable_remove_fast(&data->nexthop_ht, &nexthop->ht_node,
1164 nsim_nexthop_ht_params);
1165 nsim_nexthop_account(data, nexthop->occ, false, info->extack);
1166 nsim_nexthop_destroy(nexthop);
1169 static int nsim_nexthop_event_nb(struct notifier_block *nb, unsigned long event,
1172 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1174 struct nh_notifier_info *info = ptr;
1180 case NEXTHOP_EVENT_REPLACE:
1181 err = nsim_nexthop_insert(data, info);
1183 case NEXTHOP_EVENT_DEL:
1184 nsim_nexthop_remove(data, info);
1190 return notifier_from_errno(err);
1193 static void nsim_nexthop_free(void *ptr, void *arg)
1195 struct nsim_nexthop *nexthop = ptr;
1196 struct nsim_fib_data *data = arg;
1199 net = devlink_net(data->devlink);
1200 nexthop_set_hw_flags(net, nexthop->id, false, false);
1201 nsim_nexthop_account(data, nexthop->occ, false, NULL);
1202 nsim_nexthop_destroy(nexthop);
1205 static u64 nsim_fib_ipv4_resource_occ_get(void *priv)
1207 struct nsim_fib_data *data = priv;
1209 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB, false);
1212 static u64 nsim_fib_ipv4_rules_res_occ_get(void *priv)
1214 struct nsim_fib_data *data = priv;
1216 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB_RULES, false);
1219 static u64 nsim_fib_ipv6_resource_occ_get(void *priv)
1221 struct nsim_fib_data *data = priv;
1223 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB, false);
1226 static u64 nsim_fib_ipv6_rules_res_occ_get(void *priv)
1228 struct nsim_fib_data *data = priv;
1230 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB_RULES, false);
1233 static u64 nsim_fib_nexthops_res_occ_get(void *priv)
1235 struct nsim_fib_data *data = priv;
1237 return nsim_fib_get_val(data, NSIM_RESOURCE_NEXTHOPS, false);
1240 static void nsim_fib_set_max_all(struct nsim_fib_data *data,
1241 struct devlink *devlink)
1243 enum nsim_resource_id res_ids[] = {
1244 NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES,
1245 NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES,
1246 NSIM_RESOURCE_NEXTHOPS,
1250 for (i = 0; i < ARRAY_SIZE(res_ids); i++) {
1254 err = devlink_resource_size_get(devlink, res_ids[i], &val);
1257 nsim_fib_set_max(data, res_ids[i], val);
1261 static void nsim_fib_event_work(struct work_struct *work)
1263 struct nsim_fib_data *data = container_of(work, struct nsim_fib_data,
1265 struct nsim_fib_event *fib_event, *next_fib_event;
1267 LIST_HEAD(fib_event_queue);
1269 spin_lock_bh(&data->fib_event_queue_lock);
1270 list_splice_init(&data->fib_event_queue, &fib_event_queue);
1271 spin_unlock_bh(&data->fib_event_queue_lock);
1273 mutex_lock(&data->fib_lock);
1274 list_for_each_entry_safe(fib_event, next_fib_event, &fib_event_queue,
1276 nsim_fib_event(fib_event);
1277 list_del(&fib_event->list);
1281 mutex_unlock(&data->fib_lock);
1284 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
1285 struct netlink_ext_ack *extack)
1287 struct nsim_fib_data *data;
1290 data = kzalloc(sizeof(*data), GFP_KERNEL);
1292 return ERR_PTR(-ENOMEM);
1293 data->devlink = devlink;
1295 err = rhashtable_init(&data->nexthop_ht, &nsim_nexthop_ht_params);
1299 mutex_init(&data->fib_lock);
1300 INIT_LIST_HEAD(&data->fib_rt_list);
1301 err = rhashtable_init(&data->fib_rt_ht, &nsim_fib_rt_ht_params);
1303 goto err_rhashtable_nexthop_destroy;
1305 INIT_WORK(&data->fib_event_work, nsim_fib_event_work);
1306 INIT_LIST_HEAD(&data->fib_event_queue);
1307 spin_lock_init(&data->fib_event_queue_lock);
1309 nsim_fib_set_max_all(data, devlink);
1311 data->nexthop_nb.notifier_call = nsim_nexthop_event_nb;
1312 err = register_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb,
1315 pr_err("Failed to register nexthop notifier\n");
1316 goto err_rhashtable_fib_destroy;
1319 data->fib_nb.notifier_call = nsim_fib_event_nb;
1320 err = register_fib_notifier(devlink_net(devlink), &data->fib_nb,
1321 nsim_fib_dump_inconsistent, extack);
1323 pr_err("Failed to register fib notifier\n");
1324 goto err_nexthop_nb_unregister;
1327 devlink_resource_occ_get_register(devlink,
1328 NSIM_RESOURCE_IPV4_FIB,
1329 nsim_fib_ipv4_resource_occ_get,
1331 devlink_resource_occ_get_register(devlink,
1332 NSIM_RESOURCE_IPV4_FIB_RULES,
1333 nsim_fib_ipv4_rules_res_occ_get,
1335 devlink_resource_occ_get_register(devlink,
1336 NSIM_RESOURCE_IPV6_FIB,
1337 nsim_fib_ipv6_resource_occ_get,
1339 devlink_resource_occ_get_register(devlink,
1340 NSIM_RESOURCE_IPV6_FIB_RULES,
1341 nsim_fib_ipv6_rules_res_occ_get,
1343 devlink_resource_occ_get_register(devlink,
1344 NSIM_RESOURCE_NEXTHOPS,
1345 nsim_fib_nexthops_res_occ_get,
1349 err_nexthop_nb_unregister:
1350 unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb);
1351 err_rhashtable_fib_destroy:
1352 flush_work(&data->fib_event_work);
1353 rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free,
1355 err_rhashtable_nexthop_destroy:
1356 rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
1358 mutex_destroy(&data->fib_lock);
1361 return ERR_PTR(err);
1364 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data)
1366 devlink_resource_occ_get_unregister(devlink,
1367 NSIM_RESOURCE_NEXTHOPS);
1368 devlink_resource_occ_get_unregister(devlink,
1369 NSIM_RESOURCE_IPV6_FIB_RULES);
1370 devlink_resource_occ_get_unregister(devlink,
1371 NSIM_RESOURCE_IPV6_FIB);
1372 devlink_resource_occ_get_unregister(devlink,
1373 NSIM_RESOURCE_IPV4_FIB_RULES);
1374 devlink_resource_occ_get_unregister(devlink,
1375 NSIM_RESOURCE_IPV4_FIB);
1376 unregister_fib_notifier(devlink_net(devlink), &data->fib_nb);
1377 unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb);
1378 flush_work(&data->fib_event_work);
1379 rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free,
1381 rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
1383 WARN_ON_ONCE(!list_empty(&data->fib_event_queue));
1384 WARN_ON_ONCE(!list_empty(&data->fib_rt_list));
1385 mutex_destroy(&data->fib_lock);