1 /*
2 * Copyright (c) 2018 Cumulus Networks. All rights reserved.
3 * Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com>
4 *
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
7 * source tree.
8 *
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.
15 */
16
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>
32
33 #include "netdevsim.h"
34
35 struct nsim_fib_entry {
36 u64 max;
37 atomic64_t num;
38 };
39
40 struct nsim_per_fib_data {
41 struct nsim_fib_entry fib;
42 struct nsim_fib_entry rules;
43 };
44
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 */
61 struct dentry *ddir;
62 bool fail_route_offload;
63 bool fail_res_nexthop_group_replace;
64 bool fail_nexthop_bucket_replace;
65 };
66
67 struct nsim_fib_rt_key {
68 unsigned char addr[sizeof(struct in6_addr)];
69 unsigned char prefix_len;
70 int family;
71 u32 tb_id;
72 };
73
74 struct nsim_fib_rt {
75 struct nsim_fib_rt_key key;
76 struct rhash_head ht_node;
77 struct list_head list; /* Member of fib_rt_list */
78 };
79
80 struct nsim_fib4_rt {
81 struct nsim_fib_rt common;
82 struct fib_info *fi;
83 dscp_t dscp;
84 u8 type;
85 };
86
87 struct nsim_fib6_rt {
88 struct nsim_fib_rt common;
89 struct list_head nh_list;
90 unsigned int nhs;
91 };
92
93 struct nsim_fib6_rt_nh {
94 struct list_head list; /* Member of nh_list */
95 struct fib6_info *rt;
96 };
97
98 struct nsim_fib6_event {
99 struct fib6_info **rt_arr;
100 unsigned int nrt6;
101 };
102
103 struct nsim_fib_event {
104 struct list_head list; /* node in fib queue */
105 union {
106 struct fib_entry_notifier_info fen_info;
107 struct nsim_fib6_event fib6_event;
108 };
109 struct nsim_fib_data *data;
110 unsigned long event;
111 int family;
112 };
113
114 static const struct rhashtable_params nsim_fib_rt_ht_params = {
115 .key_offset = offsetof(struct nsim_fib_rt, key),
116 .head_offset = offsetof(struct nsim_fib_rt, ht_node),
117 .key_len = sizeof(struct nsim_fib_rt_key),
118 .automatic_shrinking = true,
119 };
120
121 struct nsim_nexthop {
122 struct rhash_head ht_node;
123 u64 occ;
124 u32 id;
125 bool is_resilient;
126 };
127
128 static const struct rhashtable_params nsim_nexthop_ht_params = {
129 .key_offset = offsetof(struct nsim_nexthop, id),
130 .head_offset = offsetof(struct nsim_nexthop, ht_node),
131 .key_len = sizeof(u32),
132 .automatic_shrinking = true,
133 };
134
nsim_fib_get_val(struct nsim_fib_data * fib_data,enum nsim_resource_id res_id,bool max)135 u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
136 enum nsim_resource_id res_id, bool max)
137 {
138 struct nsim_fib_entry *entry;
139
140 switch (res_id) {
141 case NSIM_RESOURCE_IPV4_FIB:
142 entry = &fib_data->ipv4.fib;
143 break;
144 case NSIM_RESOURCE_IPV4_FIB_RULES:
145 entry = &fib_data->ipv4.rules;
146 break;
147 case NSIM_RESOURCE_IPV6_FIB:
148 entry = &fib_data->ipv6.fib;
149 break;
150 case NSIM_RESOURCE_IPV6_FIB_RULES:
151 entry = &fib_data->ipv6.rules;
152 break;
153 case NSIM_RESOURCE_NEXTHOPS:
154 entry = &fib_data->nexthops;
155 break;
156 default:
157 return 0;
158 }
159
160 return max ? entry->max : atomic64_read(&entry->num);
161 }
162
nsim_fib_set_max(struct nsim_fib_data * fib_data,enum nsim_resource_id res_id,u64 val)163 static void nsim_fib_set_max(struct nsim_fib_data *fib_data,
164 enum nsim_resource_id res_id, u64 val)
165 {
166 struct nsim_fib_entry *entry;
167
168 switch (res_id) {
169 case NSIM_RESOURCE_IPV4_FIB:
170 entry = &fib_data->ipv4.fib;
171 break;
172 case NSIM_RESOURCE_IPV4_FIB_RULES:
173 entry = &fib_data->ipv4.rules;
174 break;
175 case NSIM_RESOURCE_IPV6_FIB:
176 entry = &fib_data->ipv6.fib;
177 break;
178 case NSIM_RESOURCE_IPV6_FIB_RULES:
179 entry = &fib_data->ipv6.rules;
180 break;
181 case NSIM_RESOURCE_NEXTHOPS:
182 entry = &fib_data->nexthops;
183 break;
184 default:
185 WARN_ON(1);
186 return;
187 }
188 entry->max = val;
189 }
190
nsim_fib_rule_account(struct nsim_fib_entry * entry,bool add,struct netlink_ext_ack * extack)191 static int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add,
192 struct netlink_ext_ack *extack)
193 {
194 int err = 0;
195
196 if (add) {
197 if (!atomic64_add_unless(&entry->num, 1, entry->max)) {
198 err = -ENOSPC;
199 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib rule entries");
200 }
201 } else {
202 atomic64_dec_if_positive(&entry->num);
203 }
204
205 return err;
206 }
207
nsim_fib_rule_event(struct nsim_fib_data * data,struct fib_notifier_info * info,bool add)208 static int nsim_fib_rule_event(struct nsim_fib_data *data,
209 struct fib_notifier_info *info, bool add)
210 {
211 struct netlink_ext_ack *extack = info->extack;
212 int err = 0;
213
214 switch (info->family) {
215 case AF_INET:
216 err = nsim_fib_rule_account(&data->ipv4.rules, add, extack);
217 break;
218 case AF_INET6:
219 err = nsim_fib_rule_account(&data->ipv6.rules, add, extack);
220 break;
221 }
222
223 return err;
224 }
225
nsim_fib_account(struct nsim_fib_entry * entry,bool add)226 static int nsim_fib_account(struct nsim_fib_entry *entry, bool add)
227 {
228 int err = 0;
229
230 if (add) {
231 if (!atomic64_add_unless(&entry->num, 1, entry->max))
232 err = -ENOSPC;
233 } else {
234 atomic64_dec_if_positive(&entry->num);
235 }
236
237 return err;
238 }
239
nsim_fib_rt_init(struct nsim_fib_data * data,struct nsim_fib_rt * fib_rt,const void * addr,size_t addr_len,unsigned int prefix_len,int family,u32 tb_id)240 static void nsim_fib_rt_init(struct nsim_fib_data *data,
241 struct nsim_fib_rt *fib_rt, const void *addr,
242 size_t addr_len, unsigned int prefix_len,
243 int family, u32 tb_id)
244 {
245 memcpy(fib_rt->key.addr, addr, addr_len);
246 fib_rt->key.prefix_len = prefix_len;
247 fib_rt->key.family = family;
248 fib_rt->key.tb_id = tb_id;
249 list_add(&fib_rt->list, &data->fib_rt_list);
250 }
251
nsim_fib_rt_fini(struct nsim_fib_rt * fib_rt)252 static void nsim_fib_rt_fini(struct nsim_fib_rt *fib_rt)
253 {
254 list_del(&fib_rt->list);
255 }
256
nsim_fib_rt_lookup(struct rhashtable * fib_rt_ht,const void * addr,size_t addr_len,unsigned int prefix_len,int family,u32 tb_id)257 static struct nsim_fib_rt *nsim_fib_rt_lookup(struct rhashtable *fib_rt_ht,
258 const void *addr, size_t addr_len,
259 unsigned int prefix_len,
260 int family, u32 tb_id)
261 {
262 struct nsim_fib_rt_key key;
263
264 memset(&key, 0, sizeof(key));
265 memcpy(key.addr, addr, addr_len);
266 key.prefix_len = prefix_len;
267 key.family = family;
268 key.tb_id = tb_id;
269
270 return rhashtable_lookup_fast(fib_rt_ht, &key, nsim_fib_rt_ht_params);
271 }
272
273 static struct nsim_fib4_rt *
nsim_fib4_rt_create(struct nsim_fib_data * data,struct fib_entry_notifier_info * fen_info)274 nsim_fib4_rt_create(struct nsim_fib_data *data,
275 struct fib_entry_notifier_info *fen_info)
276 {
277 struct nsim_fib4_rt *fib4_rt;
278
279 fib4_rt = kzalloc(sizeof(*fib4_rt), GFP_KERNEL);
280 if (!fib4_rt)
281 return NULL;
282
283 nsim_fib_rt_init(data, &fib4_rt->common, &fen_info->dst, sizeof(u32),
284 fen_info->dst_len, AF_INET, fen_info->tb_id);
285
286 fib4_rt->fi = fen_info->fi;
287 fib_info_hold(fib4_rt->fi);
288 fib4_rt->dscp = fen_info->dscp;
289 fib4_rt->type = fen_info->type;
290
291 return fib4_rt;
292 }
293
nsim_fib4_rt_destroy(struct nsim_fib4_rt * fib4_rt)294 static void nsim_fib4_rt_destroy(struct nsim_fib4_rt *fib4_rt)
295 {
296 fib_info_put(fib4_rt->fi);
297 nsim_fib_rt_fini(&fib4_rt->common);
298 kfree(fib4_rt);
299 }
300
301 static struct nsim_fib4_rt *
nsim_fib4_rt_lookup(struct rhashtable * fib_rt_ht,const struct fib_entry_notifier_info * fen_info)302 nsim_fib4_rt_lookup(struct rhashtable *fib_rt_ht,
303 const struct fib_entry_notifier_info *fen_info)
304 {
305 struct nsim_fib_rt *fib_rt;
306
307 fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &fen_info->dst, sizeof(u32),
308 fen_info->dst_len, AF_INET,
309 fen_info->tb_id);
310 if (!fib_rt)
311 return NULL;
312
313 return container_of(fib_rt, struct nsim_fib4_rt, common);
314 }
315
316 static void
nsim_fib4_rt_offload_failed_flag_set(struct net * net,struct fib_entry_notifier_info * fen_info)317 nsim_fib4_rt_offload_failed_flag_set(struct net *net,
318 struct fib_entry_notifier_info *fen_info)
319 {
320 u32 *p_dst = (u32 *)&fen_info->dst;
321 struct fib_rt_info fri;
322
323 fri.fi = fen_info->fi;
324 fri.tb_id = fen_info->tb_id;
325 fri.dst = cpu_to_be32(*p_dst);
326 fri.dst_len = fen_info->dst_len;
327 fri.dscp = fen_info->dscp;
328 fri.type = fen_info->type;
329 fri.offload = false;
330 fri.trap = false;
331 fri.offload_failed = true;
332 fib_alias_hw_flags_set(net, &fri);
333 }
334
nsim_fib4_rt_hw_flags_set(struct net * net,const struct nsim_fib4_rt * fib4_rt,bool trap)335 static void nsim_fib4_rt_hw_flags_set(struct net *net,
336 const struct nsim_fib4_rt *fib4_rt,
337 bool trap)
338 {
339 u32 *p_dst = (u32 *) fib4_rt->common.key.addr;
340 int dst_len = fib4_rt->common.key.prefix_len;
341 struct fib_rt_info fri;
342
343 fri.fi = fib4_rt->fi;
344 fri.tb_id = fib4_rt->common.key.tb_id;
345 fri.dst = cpu_to_be32(*p_dst);
346 fri.dst_len = dst_len;
347 fri.dscp = fib4_rt->dscp;
348 fri.type = fib4_rt->type;
349 fri.offload = false;
350 fri.trap = trap;
351 fri.offload_failed = false;
352 fib_alias_hw_flags_set(net, &fri);
353 }
354
nsim_fib4_rt_add(struct nsim_fib_data * data,struct nsim_fib4_rt * fib4_rt)355 static int nsim_fib4_rt_add(struct nsim_fib_data *data,
356 struct nsim_fib4_rt *fib4_rt)
357 {
358 struct net *net = devlink_net(data->devlink);
359 int err;
360
361 err = rhashtable_insert_fast(&data->fib_rt_ht,
362 &fib4_rt->common.ht_node,
363 nsim_fib_rt_ht_params);
364 if (err)
365 goto err_fib_dismiss;
366
367 /* Simulate hardware programming latency. */
368 msleep(1);
369 nsim_fib4_rt_hw_flags_set(net, fib4_rt, true);
370
371 return 0;
372
373 err_fib_dismiss:
374 /* Drop the accounting that was increased from the notification
375 * context when FIB_EVENT_ENTRY_REPLACE was triggered.
376 */
377 nsim_fib_account(&data->ipv4.fib, false);
378 return err;
379 }
380
nsim_fib4_rt_replace(struct nsim_fib_data * data,struct nsim_fib4_rt * fib4_rt,struct nsim_fib4_rt * fib4_rt_old)381 static int nsim_fib4_rt_replace(struct nsim_fib_data *data,
382 struct nsim_fib4_rt *fib4_rt,
383 struct nsim_fib4_rt *fib4_rt_old)
384 {
385 struct net *net = devlink_net(data->devlink);
386 int err;
387
388 /* We are replacing a route, so need to remove the accounting which
389 * was increased when FIB_EVENT_ENTRY_REPLACE was triggered.
390 */
391 err = nsim_fib_account(&data->ipv4.fib, false);
392 if (err)
393 return err;
394 err = rhashtable_replace_fast(&data->fib_rt_ht,
395 &fib4_rt_old->common.ht_node,
396 &fib4_rt->common.ht_node,
397 nsim_fib_rt_ht_params);
398 if (err)
399 return err;
400
401 msleep(1);
402 nsim_fib4_rt_hw_flags_set(net, fib4_rt, true);
403
404 nsim_fib4_rt_hw_flags_set(net, fib4_rt_old, false);
405 nsim_fib4_rt_destroy(fib4_rt_old);
406
407 return 0;
408 }
409
nsim_fib4_rt_insert(struct nsim_fib_data * data,struct fib_entry_notifier_info * fen_info)410 static int nsim_fib4_rt_insert(struct nsim_fib_data *data,
411 struct fib_entry_notifier_info *fen_info)
412 {
413 struct nsim_fib4_rt *fib4_rt, *fib4_rt_old;
414 int err;
415
416 if (data->fail_route_offload) {
417 /* For testing purposes, user set debugfs fail_route_offload
418 * value to true. Simulate hardware programming latency and then
419 * fail.
420 */
421 msleep(1);
422 return -EINVAL;
423 }
424
425 fib4_rt = nsim_fib4_rt_create(data, fen_info);
426 if (!fib4_rt)
427 return -ENOMEM;
428
429 fib4_rt_old = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info);
430 if (!fib4_rt_old)
431 err = nsim_fib4_rt_add(data, fib4_rt);
432 else
433 err = nsim_fib4_rt_replace(data, fib4_rt, fib4_rt_old);
434
435 if (err)
436 nsim_fib4_rt_destroy(fib4_rt);
437
438 return err;
439 }
440
nsim_fib4_rt_remove(struct nsim_fib_data * data,const struct fib_entry_notifier_info * fen_info)441 static void nsim_fib4_rt_remove(struct nsim_fib_data *data,
442 const struct fib_entry_notifier_info *fen_info)
443 {
444 struct nsim_fib4_rt *fib4_rt;
445
446 fib4_rt = nsim_fib4_rt_lookup(&data->fib_rt_ht, fen_info);
447 if (!fib4_rt)
448 return;
449
450 rhashtable_remove_fast(&data->fib_rt_ht, &fib4_rt->common.ht_node,
451 nsim_fib_rt_ht_params);
452 nsim_fib4_rt_destroy(fib4_rt);
453 }
454
nsim_fib4_event(struct nsim_fib_data * data,struct fib_entry_notifier_info * fen_info,unsigned long event)455 static int nsim_fib4_event(struct nsim_fib_data *data,
456 struct fib_entry_notifier_info *fen_info,
457 unsigned long event)
458 {
459 int err = 0;
460
461 switch (event) {
462 case FIB_EVENT_ENTRY_REPLACE:
463 err = nsim_fib4_rt_insert(data, fen_info);
464 if (err) {
465 struct net *net = devlink_net(data->devlink);
466
467 nsim_fib4_rt_offload_failed_flag_set(net, fen_info);
468 }
469 break;
470 case FIB_EVENT_ENTRY_DEL:
471 nsim_fib4_rt_remove(data, fen_info);
472 break;
473 default:
474 break;
475 }
476
477 return err;
478 }
479
480 static struct nsim_fib6_rt_nh *
nsim_fib6_rt_nh_find(const struct nsim_fib6_rt * fib6_rt,const struct fib6_info * rt)481 nsim_fib6_rt_nh_find(const struct nsim_fib6_rt *fib6_rt,
482 const struct fib6_info *rt)
483 {
484 struct nsim_fib6_rt_nh *fib6_rt_nh;
485
486 list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list) {
487 if (fib6_rt_nh->rt == rt)
488 return fib6_rt_nh;
489 }
490
491 return NULL;
492 }
493
nsim_fib6_rt_nh_add(struct nsim_fib6_rt * fib6_rt,struct fib6_info * rt)494 static int nsim_fib6_rt_nh_add(struct nsim_fib6_rt *fib6_rt,
495 struct fib6_info *rt)
496 {
497 struct nsim_fib6_rt_nh *fib6_rt_nh;
498
499 fib6_rt_nh = kzalloc(sizeof(*fib6_rt_nh), GFP_KERNEL);
500 if (!fib6_rt_nh)
501 return -ENOMEM;
502
503 fib6_info_hold(rt);
504 fib6_rt_nh->rt = rt;
505 list_add_tail(&fib6_rt_nh->list, &fib6_rt->nh_list);
506 fib6_rt->nhs++;
507
508 return 0;
509 }
510
511 #if IS_ENABLED(CONFIG_IPV6)
nsim_rt6_release(struct fib6_info * rt)512 static void nsim_rt6_release(struct fib6_info *rt)
513 {
514 fib6_info_release(rt);
515 }
516 #else
nsim_rt6_release(struct fib6_info * rt)517 static void nsim_rt6_release(struct fib6_info *rt)
518 {
519 }
520 #endif
521
nsim_fib6_rt_nh_del(struct nsim_fib6_rt * fib6_rt,const struct fib6_info * rt)522 static void nsim_fib6_rt_nh_del(struct nsim_fib6_rt *fib6_rt,
523 const struct fib6_info *rt)
524 {
525 struct nsim_fib6_rt_nh *fib6_rt_nh;
526
527 fib6_rt_nh = nsim_fib6_rt_nh_find(fib6_rt, rt);
528 if (!fib6_rt_nh)
529 return;
530
531 fib6_rt->nhs--;
532 list_del(&fib6_rt_nh->list);
533 nsim_rt6_release(fib6_rt_nh->rt);
534 kfree(fib6_rt_nh);
535 }
536
537 static struct nsim_fib6_rt *
nsim_fib6_rt_create(struct nsim_fib_data * data,struct fib6_info ** rt_arr,unsigned int nrt6)538 nsim_fib6_rt_create(struct nsim_fib_data *data,
539 struct fib6_info **rt_arr, unsigned int nrt6)
540 {
541 struct fib6_info *rt = rt_arr[0];
542 struct nsim_fib6_rt *fib6_rt;
543 int i = 0;
544 int err;
545
546 fib6_rt = kzalloc(sizeof(*fib6_rt), GFP_KERNEL);
547 if (!fib6_rt)
548 return ERR_PTR(-ENOMEM);
549
550 nsim_fib_rt_init(data, &fib6_rt->common, &rt->fib6_dst.addr,
551 sizeof(rt->fib6_dst.addr), rt->fib6_dst.plen, AF_INET6,
552 rt->fib6_table->tb6_id);
553
554 /* We consider a multipath IPv6 route as one entry, but it can be made
555 * up from several fib6_info structs (one for each nexthop), so we
556 * add them all to the same list under the entry.
557 */
558 INIT_LIST_HEAD(&fib6_rt->nh_list);
559
560 for (i = 0; i < nrt6; i++) {
561 err = nsim_fib6_rt_nh_add(fib6_rt, rt_arr[i]);
562 if (err)
563 goto err_fib6_rt_nh_del;
564 }
565
566 return fib6_rt;
567
568 err_fib6_rt_nh_del:
569 for (i--; i >= 0; i--) {
570 nsim_fib6_rt_nh_del(fib6_rt, rt_arr[i]);
571 }
572 nsim_fib_rt_fini(&fib6_rt->common);
573 kfree(fib6_rt);
574 return ERR_PTR(err);
575 }
576
nsim_fib6_rt_destroy(struct nsim_fib6_rt * fib6_rt)577 static void nsim_fib6_rt_destroy(struct nsim_fib6_rt *fib6_rt)
578 {
579 struct nsim_fib6_rt_nh *iter, *tmp;
580
581 list_for_each_entry_safe(iter, tmp, &fib6_rt->nh_list, list)
582 nsim_fib6_rt_nh_del(fib6_rt, iter->rt);
583 WARN_ON_ONCE(!list_empty(&fib6_rt->nh_list));
584 nsim_fib_rt_fini(&fib6_rt->common);
585 kfree(fib6_rt);
586 }
587
588 static struct nsim_fib6_rt *
nsim_fib6_rt_lookup(struct rhashtable * fib_rt_ht,const struct fib6_info * rt)589 nsim_fib6_rt_lookup(struct rhashtable *fib_rt_ht, const struct fib6_info *rt)
590 {
591 struct nsim_fib_rt *fib_rt;
592
593 fib_rt = nsim_fib_rt_lookup(fib_rt_ht, &rt->fib6_dst.addr,
594 sizeof(rt->fib6_dst.addr),
595 rt->fib6_dst.plen, AF_INET6,
596 rt->fib6_table->tb6_id);
597 if (!fib_rt)
598 return NULL;
599
600 return container_of(fib_rt, struct nsim_fib6_rt, common);
601 }
602
nsim_fib6_rt_append(struct nsim_fib_data * data,struct nsim_fib6_event * fib6_event)603 static int nsim_fib6_rt_append(struct nsim_fib_data *data,
604 struct nsim_fib6_event *fib6_event)
605 {
606 struct fib6_info *rt = fib6_event->rt_arr[0];
607 struct nsim_fib6_rt *fib6_rt;
608 int i, err;
609
610 if (data->fail_route_offload) {
611 /* For testing purposes, user set debugfs fail_route_offload
612 * value to true. Simulate hardware programming latency and then
613 * fail.
614 */
615 msleep(1);
616 return -EINVAL;
617 }
618
619 fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
620 if (!fib6_rt)
621 return -EINVAL;
622
623 for (i = 0; i < fib6_event->nrt6; i++) {
624 err = nsim_fib6_rt_nh_add(fib6_rt, fib6_event->rt_arr[i]);
625 if (err)
626 goto err_fib6_rt_nh_del;
627
628 WRITE_ONCE(fib6_event->rt_arr[i]->trap, true);
629 }
630
631 return 0;
632
633 err_fib6_rt_nh_del:
634 for (i--; i >= 0; i--) {
635 WRITE_ONCE(fib6_event->rt_arr[i]->trap, false);
636 nsim_fib6_rt_nh_del(fib6_rt, fib6_event->rt_arr[i]);
637 }
638 return err;
639 }
640
641 #if IS_ENABLED(CONFIG_IPV6)
nsim_fib6_rt_offload_failed_flag_set(struct nsim_fib_data * data,struct fib6_info ** rt_arr,unsigned int nrt6)642 static void nsim_fib6_rt_offload_failed_flag_set(struct nsim_fib_data *data,
643 struct fib6_info **rt_arr,
644 unsigned int nrt6)
645
646 {
647 struct net *net = devlink_net(data->devlink);
648 int i;
649
650 for (i = 0; i < nrt6; i++)
651 fib6_info_hw_flags_set(net, rt_arr[i], false, false, true);
652 }
653 #else
nsim_fib6_rt_offload_failed_flag_set(struct nsim_fib_data * data,struct fib6_info ** rt_arr,unsigned int nrt6)654 static void nsim_fib6_rt_offload_failed_flag_set(struct nsim_fib_data *data,
655 struct fib6_info **rt_arr,
656 unsigned int nrt6)
657 {
658 }
659 #endif
660
661 #if IS_ENABLED(CONFIG_IPV6)
nsim_fib6_rt_hw_flags_set(struct nsim_fib_data * data,const struct nsim_fib6_rt * fib6_rt,bool trap)662 static void nsim_fib6_rt_hw_flags_set(struct nsim_fib_data *data,
663 const struct nsim_fib6_rt *fib6_rt,
664 bool trap)
665 {
666 struct net *net = devlink_net(data->devlink);
667 struct nsim_fib6_rt_nh *fib6_rt_nh;
668
669 list_for_each_entry(fib6_rt_nh, &fib6_rt->nh_list, list)
670 fib6_info_hw_flags_set(net, fib6_rt_nh->rt, false, trap, false);
671 }
672 #else
nsim_fib6_rt_hw_flags_set(struct nsim_fib_data * data,const struct nsim_fib6_rt * fib6_rt,bool trap)673 static void nsim_fib6_rt_hw_flags_set(struct nsim_fib_data *data,
674 const struct nsim_fib6_rt *fib6_rt,
675 bool trap)
676 {
677 }
678 #endif
679
nsim_fib6_rt_add(struct nsim_fib_data * data,struct nsim_fib6_rt * fib6_rt)680 static int nsim_fib6_rt_add(struct nsim_fib_data *data,
681 struct nsim_fib6_rt *fib6_rt)
682 {
683 int err;
684
685 err = rhashtable_insert_fast(&data->fib_rt_ht,
686 &fib6_rt->common.ht_node,
687 nsim_fib_rt_ht_params);
688
689 if (err)
690 goto err_fib_dismiss;
691
692 msleep(1);
693 nsim_fib6_rt_hw_flags_set(data, fib6_rt, true);
694
695 return 0;
696
697 err_fib_dismiss:
698 /* Drop the accounting that was increased from the notification
699 * context when FIB_EVENT_ENTRY_REPLACE was triggered.
700 */
701 nsim_fib_account(&data->ipv6.fib, false);
702 return err;
703 }
704
nsim_fib6_rt_replace(struct nsim_fib_data * data,struct nsim_fib6_rt * fib6_rt,struct nsim_fib6_rt * fib6_rt_old)705 static int nsim_fib6_rt_replace(struct nsim_fib_data *data,
706 struct nsim_fib6_rt *fib6_rt,
707 struct nsim_fib6_rt *fib6_rt_old)
708 {
709 int err;
710
711 /* We are replacing a route, so need to remove the accounting which
712 * was increased when FIB_EVENT_ENTRY_REPLACE was triggered.
713 */
714 err = nsim_fib_account(&data->ipv6.fib, false);
715 if (err)
716 return err;
717
718 err = rhashtable_replace_fast(&data->fib_rt_ht,
719 &fib6_rt_old->common.ht_node,
720 &fib6_rt->common.ht_node,
721 nsim_fib_rt_ht_params);
722
723 if (err)
724 return err;
725
726 msleep(1);
727 nsim_fib6_rt_hw_flags_set(data, fib6_rt, true);
728
729 nsim_fib6_rt_hw_flags_set(data, fib6_rt_old, false);
730 nsim_fib6_rt_destroy(fib6_rt_old);
731
732 return 0;
733 }
734
nsim_fib6_rt_insert(struct nsim_fib_data * data,struct nsim_fib6_event * fib6_event)735 static int nsim_fib6_rt_insert(struct nsim_fib_data *data,
736 struct nsim_fib6_event *fib6_event)
737 {
738 struct fib6_info *rt = fib6_event->rt_arr[0];
739 struct nsim_fib6_rt *fib6_rt, *fib6_rt_old;
740 int err;
741
742 if (data->fail_route_offload) {
743 /* For testing purposes, user set debugfs fail_route_offload
744 * value to true. Simulate hardware programming latency and then
745 * fail.
746 */
747 msleep(1);
748 return -EINVAL;
749 }
750
751 fib6_rt = nsim_fib6_rt_create(data, fib6_event->rt_arr,
752 fib6_event->nrt6);
753 if (IS_ERR(fib6_rt))
754 return PTR_ERR(fib6_rt);
755
756 fib6_rt_old = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
757 if (!fib6_rt_old)
758 err = nsim_fib6_rt_add(data, fib6_rt);
759 else
760 err = nsim_fib6_rt_replace(data, fib6_rt, fib6_rt_old);
761
762 if (err)
763 nsim_fib6_rt_destroy(fib6_rt);
764
765 return err;
766 }
767
nsim_fib6_rt_remove(struct nsim_fib_data * data,struct nsim_fib6_event * fib6_event)768 static void nsim_fib6_rt_remove(struct nsim_fib_data *data,
769 struct nsim_fib6_event *fib6_event)
770 {
771 struct fib6_info *rt = fib6_event->rt_arr[0];
772 struct nsim_fib6_rt *fib6_rt;
773 int i;
774
775 /* Multipath routes are first added to the FIB trie and only then
776 * notified. If we vetoed the addition, we will get a delete
777 * notification for a route we do not have. Therefore, do not warn if
778 * route was not found.
779 */
780 fib6_rt = nsim_fib6_rt_lookup(&data->fib_rt_ht, rt);
781 if (!fib6_rt)
782 return;
783
784 /* If not all the nexthops are deleted, then only reduce the nexthop
785 * group.
786 */
787 if (fib6_event->nrt6 != fib6_rt->nhs) {
788 for (i = 0; i < fib6_event->nrt6; i++)
789 nsim_fib6_rt_nh_del(fib6_rt, fib6_event->rt_arr[i]);
790 return;
791 }
792
793 rhashtable_remove_fast(&data->fib_rt_ht, &fib6_rt->common.ht_node,
794 nsim_fib_rt_ht_params);
795 nsim_fib6_rt_destroy(fib6_rt);
796 }
797
nsim_fib6_event_init(struct nsim_fib6_event * fib6_event,struct fib6_entry_notifier_info * fen6_info)798 static int nsim_fib6_event_init(struct nsim_fib6_event *fib6_event,
799 struct fib6_entry_notifier_info *fen6_info)
800 {
801 struct fib6_info *rt = fen6_info->rt;
802 struct fib6_info **rt_arr;
803 struct fib6_info *iter;
804 unsigned int nrt6;
805 int i = 0;
806
807 nrt6 = fen6_info->nsiblings + 1;
808
809 rt_arr = kcalloc(nrt6, sizeof(struct fib6_info *), GFP_ATOMIC);
810 if (!rt_arr)
811 return -ENOMEM;
812
813 fib6_event->rt_arr = rt_arr;
814 fib6_event->nrt6 = nrt6;
815
816 rt_arr[0] = rt;
817 fib6_info_hold(rt);
818
819 if (!fen6_info->nsiblings)
820 return 0;
821
822 list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
823 if (i == fen6_info->nsiblings)
824 break;
825
826 rt_arr[i + 1] = iter;
827 fib6_info_hold(iter);
828 i++;
829 }
830 WARN_ON_ONCE(i != fen6_info->nsiblings);
831
832 return 0;
833 }
834
nsim_fib6_event_fini(struct nsim_fib6_event * fib6_event)835 static void nsim_fib6_event_fini(struct nsim_fib6_event *fib6_event)
836 {
837 int i;
838
839 for (i = 0; i < fib6_event->nrt6; i++)
840 nsim_rt6_release(fib6_event->rt_arr[i]);
841 kfree(fib6_event->rt_arr);
842 }
843
nsim_fib6_event(struct nsim_fib_data * data,struct nsim_fib6_event * fib6_event,unsigned long event)844 static int nsim_fib6_event(struct nsim_fib_data *data,
845 struct nsim_fib6_event *fib6_event,
846 unsigned long event)
847 {
848 int err;
849
850 if (fib6_event->rt_arr[0]->fib6_src.plen)
851 return 0;
852
853 switch (event) {
854 case FIB_EVENT_ENTRY_REPLACE:
855 err = nsim_fib6_rt_insert(data, fib6_event);
856 if (err)
857 goto err_rt_offload_failed_flag_set;
858 break;
859 case FIB_EVENT_ENTRY_APPEND:
860 err = nsim_fib6_rt_append(data, fib6_event);
861 if (err)
862 goto err_rt_offload_failed_flag_set;
863 break;
864 case FIB_EVENT_ENTRY_DEL:
865 nsim_fib6_rt_remove(data, fib6_event);
866 break;
867 default:
868 break;
869 }
870
871 return 0;
872
873 err_rt_offload_failed_flag_set:
874 nsim_fib6_rt_offload_failed_flag_set(data, fib6_event->rt_arr,
875 fib6_event->nrt6);
876 return err;
877 }
878
nsim_fib_event(struct nsim_fib_event * fib_event)879 static void nsim_fib_event(struct nsim_fib_event *fib_event)
880 {
881 switch (fib_event->family) {
882 case AF_INET:
883 nsim_fib4_event(fib_event->data, &fib_event->fen_info,
884 fib_event->event);
885 fib_info_put(fib_event->fen_info.fi);
886 break;
887 case AF_INET6:
888 nsim_fib6_event(fib_event->data, &fib_event->fib6_event,
889 fib_event->event);
890 nsim_fib6_event_fini(&fib_event->fib6_event);
891 break;
892 }
893 }
894
nsim_fib4_prepare_event(struct fib_notifier_info * info,struct nsim_fib_event * fib_event,unsigned long event)895 static int nsim_fib4_prepare_event(struct fib_notifier_info *info,
896 struct nsim_fib_event *fib_event,
897 unsigned long event)
898 {
899 struct nsim_fib_data *data = fib_event->data;
900 struct fib_entry_notifier_info *fen_info;
901 struct netlink_ext_ack *extack;
902 int err = 0;
903
904 fen_info = container_of(info, struct fib_entry_notifier_info,
905 info);
906 fib_event->fen_info = *fen_info;
907 extack = info->extack;
908
909 switch (event) {
910 case FIB_EVENT_ENTRY_REPLACE:
911 err = nsim_fib_account(&data->ipv4.fib, true);
912 if (err) {
913 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
914 return err;
915 }
916 break;
917 case FIB_EVENT_ENTRY_DEL:
918 nsim_fib_account(&data->ipv4.fib, false);
919 break;
920 }
921
922 /* Take reference on fib_info to prevent it from being
923 * freed while event is queued. Release it afterwards.
924 */
925 fib_info_hold(fib_event->fen_info.fi);
926
927 return 0;
928 }
929
nsim_fib6_prepare_event(struct fib_notifier_info * info,struct nsim_fib_event * fib_event,unsigned long event)930 static int nsim_fib6_prepare_event(struct fib_notifier_info *info,
931 struct nsim_fib_event *fib_event,
932 unsigned long event)
933 {
934 struct nsim_fib_data *data = fib_event->data;
935 struct fib6_entry_notifier_info *fen6_info;
936 struct netlink_ext_ack *extack;
937 int err = 0;
938
939 fen6_info = container_of(info, struct fib6_entry_notifier_info,
940 info);
941
942 err = nsim_fib6_event_init(&fib_event->fib6_event, fen6_info);
943 if (err)
944 return err;
945
946 extack = info->extack;
947 switch (event) {
948 case FIB_EVENT_ENTRY_REPLACE:
949 err = nsim_fib_account(&data->ipv6.fib, true);
950 if (err) {
951 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
952 goto err_fib6_event_fini;
953 }
954 break;
955 case FIB_EVENT_ENTRY_DEL:
956 nsim_fib_account(&data->ipv6.fib, false);
957 break;
958 }
959
960 return 0;
961
962 err_fib6_event_fini:
963 nsim_fib6_event_fini(&fib_event->fib6_event);
964 return err;
965 }
966
nsim_fib_event_schedule_work(struct nsim_fib_data * data,struct fib_notifier_info * info,unsigned long event)967 static int nsim_fib_event_schedule_work(struct nsim_fib_data *data,
968 struct fib_notifier_info *info,
969 unsigned long event)
970 {
971 struct nsim_fib_event *fib_event;
972 int err;
973
974 if (info->family != AF_INET && info->family != AF_INET6)
975 /* netdevsim does not support 'RTNL_FAMILY_IP6MR' and
976 * 'RTNL_FAMILY_IPMR' and should ignore them.
977 */
978 return NOTIFY_DONE;
979
980 fib_event = kzalloc(sizeof(*fib_event), GFP_ATOMIC);
981 if (!fib_event)
982 goto err_fib_event_alloc;
983
984 fib_event->data = data;
985 fib_event->event = event;
986 fib_event->family = info->family;
987
988 switch (info->family) {
989 case AF_INET:
990 err = nsim_fib4_prepare_event(info, fib_event, event);
991 break;
992 case AF_INET6:
993 err = nsim_fib6_prepare_event(info, fib_event, event);
994 break;
995 }
996
997 if (err)
998 goto err_fib_prepare_event;
999
1000 /* Enqueue the event and trigger the work */
1001 spin_lock_bh(&data->fib_event_queue_lock);
1002 list_add_tail(&fib_event->list, &data->fib_event_queue);
1003 spin_unlock_bh(&data->fib_event_queue_lock);
1004 schedule_work(&data->fib_event_work);
1005
1006 return NOTIFY_DONE;
1007
1008 err_fib_prepare_event:
1009 kfree(fib_event);
1010 err_fib_event_alloc:
1011 if (event == FIB_EVENT_ENTRY_DEL)
1012 schedule_work(&data->fib_flush_work);
1013 return NOTIFY_BAD;
1014 }
1015
nsim_fib_event_nb(struct notifier_block * nb,unsigned long event,void * ptr)1016 static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event,
1017 void *ptr)
1018 {
1019 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1020 fib_nb);
1021 struct fib_notifier_info *info = ptr;
1022 int err;
1023
1024 switch (event) {
1025 case FIB_EVENT_RULE_ADD:
1026 case FIB_EVENT_RULE_DEL:
1027 err = nsim_fib_rule_event(data, info,
1028 event == FIB_EVENT_RULE_ADD);
1029 return notifier_from_errno(err);
1030 case FIB_EVENT_ENTRY_REPLACE:
1031 case FIB_EVENT_ENTRY_APPEND:
1032 case FIB_EVENT_ENTRY_DEL:
1033 return nsim_fib_event_schedule_work(data, info, event);
1034 }
1035
1036 return NOTIFY_DONE;
1037 }
1038
nsim_fib4_rt_free(struct nsim_fib_rt * fib_rt,struct nsim_fib_data * data)1039 static void nsim_fib4_rt_free(struct nsim_fib_rt *fib_rt,
1040 struct nsim_fib_data *data)
1041 {
1042 struct devlink *devlink = data->devlink;
1043 struct nsim_fib4_rt *fib4_rt;
1044
1045 fib4_rt = container_of(fib_rt, struct nsim_fib4_rt, common);
1046 nsim_fib4_rt_hw_flags_set(devlink_net(devlink), fib4_rt, false);
1047 nsim_fib_account(&data->ipv4.fib, false);
1048 nsim_fib4_rt_destroy(fib4_rt);
1049 }
1050
nsim_fib6_rt_free(struct nsim_fib_rt * fib_rt,struct nsim_fib_data * data)1051 static void nsim_fib6_rt_free(struct nsim_fib_rt *fib_rt,
1052 struct nsim_fib_data *data)
1053 {
1054 struct nsim_fib6_rt *fib6_rt;
1055
1056 fib6_rt = container_of(fib_rt, struct nsim_fib6_rt, common);
1057 nsim_fib6_rt_hw_flags_set(data, fib6_rt, false);
1058 nsim_fib_account(&data->ipv6.fib, false);
1059 nsim_fib6_rt_destroy(fib6_rt);
1060 }
1061
nsim_fib_rt_free(void * ptr,void * arg)1062 static void nsim_fib_rt_free(void *ptr, void *arg)
1063 {
1064 struct nsim_fib_rt *fib_rt = ptr;
1065 struct nsim_fib_data *data = arg;
1066
1067 switch (fib_rt->key.family) {
1068 case AF_INET:
1069 nsim_fib4_rt_free(fib_rt, data);
1070 break;
1071 case AF_INET6:
1072 nsim_fib6_rt_free(fib_rt, data);
1073 break;
1074 default:
1075 WARN_ON_ONCE(1);
1076 }
1077 }
1078
1079 /* inconsistent dump, trying again */
nsim_fib_dump_inconsistent(struct notifier_block * nb)1080 static void nsim_fib_dump_inconsistent(struct notifier_block *nb)
1081 {
1082 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1083 fib_nb);
1084 struct nsim_fib_rt *fib_rt, *fib_rt_tmp;
1085
1086 /* Flush the work to make sure there is no race with notifications. */
1087 flush_work(&data->fib_event_work);
1088
1089 /* The notifier block is still not registered, so we do not need to
1090 * take any locks here.
1091 */
1092 list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) {
1093 rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node,
1094 nsim_fib_rt_ht_params);
1095 nsim_fib_rt_free(fib_rt, data);
1096 }
1097
1098 atomic64_set(&data->ipv4.rules.num, 0ULL);
1099 atomic64_set(&data->ipv6.rules.num, 0ULL);
1100 }
1101
nsim_nexthop_create(struct nsim_fib_data * data,struct nh_notifier_info * info)1102 static struct nsim_nexthop *nsim_nexthop_create(struct nsim_fib_data *data,
1103 struct nh_notifier_info *info)
1104 {
1105 struct nsim_nexthop *nexthop;
1106 u64 occ = 0;
1107 int i;
1108
1109 nexthop = kzalloc(sizeof(*nexthop), GFP_KERNEL);
1110 if (!nexthop)
1111 return ERR_PTR(-ENOMEM);
1112
1113 nexthop->id = info->id;
1114
1115 /* Determine the number of nexthop entries the new nexthop will
1116 * occupy.
1117 */
1118
1119 switch (info->type) {
1120 case NH_NOTIFIER_INFO_TYPE_SINGLE:
1121 occ = 1;
1122 break;
1123 case NH_NOTIFIER_INFO_TYPE_GRP:
1124 for (i = 0; i < info->nh_grp->num_nh; i++)
1125 occ += info->nh_grp->nh_entries[i].weight;
1126 break;
1127 case NH_NOTIFIER_INFO_TYPE_RES_TABLE:
1128 occ = info->nh_res_table->num_nh_buckets;
1129 nexthop->is_resilient = true;
1130 break;
1131 default:
1132 NL_SET_ERR_MSG_MOD(info->extack, "Unsupported nexthop type");
1133 kfree(nexthop);
1134 return ERR_PTR(-EOPNOTSUPP);
1135 }
1136
1137 nexthop->occ = occ;
1138 return nexthop;
1139 }
1140
nsim_nexthop_destroy(struct nsim_nexthop * nexthop)1141 static void nsim_nexthop_destroy(struct nsim_nexthop *nexthop)
1142 {
1143 kfree(nexthop);
1144 }
1145
nsim_nexthop_account(struct nsim_fib_data * data,u64 occ,bool add,struct netlink_ext_ack * extack)1146 static int nsim_nexthop_account(struct nsim_fib_data *data, u64 occ,
1147 bool add, struct netlink_ext_ack *extack)
1148 {
1149 int i, err = 0;
1150
1151 if (add) {
1152 for (i = 0; i < occ; i++)
1153 if (!atomic64_add_unless(&data->nexthops.num, 1,
1154 data->nexthops.max)) {
1155 err = -ENOSPC;
1156 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported nexthops");
1157 goto err_num_decrease;
1158 }
1159 } else {
1160 if (WARN_ON(occ > atomic64_read(&data->nexthops.num)))
1161 return -EINVAL;
1162 atomic64_sub(occ, &data->nexthops.num);
1163 }
1164
1165 return err;
1166
1167 err_num_decrease:
1168 atomic64_sub(i, &data->nexthops.num);
1169 return err;
1170
1171 }
1172
nsim_nexthop_hw_flags_set(struct net * net,const struct nsim_nexthop * nexthop,bool trap)1173 static void nsim_nexthop_hw_flags_set(struct net *net,
1174 const struct nsim_nexthop *nexthop,
1175 bool trap)
1176 {
1177 int i;
1178
1179 nexthop_set_hw_flags(net, nexthop->id, false, trap);
1180
1181 if (!nexthop->is_resilient)
1182 return;
1183
1184 for (i = 0; i < nexthop->occ; i++)
1185 nexthop_bucket_set_hw_flags(net, nexthop->id, i, false, trap);
1186 }
1187
nsim_nexthop_add(struct nsim_fib_data * data,struct nsim_nexthop * nexthop,struct netlink_ext_ack * extack)1188 static int nsim_nexthop_add(struct nsim_fib_data *data,
1189 struct nsim_nexthop *nexthop,
1190 struct netlink_ext_ack *extack)
1191 {
1192 struct net *net = devlink_net(data->devlink);
1193 int err;
1194
1195 err = nsim_nexthop_account(data, nexthop->occ, true, extack);
1196 if (err)
1197 return err;
1198
1199 err = rhashtable_insert_fast(&data->nexthop_ht, &nexthop->ht_node,
1200 nsim_nexthop_ht_params);
1201 if (err) {
1202 NL_SET_ERR_MSG_MOD(extack, "Failed to insert nexthop");
1203 goto err_nexthop_dismiss;
1204 }
1205
1206 nsim_nexthop_hw_flags_set(net, nexthop, true);
1207
1208 return 0;
1209
1210 err_nexthop_dismiss:
1211 nsim_nexthop_account(data, nexthop->occ, false, extack);
1212 return err;
1213 }
1214
nsim_nexthop_replace(struct nsim_fib_data * data,struct nsim_nexthop * nexthop,struct nsim_nexthop * nexthop_old,struct netlink_ext_ack * extack)1215 static int nsim_nexthop_replace(struct nsim_fib_data *data,
1216 struct nsim_nexthop *nexthop,
1217 struct nsim_nexthop *nexthop_old,
1218 struct netlink_ext_ack *extack)
1219 {
1220 struct net *net = devlink_net(data->devlink);
1221 int err;
1222
1223 err = nsim_nexthop_account(data, nexthop->occ, true, extack);
1224 if (err)
1225 return err;
1226
1227 err = rhashtable_replace_fast(&data->nexthop_ht,
1228 &nexthop_old->ht_node, &nexthop->ht_node,
1229 nsim_nexthop_ht_params);
1230 if (err) {
1231 NL_SET_ERR_MSG_MOD(extack, "Failed to replace nexthop");
1232 goto err_nexthop_dismiss;
1233 }
1234
1235 nsim_nexthop_hw_flags_set(net, nexthop, true);
1236 nsim_nexthop_account(data, nexthop_old->occ, false, extack);
1237 nsim_nexthop_destroy(nexthop_old);
1238
1239 return 0;
1240
1241 err_nexthop_dismiss:
1242 nsim_nexthop_account(data, nexthop->occ, false, extack);
1243 return err;
1244 }
1245
nsim_nexthop_insert(struct nsim_fib_data * data,struct nh_notifier_info * info)1246 static int nsim_nexthop_insert(struct nsim_fib_data *data,
1247 struct nh_notifier_info *info)
1248 {
1249 struct nsim_nexthop *nexthop, *nexthop_old;
1250 int err;
1251
1252 nexthop = nsim_nexthop_create(data, info);
1253 if (IS_ERR(nexthop))
1254 return PTR_ERR(nexthop);
1255
1256 nexthop_old = rhashtable_lookup_fast(&data->nexthop_ht, &info->id,
1257 nsim_nexthop_ht_params);
1258 if (!nexthop_old)
1259 err = nsim_nexthop_add(data, nexthop, info->extack);
1260 else
1261 err = nsim_nexthop_replace(data, nexthop, nexthop_old,
1262 info->extack);
1263
1264 if (err)
1265 nsim_nexthop_destroy(nexthop);
1266
1267 return err;
1268 }
1269
nsim_nexthop_remove(struct nsim_fib_data * data,struct nh_notifier_info * info)1270 static void nsim_nexthop_remove(struct nsim_fib_data *data,
1271 struct nh_notifier_info *info)
1272 {
1273 struct nsim_nexthop *nexthop;
1274
1275 nexthop = rhashtable_lookup_fast(&data->nexthop_ht, &info->id,
1276 nsim_nexthop_ht_params);
1277 if (!nexthop)
1278 return;
1279
1280 rhashtable_remove_fast(&data->nexthop_ht, &nexthop->ht_node,
1281 nsim_nexthop_ht_params);
1282 nsim_nexthop_account(data, nexthop->occ, false, info->extack);
1283 nsim_nexthop_destroy(nexthop);
1284 }
1285
nsim_nexthop_res_table_pre_replace(struct nsim_fib_data * data,struct nh_notifier_info * info)1286 static int nsim_nexthop_res_table_pre_replace(struct nsim_fib_data *data,
1287 struct nh_notifier_info *info)
1288 {
1289 if (data->fail_res_nexthop_group_replace) {
1290 NL_SET_ERR_MSG_MOD(info->extack, "Failed to replace a resilient nexthop group");
1291 return -EINVAL;
1292 }
1293
1294 return 0;
1295 }
1296
nsim_nexthop_bucket_replace(struct nsim_fib_data * data,struct nh_notifier_info * info)1297 static int nsim_nexthop_bucket_replace(struct nsim_fib_data *data,
1298 struct nh_notifier_info *info)
1299 {
1300 if (data->fail_nexthop_bucket_replace) {
1301 NL_SET_ERR_MSG_MOD(info->extack, "Failed to replace nexthop bucket");
1302 return -EINVAL;
1303 }
1304
1305 nexthop_bucket_set_hw_flags(info->net, info->id,
1306 info->nh_res_bucket->bucket_index,
1307 false, true);
1308
1309 return 0;
1310 }
1311
nsim_nexthop_event_nb(struct notifier_block * nb,unsigned long event,void * ptr)1312 static int nsim_nexthop_event_nb(struct notifier_block *nb, unsigned long event,
1313 void *ptr)
1314 {
1315 struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
1316 nexthop_nb);
1317 struct nh_notifier_info *info = ptr;
1318 int err = 0;
1319
1320 mutex_lock(&data->nh_lock);
1321 switch (event) {
1322 case NEXTHOP_EVENT_REPLACE:
1323 err = nsim_nexthop_insert(data, info);
1324 break;
1325 case NEXTHOP_EVENT_DEL:
1326 nsim_nexthop_remove(data, info);
1327 break;
1328 case NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE:
1329 err = nsim_nexthop_res_table_pre_replace(data, info);
1330 break;
1331 case NEXTHOP_EVENT_BUCKET_REPLACE:
1332 err = nsim_nexthop_bucket_replace(data, info);
1333 break;
1334 default:
1335 break;
1336 }
1337
1338 mutex_unlock(&data->nh_lock);
1339 return notifier_from_errno(err);
1340 }
1341
nsim_nexthop_free(void * ptr,void * arg)1342 static void nsim_nexthop_free(void *ptr, void *arg)
1343 {
1344 struct nsim_nexthop *nexthop = ptr;
1345 struct nsim_fib_data *data = arg;
1346 struct net *net;
1347
1348 net = devlink_net(data->devlink);
1349 nsim_nexthop_hw_flags_set(net, nexthop, false);
1350 nsim_nexthop_account(data, nexthop->occ, false, NULL);
1351 nsim_nexthop_destroy(nexthop);
1352 }
1353
nsim_nexthop_bucket_activity_write(struct file * file,const char __user * user_buf,size_t size,loff_t * ppos)1354 static ssize_t nsim_nexthop_bucket_activity_write(struct file *file,
1355 const char __user *user_buf,
1356 size_t size, loff_t *ppos)
1357 {
1358 struct nsim_fib_data *data = file->private_data;
1359 struct net *net = devlink_net(data->devlink);
1360 struct nsim_nexthop *nexthop;
1361 unsigned long *activity;
1362 loff_t pos = *ppos;
1363 u16 bucket_index;
1364 char buf[128];
1365 int err = 0;
1366 u32 nhid;
1367
1368 if (pos != 0)
1369 return -EINVAL;
1370 if (size > sizeof(buf))
1371 return -EINVAL;
1372 if (copy_from_user(buf, user_buf, size))
1373 return -EFAULT;
1374 if (sscanf(buf, "%u %hu", &nhid, &bucket_index) != 2)
1375 return -EINVAL;
1376
1377 rtnl_lock();
1378
1379 nexthop = rhashtable_lookup_fast(&data->nexthop_ht, &nhid,
1380 nsim_nexthop_ht_params);
1381 if (!nexthop || !nexthop->is_resilient ||
1382 bucket_index >= nexthop->occ) {
1383 err = -EINVAL;
1384 goto out;
1385 }
1386
1387 activity = bitmap_zalloc(nexthop->occ, GFP_KERNEL);
1388 if (!activity) {
1389 err = -ENOMEM;
1390 goto out;
1391 }
1392
1393 bitmap_set(activity, bucket_index, 1);
1394 nexthop_res_grp_activity_update(net, nhid, nexthop->occ, activity);
1395 bitmap_free(activity);
1396
1397 out:
1398 rtnl_unlock();
1399
1400 *ppos = size;
1401 return err ?: size;
1402 }
1403
1404 static const struct file_operations nsim_nexthop_bucket_activity_fops = {
1405 .open = simple_open,
1406 .write = nsim_nexthop_bucket_activity_write,
1407 .llseek = no_llseek,
1408 .owner = THIS_MODULE,
1409 };
1410
nsim_fib_ipv4_resource_occ_get(void * priv)1411 static u64 nsim_fib_ipv4_resource_occ_get(void *priv)
1412 {
1413 struct nsim_fib_data *data = priv;
1414
1415 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB, false);
1416 }
1417
nsim_fib_ipv4_rules_res_occ_get(void * priv)1418 static u64 nsim_fib_ipv4_rules_res_occ_get(void *priv)
1419 {
1420 struct nsim_fib_data *data = priv;
1421
1422 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB_RULES, false);
1423 }
1424
nsim_fib_ipv6_resource_occ_get(void * priv)1425 static u64 nsim_fib_ipv6_resource_occ_get(void *priv)
1426 {
1427 struct nsim_fib_data *data = priv;
1428
1429 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB, false);
1430 }
1431
nsim_fib_ipv6_rules_res_occ_get(void * priv)1432 static u64 nsim_fib_ipv6_rules_res_occ_get(void *priv)
1433 {
1434 struct nsim_fib_data *data = priv;
1435
1436 return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB_RULES, false);
1437 }
1438
nsim_fib_nexthops_res_occ_get(void * priv)1439 static u64 nsim_fib_nexthops_res_occ_get(void *priv)
1440 {
1441 struct nsim_fib_data *data = priv;
1442
1443 return nsim_fib_get_val(data, NSIM_RESOURCE_NEXTHOPS, false);
1444 }
1445
nsim_fib_set_max_all(struct nsim_fib_data * data,struct devlink * devlink)1446 static void nsim_fib_set_max_all(struct nsim_fib_data *data,
1447 struct devlink *devlink)
1448 {
1449 static const enum nsim_resource_id res_ids[] = {
1450 NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES,
1451 NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES,
1452 NSIM_RESOURCE_NEXTHOPS,
1453 };
1454 int i;
1455
1456 for (i = 0; i < ARRAY_SIZE(res_ids); i++) {
1457 int err;
1458 u64 val;
1459
1460 err = devlink_resource_size_get(devlink, res_ids[i], &val);
1461 if (err)
1462 val = (u64) -1;
1463 nsim_fib_set_max(data, res_ids[i], val);
1464 }
1465 }
1466
nsim_fib_event_work(struct work_struct * work)1467 static void nsim_fib_event_work(struct work_struct *work)
1468 {
1469 struct nsim_fib_data *data = container_of(work, struct nsim_fib_data,
1470 fib_event_work);
1471 struct nsim_fib_event *fib_event, *next_fib_event;
1472
1473 LIST_HEAD(fib_event_queue);
1474
1475 spin_lock_bh(&data->fib_event_queue_lock);
1476 list_splice_init(&data->fib_event_queue, &fib_event_queue);
1477 spin_unlock_bh(&data->fib_event_queue_lock);
1478
1479 mutex_lock(&data->fib_lock);
1480 list_for_each_entry_safe(fib_event, next_fib_event, &fib_event_queue,
1481 list) {
1482 nsim_fib_event(fib_event);
1483 list_del(&fib_event->list);
1484 kfree(fib_event);
1485 cond_resched();
1486 }
1487 mutex_unlock(&data->fib_lock);
1488 }
1489
nsim_fib_flush_work(struct work_struct * work)1490 static void nsim_fib_flush_work(struct work_struct *work)
1491 {
1492 struct nsim_fib_data *data = container_of(work, struct nsim_fib_data,
1493 fib_flush_work);
1494 struct nsim_fib_rt *fib_rt, *fib_rt_tmp;
1495
1496 /* Process pending work. */
1497 flush_work(&data->fib_event_work);
1498
1499 mutex_lock(&data->fib_lock);
1500 list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) {
1501 rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node,
1502 nsim_fib_rt_ht_params);
1503 nsim_fib_rt_free(fib_rt, data);
1504 }
1505 mutex_unlock(&data->fib_lock);
1506 }
1507
1508 static int
nsim_fib_debugfs_init(struct nsim_fib_data * data,struct nsim_dev * nsim_dev)1509 nsim_fib_debugfs_init(struct nsim_fib_data *data, struct nsim_dev *nsim_dev)
1510 {
1511 data->ddir = debugfs_create_dir("fib", nsim_dev->ddir);
1512 if (IS_ERR(data->ddir))
1513 return PTR_ERR(data->ddir);
1514
1515 data->fail_route_offload = false;
1516 debugfs_create_bool("fail_route_offload", 0600, data->ddir,
1517 &data->fail_route_offload);
1518
1519 data->fail_res_nexthop_group_replace = false;
1520 debugfs_create_bool("fail_res_nexthop_group_replace", 0600, data->ddir,
1521 &data->fail_res_nexthop_group_replace);
1522
1523 data->fail_nexthop_bucket_replace = false;
1524 debugfs_create_bool("fail_nexthop_bucket_replace", 0600, data->ddir,
1525 &data->fail_nexthop_bucket_replace);
1526
1527 debugfs_create_file("nexthop_bucket_activity", 0200, data->ddir,
1528 data, &nsim_nexthop_bucket_activity_fops);
1529 return 0;
1530 }
1531
nsim_fib_debugfs_exit(struct nsim_fib_data * data)1532 static void nsim_fib_debugfs_exit(struct nsim_fib_data *data)
1533 {
1534 debugfs_remove_recursive(data->ddir);
1535 }
1536
nsim_fib_create(struct devlink * devlink,struct netlink_ext_ack * extack)1537 struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
1538 struct netlink_ext_ack *extack)
1539 {
1540 struct nsim_fib_data *data;
1541 struct nsim_dev *nsim_dev;
1542 int err;
1543
1544 data = kzalloc(sizeof(*data), GFP_KERNEL);
1545 if (!data)
1546 return ERR_PTR(-ENOMEM);
1547 data->devlink = devlink;
1548
1549 nsim_dev = devlink_priv(devlink);
1550 err = nsim_fib_debugfs_init(data, nsim_dev);
1551 if (err)
1552 goto err_data_free;
1553
1554 mutex_init(&data->nh_lock);
1555 err = rhashtable_init(&data->nexthop_ht, &nsim_nexthop_ht_params);
1556 if (err)
1557 goto err_debugfs_exit;
1558
1559 mutex_init(&data->fib_lock);
1560 INIT_LIST_HEAD(&data->fib_rt_list);
1561 err = rhashtable_init(&data->fib_rt_ht, &nsim_fib_rt_ht_params);
1562 if (err)
1563 goto err_rhashtable_nexthop_destroy;
1564
1565 INIT_WORK(&data->fib_event_work, nsim_fib_event_work);
1566 INIT_WORK(&data->fib_flush_work, nsim_fib_flush_work);
1567 INIT_LIST_HEAD(&data->fib_event_queue);
1568 spin_lock_init(&data->fib_event_queue_lock);
1569
1570 nsim_fib_set_max_all(data, devlink);
1571
1572 data->nexthop_nb.notifier_call = nsim_nexthop_event_nb;
1573 err = register_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb,
1574 extack);
1575 if (err) {
1576 pr_err("Failed to register nexthop notifier\n");
1577 goto err_rhashtable_fib_destroy;
1578 }
1579
1580 data->fib_nb.notifier_call = nsim_fib_event_nb;
1581 err = register_fib_notifier(devlink_net(devlink), &data->fib_nb,
1582 nsim_fib_dump_inconsistent, extack);
1583 if (err) {
1584 pr_err("Failed to register fib notifier\n");
1585 goto err_nexthop_nb_unregister;
1586 }
1587
1588 devlink_resource_occ_get_register(devlink,
1589 NSIM_RESOURCE_IPV4_FIB,
1590 nsim_fib_ipv4_resource_occ_get,
1591 data);
1592 devlink_resource_occ_get_register(devlink,
1593 NSIM_RESOURCE_IPV4_FIB_RULES,
1594 nsim_fib_ipv4_rules_res_occ_get,
1595 data);
1596 devlink_resource_occ_get_register(devlink,
1597 NSIM_RESOURCE_IPV6_FIB,
1598 nsim_fib_ipv6_resource_occ_get,
1599 data);
1600 devlink_resource_occ_get_register(devlink,
1601 NSIM_RESOURCE_IPV6_FIB_RULES,
1602 nsim_fib_ipv6_rules_res_occ_get,
1603 data);
1604 devlink_resource_occ_get_register(devlink,
1605 NSIM_RESOURCE_NEXTHOPS,
1606 nsim_fib_nexthops_res_occ_get,
1607 data);
1608 return data;
1609
1610 err_nexthop_nb_unregister:
1611 unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb);
1612 err_rhashtable_fib_destroy:
1613 cancel_work_sync(&data->fib_flush_work);
1614 flush_work(&data->fib_event_work);
1615 rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free,
1616 data);
1617 err_rhashtable_nexthop_destroy:
1618 rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
1619 data);
1620 mutex_destroy(&data->fib_lock);
1621 err_debugfs_exit:
1622 mutex_destroy(&data->nh_lock);
1623 nsim_fib_debugfs_exit(data);
1624 err_data_free:
1625 kfree(data);
1626 return ERR_PTR(err);
1627 }
1628
nsim_fib_destroy(struct devlink * devlink,struct nsim_fib_data * data)1629 void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data)
1630 {
1631 devlink_resource_occ_get_unregister(devlink,
1632 NSIM_RESOURCE_NEXTHOPS);
1633 devlink_resource_occ_get_unregister(devlink,
1634 NSIM_RESOURCE_IPV6_FIB_RULES);
1635 devlink_resource_occ_get_unregister(devlink,
1636 NSIM_RESOURCE_IPV6_FIB);
1637 devlink_resource_occ_get_unregister(devlink,
1638 NSIM_RESOURCE_IPV4_FIB_RULES);
1639 devlink_resource_occ_get_unregister(devlink,
1640 NSIM_RESOURCE_IPV4_FIB);
1641 unregister_fib_notifier(devlink_net(devlink), &data->fib_nb);
1642 unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb);
1643 cancel_work_sync(&data->fib_flush_work);
1644 flush_work(&data->fib_event_work);
1645 rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free,
1646 data);
1647 rhashtable_free_and_destroy(&data->nexthop_ht, nsim_nexthop_free,
1648 data);
1649 WARN_ON_ONCE(!list_empty(&data->fib_event_queue));
1650 WARN_ON_ONCE(!list_empty(&data->fib_rt_list));
1651 mutex_destroy(&data->fib_lock);
1652 mutex_destroy(&data->nh_lock);
1653 nsim_fib_debugfs_exit(data);
1654 kfree(data);
1655 }
1656