0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef __LINUX_NEXTHOP_H
0010 #define __LINUX_NEXTHOP_H
0011
0012 #include <linux/netdevice.h>
0013 #include <linux/notifier.h>
0014 #include <linux/route.h>
0015 #include <linux/types.h>
0016 #include <net/ip_fib.h>
0017 #include <net/ip6_fib.h>
0018 #include <net/netlink.h>
0019
0020 #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
0021
0022 struct nexthop;
0023
0024 struct nh_config {
0025 u32 nh_id;
0026
0027 u8 nh_family;
0028 u8 nh_protocol;
0029 u8 nh_blackhole;
0030 u8 nh_fdb;
0031 u32 nh_flags;
0032
0033 int nh_ifindex;
0034 struct net_device *dev;
0035
0036 union {
0037 __be32 ipv4;
0038 struct in6_addr ipv6;
0039 } gw;
0040
0041 struct nlattr *nh_grp;
0042 u16 nh_grp_type;
0043 u16 nh_grp_res_num_buckets;
0044 unsigned long nh_grp_res_idle_timer;
0045 unsigned long nh_grp_res_unbalanced_timer;
0046 bool nh_grp_res_has_num_buckets;
0047 bool nh_grp_res_has_idle_timer;
0048 bool nh_grp_res_has_unbalanced_timer;
0049
0050 struct nlattr *nh_encap;
0051 u16 nh_encap_type;
0052
0053 u32 nlflags;
0054 struct nl_info nlinfo;
0055 };
0056
0057 struct nh_info {
0058 struct hlist_node dev_hash;
0059 struct nexthop *nh_parent;
0060
0061 u8 family;
0062 bool reject_nh;
0063 bool fdb_nh;
0064
0065 union {
0066 struct fib_nh_common fib_nhc;
0067 struct fib_nh fib_nh;
0068 struct fib6_nh fib6_nh;
0069 };
0070 };
0071
0072 struct nh_res_bucket {
0073 struct nh_grp_entry __rcu *nh_entry;
0074 atomic_long_t used_time;
0075 unsigned long migrated_time;
0076 bool occupied;
0077 u8 nh_flags;
0078 };
0079
0080 struct nh_res_table {
0081 struct net *net;
0082 u32 nhg_id;
0083 struct delayed_work upkeep_dw;
0084
0085
0086
0087
0088 struct list_head uw_nh_entries;
0089 unsigned long unbalanced_since;
0090
0091 u32 idle_timer;
0092 u32 unbalanced_timer;
0093
0094 u16 num_nh_buckets;
0095 struct nh_res_bucket nh_buckets[];
0096 };
0097
0098 struct nh_grp_entry {
0099 struct nexthop *nh;
0100 u8 weight;
0101
0102 union {
0103 struct {
0104 atomic_t upper_bound;
0105 } hthr;
0106 struct {
0107
0108 struct list_head uw_nh_entry;
0109
0110 u16 count_buckets;
0111 u16 wants_buckets;
0112 } res;
0113 };
0114
0115 struct list_head nh_list;
0116 struct nexthop *nh_parent;
0117 };
0118
0119 struct nh_group {
0120 struct nh_group *spare;
0121 u16 num_nh;
0122 bool is_multipath;
0123 bool hash_threshold;
0124 bool resilient;
0125 bool fdb_nh;
0126 bool has_v4;
0127
0128 struct nh_res_table __rcu *res_table;
0129 struct nh_grp_entry nh_entries[];
0130 };
0131
0132 struct nexthop {
0133 struct rb_node rb_node;
0134 struct list_head fi_list;
0135 struct list_head f6i_list;
0136 struct list_head fdb_list;
0137 struct list_head grp_list;
0138 struct net *net;
0139
0140 u32 id;
0141
0142 u8 protocol;
0143 u8 nh_flags;
0144 bool is_group;
0145
0146 refcount_t refcnt;
0147 struct rcu_head rcu;
0148
0149 union {
0150 struct nh_info __rcu *nh_info;
0151 struct nh_group __rcu *nh_grp;
0152 };
0153 };
0154
0155 enum nexthop_event_type {
0156 NEXTHOP_EVENT_DEL,
0157 NEXTHOP_EVENT_REPLACE,
0158 NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE,
0159 NEXTHOP_EVENT_BUCKET_REPLACE,
0160 };
0161
0162 enum nh_notifier_info_type {
0163 NH_NOTIFIER_INFO_TYPE_SINGLE,
0164 NH_NOTIFIER_INFO_TYPE_GRP,
0165 NH_NOTIFIER_INFO_TYPE_RES_TABLE,
0166 NH_NOTIFIER_INFO_TYPE_RES_BUCKET,
0167 };
0168
0169 struct nh_notifier_single_info {
0170 struct net_device *dev;
0171 u8 gw_family;
0172 union {
0173 __be32 ipv4;
0174 struct in6_addr ipv6;
0175 };
0176 u8 is_reject:1,
0177 is_fdb:1,
0178 has_encap:1;
0179 };
0180
0181 struct nh_notifier_grp_entry_info {
0182 u8 weight;
0183 u32 id;
0184 struct nh_notifier_single_info nh;
0185 };
0186
0187 struct nh_notifier_grp_info {
0188 u16 num_nh;
0189 bool is_fdb;
0190 struct nh_notifier_grp_entry_info nh_entries[];
0191 };
0192
0193 struct nh_notifier_res_bucket_info {
0194 u16 bucket_index;
0195 unsigned int idle_timer_ms;
0196 bool force;
0197 struct nh_notifier_single_info old_nh;
0198 struct nh_notifier_single_info new_nh;
0199 };
0200
0201 struct nh_notifier_res_table_info {
0202 u16 num_nh_buckets;
0203 struct nh_notifier_single_info nhs[];
0204 };
0205
0206 struct nh_notifier_info {
0207 struct net *net;
0208 struct netlink_ext_ack *extack;
0209 u32 id;
0210 enum nh_notifier_info_type type;
0211 union {
0212 struct nh_notifier_single_info *nh;
0213 struct nh_notifier_grp_info *nh_grp;
0214 struct nh_notifier_res_table_info *nh_res_table;
0215 struct nh_notifier_res_bucket_info *nh_res_bucket;
0216 };
0217 };
0218
0219 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
0220 struct netlink_ext_ack *extack);
0221 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
0222 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
0223 void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index,
0224 bool offload, bool trap);
0225 void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets,
0226 unsigned long *activity);
0227
0228
0229 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
0230 void nexthop_free_rcu(struct rcu_head *head);
0231
0232 static inline bool nexthop_get(struct nexthop *nh)
0233 {
0234 return refcount_inc_not_zero(&nh->refcnt);
0235 }
0236
0237 static inline void nexthop_put(struct nexthop *nh)
0238 {
0239 if (refcount_dec_and_test(&nh->refcnt))
0240 call_rcu(&nh->rcu, nexthop_free_rcu);
0241 }
0242
0243 static inline bool nexthop_cmp(const struct nexthop *nh1,
0244 const struct nexthop *nh2)
0245 {
0246 return nh1 == nh2;
0247 }
0248
0249 static inline bool nexthop_is_fdb(const struct nexthop *nh)
0250 {
0251 if (nh->is_group) {
0252 const struct nh_group *nh_grp;
0253
0254 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0255 return nh_grp->fdb_nh;
0256 } else {
0257 const struct nh_info *nhi;
0258
0259 nhi = rcu_dereference_rtnl(nh->nh_info);
0260 return nhi->fdb_nh;
0261 }
0262 }
0263
0264 static inline bool nexthop_has_v4(const struct nexthop *nh)
0265 {
0266 if (nh->is_group) {
0267 struct nh_group *nh_grp;
0268
0269 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0270 return nh_grp->has_v4;
0271 }
0272 return false;
0273 }
0274
0275 static inline bool nexthop_is_multipath(const struct nexthop *nh)
0276 {
0277 if (nh->is_group) {
0278 struct nh_group *nh_grp;
0279
0280 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0281 return nh_grp->is_multipath;
0282 }
0283 return false;
0284 }
0285
0286 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
0287
0288 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
0289 {
0290 unsigned int rc = 1;
0291
0292 if (nh->is_group) {
0293 struct nh_group *nh_grp;
0294
0295 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0296 if (nh_grp->is_multipath)
0297 rc = nh_grp->num_nh;
0298 }
0299
0300 return rc;
0301 }
0302
0303 static inline
0304 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
0305 {
0306
0307
0308
0309 if (nhsel >= nhg->num_nh)
0310 return NULL;
0311
0312 return nhg->nh_entries[nhsel].nh;
0313 }
0314
0315 static inline
0316 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
0317 u8 rt_family)
0318 {
0319 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
0320 int i;
0321
0322 for (i = 0; i < nhg->num_nh; i++) {
0323 struct nexthop *nhe = nhg->nh_entries[i].nh;
0324 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
0325 struct fib_nh_common *nhc = &nhi->fib_nhc;
0326 int weight = nhg->nh_entries[i].weight;
0327
0328 if (fib_add_nexthop(skb, nhc, weight, rt_family, 0) < 0)
0329 return -EMSGSIZE;
0330 }
0331
0332 return 0;
0333 }
0334
0335
0336 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
0337 {
0338 const struct nh_info *nhi;
0339
0340 if (nh->is_group) {
0341 struct nh_group *nh_grp;
0342
0343 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0344 if (nh_grp->num_nh > 1)
0345 return false;
0346
0347 nh = nh_grp->nh_entries[0].nh;
0348 }
0349
0350 nhi = rcu_dereference_rtnl(nh->nh_info);
0351 return nhi->reject_nh;
0352 }
0353
0354 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
0355 {
0356 struct nh_info *nhi;
0357 struct nexthop *nh;
0358
0359 nh = nexthop_select_path(res->fi->nh, hash);
0360 nhi = rcu_dereference(nh->nh_info);
0361 res->nhc = &nhi->fib_nhc;
0362 }
0363
0364
0365 static inline
0366 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
0367 {
0368 struct nh_info *nhi;
0369
0370 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
0371 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
0372
0373 if (nh->is_group) {
0374 struct nh_group *nh_grp;
0375
0376 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0377 if (nh_grp->is_multipath) {
0378 nh = nexthop_mpath_select(nh_grp, nhsel);
0379 if (!nh)
0380 return NULL;
0381 }
0382 }
0383
0384 nhi = rcu_dereference_rtnl(nh->nh_info);
0385 return &nhi->fib_nhc;
0386 }
0387
0388
0389 static inline
0390 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
0391 int fib_flags,
0392 const struct flowi4 *flp,
0393 int *nhsel)
0394 {
0395 struct nh_info *nhi;
0396
0397 if (nh->is_group) {
0398 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
0399 int i;
0400
0401 for (i = 0; i < nhg->num_nh; i++) {
0402 struct nexthop *nhe = nhg->nh_entries[i].nh;
0403
0404 nhi = rcu_dereference(nhe->nh_info);
0405 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
0406 *nhsel = i;
0407 return &nhi->fib_nhc;
0408 }
0409 }
0410 } else {
0411 nhi = rcu_dereference(nh->nh_info);
0412 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
0413 *nhsel = 0;
0414 return &nhi->fib_nhc;
0415 }
0416 }
0417
0418 return NULL;
0419 }
0420
0421 static inline bool nexthop_uses_dev(const struct nexthop *nh,
0422 const struct net_device *dev)
0423 {
0424 struct nh_info *nhi;
0425
0426 if (nh->is_group) {
0427 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
0428 int i;
0429
0430 for (i = 0; i < nhg->num_nh; i++) {
0431 struct nexthop *nhe = nhg->nh_entries[i].nh;
0432
0433 nhi = rcu_dereference(nhe->nh_info);
0434 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
0435 return true;
0436 }
0437 } else {
0438 nhi = rcu_dereference(nh->nh_info);
0439 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
0440 return true;
0441 }
0442
0443 return false;
0444 }
0445
0446 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
0447 {
0448 if (unlikely(fi->nh))
0449 return nexthop_num_path(fi->nh);
0450
0451 return fi->fib_nhs;
0452 }
0453
0454 int fib_check_nexthop(struct nexthop *nh, u8 scope,
0455 struct netlink_ext_ack *extack);
0456
0457 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
0458 {
0459 if (unlikely(fi->nh))
0460 return nexthop_fib_nhc(fi->nh, nhsel);
0461
0462 return &fi->fib_nh[nhsel].nh_common;
0463 }
0464
0465
0466 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
0467 {
0468 WARN_ON(fi->nh);
0469
0470 return &fi->fib_nh[nhsel];
0471 }
0472
0473
0474
0475
0476 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
0477 struct netlink_ext_ack *extack);
0478
0479
0480 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
0481 {
0482 struct nh_info *nhi;
0483
0484 if (nh->is_group) {
0485 struct nh_group *nh_grp;
0486
0487 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0488 nh = nexthop_mpath_select(nh_grp, 0);
0489 if (!nh)
0490 return NULL;
0491 }
0492
0493 nhi = rcu_dereference_rtnl(nh->nh_info);
0494 if (nhi->family == AF_INET6)
0495 return &nhi->fib6_nh;
0496
0497 return NULL;
0498 }
0499
0500
0501
0502
0503 static inline struct fib6_nh *nexthop_fib6_nh_bh(struct nexthop *nh)
0504 {
0505 struct nh_info *nhi;
0506
0507 if (nh->is_group) {
0508 struct nh_group *nh_grp;
0509
0510 nh_grp = rcu_dereference_bh_rtnl(nh->nh_grp);
0511 nh = nexthop_mpath_select(nh_grp, 0);
0512 if (!nh)
0513 return NULL;
0514 }
0515
0516 nhi = rcu_dereference_bh_rtnl(nh->nh_info);
0517 if (nhi->family == AF_INET6)
0518 return &nhi->fib6_nh;
0519
0520 return NULL;
0521 }
0522
0523 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
0524 {
0525 struct fib6_nh *fib6_nh;
0526
0527 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
0528 return fib6_nh->fib_nh_dev;
0529 }
0530
0531 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
0532 {
0533 struct nexthop *nh = res->f6i->nh;
0534 struct nh_info *nhi;
0535
0536 nh = nexthop_select_path(nh, hash);
0537
0538 nhi = rcu_dereference_rtnl(nh->nh_info);
0539 if (nhi->reject_nh) {
0540 res->fib6_type = RTN_BLACKHOLE;
0541 res->fib6_flags |= RTF_REJECT;
0542 res->nh = nexthop_fib6_nh(nh);
0543 } else {
0544 res->nh = &nhi->fib6_nh;
0545 }
0546 }
0547
0548 int nexthop_for_each_fib6_nh(struct nexthop *nh,
0549 int (*cb)(struct fib6_nh *nh, void *arg),
0550 void *arg);
0551
0552 static inline int nexthop_get_family(struct nexthop *nh)
0553 {
0554 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
0555
0556 return nhi->family;
0557 }
0558
0559 static inline
0560 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
0561 {
0562 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
0563
0564 return &nhi->fib_nhc;
0565 }
0566
0567 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
0568 int hash)
0569 {
0570 struct nh_info *nhi;
0571 struct nexthop *nhp;
0572
0573 nhp = nexthop_select_path(nh, hash);
0574 if (unlikely(!nhp))
0575 return NULL;
0576 nhi = rcu_dereference(nhp->nh_info);
0577 return &nhi->fib_nhc;
0578 }
0579 #endif