1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
3 
4 #ifndef _MLXSW_ROUTER_H_
5 #define _MLXSW_ROUTER_H_
6 
7 #include "spectrum.h"
8 #include "reg.h"
9 
10 struct mlxsw_sp_router_nve_decap {
11 	u32 ul_tb_id;
12 	u32 tunnel_index;
13 	enum mlxsw_sp_l3proto ul_proto;
14 	union mlxsw_sp_l3addr ul_sip;
15 	u8 valid:1;
16 };
17 
18 struct mlxsw_sp_router {
19 	struct mlxsw_sp *mlxsw_sp;
20 	struct mlxsw_sp_rif **rifs;
21 	struct idr rif_mac_profiles_idr;
22 	atomic_t rif_mac_profiles_count;
23 	atomic_t rifs_count;
24 	u8 max_rif_mac_profile;
25 	struct mlxsw_sp_vr *vrs;
26 	struct rhashtable neigh_ht;
27 	struct rhashtable nexthop_group_ht;
28 	struct rhashtable nexthop_ht;
29 	struct list_head nexthop_list;
30 	struct {
31 		/* One tree for each protocol: IPv4 and IPv6 */
32 		struct mlxsw_sp_lpm_tree *proto_trees[2];
33 		struct mlxsw_sp_lpm_tree *trees;
34 		unsigned int tree_count;
35 	} lpm;
36 	struct {
37 		struct delayed_work dw;
38 		unsigned long interval;	/* ms */
39 		atomic_t neigh_count;
40 	} neighs_update;
41 	struct delayed_work nexthop_probe_dw;
42 #define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */
43 	struct list_head nexthop_neighs_list;
44 	struct list_head ipip_list;
45 	struct notifier_block nexthop_nb;
46 	struct notifier_block fib_nb;
47 	struct notifier_block netevent_nb;
48 	struct notifier_block inetaddr_nb;
49 	struct notifier_block inet6addr_nb;
50 	struct notifier_block netdevice_nb;
51 	const struct mlxsw_sp_rif_ops **rif_ops_arr;
52 	const struct mlxsw_sp_ipip_ops **ipip_ops_arr;
53 	struct mlxsw_sp_router_nve_decap nve_decap_config;
54 	struct mutex lock; /* Protects shared router resources */
55 	struct mlxsw_sp_fib_entry_op_ctx *ll_op_ctx;
56 	u16 lb_rif_index;
57 	const struct mlxsw_sp_adj_grp_size_range *adj_grp_size_ranges;
58 	size_t adj_grp_size_ranges_count;
59 	struct delayed_work nh_grp_activity_dw;
60 	struct list_head nh_res_grp_list;
61 	bool inc_parsing_depth;
62 	refcount_t num_groups;
63 	u32 adj_trap_index;
64 };
65 
66 struct mlxsw_sp_rif_ipip_lb;
67 struct mlxsw_sp_rif_ipip_lb_config {
68 	enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt;
69 	u32 okey;
70 	enum mlxsw_sp_l3proto ul_protocol; /* Underlay. */
71 	union mlxsw_sp_l3addr saddr;
72 };
73 
74 enum mlxsw_sp_rif_counter_dir {
75 	MLXSW_SP_RIF_COUNTER_INGRESS,
76 	MLXSW_SP_RIF_COUNTER_EGRESS,
77 };
78 
79 struct mlxsw_sp_neigh_entry;
80 struct mlxsw_sp_nexthop;
81 struct mlxsw_sp_ipip_entry;
82 
83 struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
84 					   u16 rif_index);
85 u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *rif);
86 u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *rif);
87 u16 mlxsw_sp_ipip_lb_ul_rif_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif);
88 u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev);
89 int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif);
90 const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif);
91 int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
92 				   struct mlxsw_sp_rif *rif,
93 				   enum mlxsw_sp_rif_counter_dir dir,
94 				   u64 *cnt);
95 void mlxsw_sp_rif_counter_free(struct mlxsw_sp_rif *rif,
96 			       enum mlxsw_sp_rif_counter_dir dir);
97 int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp_rif *rif,
98 			       enum mlxsw_sp_rif_counter_dir dir);
99 struct mlxsw_sp_neigh_entry *
100 mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif,
101 			struct mlxsw_sp_neigh_entry *neigh_entry);
102 int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry);
103 unsigned char *
104 mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry);
105 u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry);
106 struct in6_addr *
107 mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry);
108 
109 #define mlxsw_sp_rif_neigh_for_each(neigh_entry, rif)				\
110 	for (neigh_entry = mlxsw_sp_rif_neigh_next(rif, NULL); neigh_entry;	\
111 	     neigh_entry = mlxsw_sp_rif_neigh_next(rif, neigh_entry))
112 int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp,
113 			       struct mlxsw_sp_neigh_entry *neigh_entry,
114 			       u64 *p_counter);
115 void
116 mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp,
117 				    struct mlxsw_sp_neigh_entry *neigh_entry,
118 				    bool adding);
119 bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry);
120 int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp,
121 					struct mlxsw_sp_ipip_entry *ipip_entry,
122 					bool recreate_loopback,
123 					bool keep_encap,
124 					bool update_nexthops,
125 					struct netlink_ext_ack *extack);
126 void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp,
127 				       struct mlxsw_sp_ipip_entry *ipip_entry);
128 bool
129 mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp,
130 				     enum mlxsw_sp_l3proto ul_proto,
131 				     union mlxsw_sp_l3addr saddr,
132 				     u32 ul_tb_id,
133 				     const struct mlxsw_sp_ipip_entry *except);
134 struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router,
135 					       struct mlxsw_sp_nexthop *nh);
136 bool mlxsw_sp_nexthop_is_forward(const struct mlxsw_sp_nexthop *nh);
137 unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh);
138 int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index,
139 			     u32 *p_adj_size, u32 *p_adj_hash_index);
140 struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh);
141 bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh);
142 #define mlxsw_sp_nexthop_for_each(nh, router)				\
143 	for (nh = mlxsw_sp_nexthop_next(router, NULL); nh;		\
144 	     nh = mlxsw_sp_nexthop_next(router, nh))
145 int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp,
146 				 struct mlxsw_sp_nexthop *nh, u64 *p_counter);
147 int mlxsw_sp_nexthop_eth_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
148 				struct mlxsw_sp_nexthop *nh, bool force,
149 				char *ratr_pl);
150 void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp,
151 				    struct mlxsw_sp_nexthop *nh);
152 void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp,
153 				   struct mlxsw_sp_nexthop *nh);
154 
mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr * addr1,const union mlxsw_sp_l3addr * addr2)155 static inline bool mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr *addr1,
156 				      const union mlxsw_sp_l3addr *addr2)
157 {
158 	return !memcmp(addr1, addr2, sizeof(*addr1));
159 }
160 
161 int mlxsw_sp_ipip_ecn_encap_init(struct mlxsw_sp *mlxsw_sp);
162 int mlxsw_sp_ipip_ecn_decap_init(struct mlxsw_sp *mlxsw_sp);
163 struct net_device *
164 mlxsw_sp_ipip_netdev_ul_dev_get(const struct net_device *ol_dev);
165 
166 #endif /* _MLXSW_ROUTER_H_*/
167