xref: /linux/drivers/net/ethernet/mellanox/mlxsw/spectrum_router.h (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 /* gen_pool_alloc() returns 0 when allocation fails, so use an offset */
19 #define MLXSW_SP_ROUTER_GENALLOC_OFFSET 0x100
20 
21 struct mlxsw_sp_router {
22 	struct mlxsw_sp *mlxsw_sp;
23 	struct rhashtable crif_ht;
24 	struct gen_pool *rifs_table;
25 	struct mlxsw_sp_rif **rifs;
26 	struct idr rif_mac_profiles_idr;
27 	atomic_t rif_mac_profiles_count;
28 	atomic_t rifs_count;
29 	u8 max_rif_mac_profile;
30 	struct mlxsw_sp_vr *vrs;
31 	struct rhashtable neigh_ht;
32 	struct rhashtable nexthop_group_ht;
33 	struct rhashtable nexthop_ht;
34 	struct list_head nexthop_list;
35 	struct {
36 		/* One tree for each protocol: IPv4 and IPv6 */
37 		struct mlxsw_sp_lpm_tree *proto_trees[2];
38 		struct mlxsw_sp_lpm_tree *trees;
39 		unsigned int tree_count;
40 	} lpm;
41 	struct {
42 		struct delayed_work dw;
43 		unsigned long interval;	/* ms */
44 		atomic_t neigh_count;
45 	} neighs_update;
46 	struct delayed_work nexthop_probe_dw;
47 #define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */
48 	struct list_head nexthop_neighs_list;
49 	struct list_head ipip_list;
50 	struct notifier_block nexthop_nb;
51 	struct notifier_block fib_nb;
52 	struct notifier_block netevent_nb;
53 	struct notifier_block inetaddr_nb;
54 	struct notifier_block inet6addr_nb;
55 	struct notifier_block netdevice_nb;
56 	struct notifier_block inetaddr_valid_nb;
57 	struct notifier_block inet6addr_valid_nb;
58 	const struct mlxsw_sp_rif_ops **rif_ops_arr;
59 	const struct mlxsw_sp_ipip_ops **ipip_ops_arr;
60 	struct mlxsw_sp_router_nve_decap nve_decap_config;
61 	struct mutex lock; /* Protects shared router resources */
62 	struct mlxsw_sp_fib_entry_op_ctx *ll_op_ctx;
63 	struct mlxsw_sp_crif *lb_crif;
64 	const struct mlxsw_sp_adj_grp_size_range *adj_grp_size_ranges;
65 	size_t adj_grp_size_ranges_count;
66 	struct delayed_work nh_grp_activity_dw;
67 	struct list_head nh_res_grp_list;
68 	bool inc_parsing_depth;
69 	refcount_t num_groups;
70 	u32 adj_trap_index;
71 };
72 
73 struct mlxsw_sp_rif_ipip_lb;
74 struct mlxsw_sp_rif_ipip_lb_config {
75 	enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt;
76 	u32 okey;
77 	enum mlxsw_sp_l3proto ul_protocol; /* Underlay. */
78 	union mlxsw_sp_l3addr saddr;
79 };
80 
81 enum mlxsw_sp_rif_counter_dir {
82 	MLXSW_SP_RIF_COUNTER_INGRESS,
83 	MLXSW_SP_RIF_COUNTER_EGRESS,
84 };
85 
86 struct mlxsw_sp_neigh_entry;
87 struct mlxsw_sp_nexthop;
88 struct mlxsw_sp_ipip_entry;
89 
90 struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
91 					   u16 rif_index);
92 u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *rif);
93 u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *rif);
94 u16 mlxsw_sp_ipip_lb_ul_rif_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif);
95 u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev);
96 int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif);
97 bool mlxsw_sp_rif_has_dev(const struct mlxsw_sp_rif *rif);
98 bool mlxsw_sp_rif_dev_is(const struct mlxsw_sp_rif *rif,
99 			 const struct net_device *dev);
100 int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
101 				   struct mlxsw_sp_rif *rif,
102 				   enum mlxsw_sp_rif_counter_dir dir,
103 				   u64 *cnt);
104 void mlxsw_sp_rif_counter_free(struct mlxsw_sp_rif *rif,
105 			       enum mlxsw_sp_rif_counter_dir dir);
106 int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp_rif *rif,
107 			       enum mlxsw_sp_rif_counter_dir dir);
108 struct mlxsw_sp_neigh_entry *
109 mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif,
110 			struct mlxsw_sp_neigh_entry *neigh_entry);
111 int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry);
112 unsigned char *
113 mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry);
114 u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry);
115 struct in6_addr *
116 mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry);
117 
118 #define mlxsw_sp_rif_neigh_for_each(neigh_entry, rif)				\
119 	for (neigh_entry = mlxsw_sp_rif_neigh_next(rif, NULL); neigh_entry;	\
120 	     neigh_entry = mlxsw_sp_rif_neigh_next(rif, neigh_entry))
121 int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp,
122 			       struct mlxsw_sp_neigh_entry *neigh_entry,
123 			       u64 *p_counter);
124 void
125 mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp,
126 				    struct mlxsw_sp_neigh_entry *neigh_entry,
127 				    bool adding);
128 bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry);
129 int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp,
130 					struct mlxsw_sp_ipip_entry *ipip_entry,
131 					bool recreate_loopback,
132 					bool keep_encap,
133 					bool update_nexthops,
134 					struct netlink_ext_ack *extack);
135 void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp,
136 				       struct mlxsw_sp_ipip_entry *ipip_entry);
137 bool
138 mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp,
139 				     enum mlxsw_sp_l3proto ul_proto,
140 				     union mlxsw_sp_l3addr saddr,
141 				     u32 ul_tb_id,
142 				     const struct mlxsw_sp_ipip_entry *except);
143 struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router,
144 					       struct mlxsw_sp_nexthop *nh);
145 bool mlxsw_sp_nexthop_is_forward(const struct mlxsw_sp_nexthop *nh);
146 unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh);
147 int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index,
148 			     u32 *p_adj_size, u32 *p_adj_hash_index);
149 struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh);
150 bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh);
151 #define mlxsw_sp_nexthop_for_each(nh, router)				\
152 	for (nh = mlxsw_sp_nexthop_next(router, NULL); nh;		\
153 	     nh = mlxsw_sp_nexthop_next(router, nh))
154 int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp,
155 				 struct mlxsw_sp_nexthop *nh, u64 *p_counter);
156 int mlxsw_sp_nexthop_eth_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
157 				struct mlxsw_sp_nexthop *nh, bool force,
158 				char *ratr_pl);
159 int mlxsw_sp_nexthop_counter_enable(struct mlxsw_sp *mlxsw_sp,
160 				    struct mlxsw_sp_nexthop *nh);
161 void mlxsw_sp_nexthop_counter_disable(struct mlxsw_sp *mlxsw_sp,
162 				      struct mlxsw_sp_nexthop *nh);
163 
mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr * addr1,const union mlxsw_sp_l3addr * addr2)164 static inline bool mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr *addr1,
165 				      const union mlxsw_sp_l3addr *addr2)
166 {
167 	return !memcmp(addr1, addr2, sizeof(*addr1));
168 }
169 
170 int mlxsw_sp_ipip_ecn_encap_init(struct mlxsw_sp *mlxsw_sp);
171 int mlxsw_sp_ipip_ecn_decap_init(struct mlxsw_sp *mlxsw_sp);
172 struct net_device *
173 mlxsw_sp_ipip_netdev_ul_dev_get(const struct net_device *ol_dev);
174 int mlxsw_sp_router_bridge_vlan_add(struct mlxsw_sp *mlxsw_sp,
175 				    struct net_device *dev,
176 				    u16 new_vid, bool is_pvid,
177 				    struct netlink_ext_ack *extack);
178 int mlxsw_sp_router_port_join_lag(struct mlxsw_sp_port *mlxsw_sp_port,
179 				  struct net_device *lag_dev,
180 				  struct netlink_ext_ack *extack);
181 void mlxsw_sp_router_port_leave_lag(struct mlxsw_sp_port *mlxsw_sp_port,
182 				    struct net_device *lag_dev);
183 int mlxsw_sp_netdevice_enslavement_replay(struct mlxsw_sp *mlxsw_sp,
184 					  struct net_device *upper_dev,
185 					  struct netlink_ext_ack *extack);
186 void mlxsw_sp_netdevice_deslavement_replay(struct mlxsw_sp *mlxsw_sp,
187 					   struct net_device *dev);
188 
189 #endif /* _MLXSW_ROUTER_H_*/
190