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_fib_entry_op_ctx { 19 u8 bulk_ok:1, /* Indicate to the low-level op it is ok to bulk 20 * the actual entry with the one that is the next 21 * in queue. 22 */ 23 initialized:1; /* Bit that the low-level op sets in case 24 * the context priv is initialized. 25 */ 26 struct list_head fib_entry_priv_list; 27 unsigned long ll_priv[]; 28 }; 29 30 static inline void 31 mlxsw_sp_fib_entry_op_ctx_clear(struct mlxsw_sp_fib_entry_op_ctx *op_ctx) 32 { 33 WARN_ON_ONCE(!list_empty(&op_ctx->fib_entry_priv_list)); 34 memset(op_ctx, 0, sizeof(*op_ctx)); 35 INIT_LIST_HEAD(&op_ctx->fib_entry_priv_list); 36 } 37 38 struct mlxsw_sp_router { 39 struct mlxsw_sp *mlxsw_sp; 40 struct mlxsw_sp_rif **rifs; 41 struct mlxsw_sp_vr *vrs; 42 struct rhashtable neigh_ht; 43 struct rhashtable nexthop_group_ht; 44 struct rhashtable nexthop_ht; 45 struct list_head nexthop_list; 46 struct { 47 /* One tree for each protocol: IPv4 and IPv6 */ 48 struct mlxsw_sp_lpm_tree *proto_trees[2]; 49 struct mlxsw_sp_lpm_tree *trees; 50 unsigned int tree_count; 51 } lpm; 52 struct { 53 struct delayed_work dw; 54 unsigned long interval; /* ms */ 55 } neighs_update; 56 struct delayed_work nexthop_probe_dw; 57 #define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */ 58 struct list_head nexthop_neighs_list; 59 struct list_head ipip_list; 60 bool aborted; 61 struct notifier_block nexthop_nb; 62 struct notifier_block fib_nb; 63 struct notifier_block netevent_nb; 64 struct notifier_block inetaddr_nb; 65 struct notifier_block inet6addr_nb; 66 const struct mlxsw_sp_rif_ops **rif_ops_arr; 67 const struct mlxsw_sp_ipip_ops **ipip_ops_arr; 68 u32 adj_discard_index; 69 bool adj_discard_index_valid; 70 struct mlxsw_sp_router_nve_decap nve_decap_config; 71 struct mutex lock; /* Protects shared router resources */ 72 struct work_struct fib_event_work; 73 struct list_head fib_event_queue; 74 spinlock_t fib_event_queue_lock; /* Protects fib event queue list */ 75 /* One set of ops for each protocol: IPv4 and IPv6 */ 76 const struct mlxsw_sp_router_ll_ops *proto_ll_ops[MLXSW_SP_L3_PROTO_MAX]; 77 struct mlxsw_sp_fib_entry_op_ctx *ll_op_ctx; 78 }; 79 80 struct mlxsw_sp_fib_entry_priv { 81 refcount_t refcnt; 82 struct list_head list; /* Member in op_ctx->fib_entry_priv_list */ 83 unsigned long priv[]; 84 }; 85 86 enum mlxsw_sp_fib_entry_op { 87 MLXSW_SP_FIB_ENTRY_OP_WRITE, 88 MLXSW_SP_FIB_ENTRY_OP_UPDATE, 89 MLXSW_SP_FIB_ENTRY_OP_DELETE, 90 }; 91 92 /* Low-level router ops. Basically this is to handle the different 93 * register sets to work with ordinary and XM trees and FIB entries. 94 */ 95 struct mlxsw_sp_router_ll_ops { 96 int (*ralta_write)(struct mlxsw_sp *mlxsw_sp, char *xralta_pl); 97 int (*ralst_write)(struct mlxsw_sp *mlxsw_sp, char *xralst_pl); 98 int (*raltb_write)(struct mlxsw_sp *mlxsw_sp, char *xraltb_pl); 99 size_t fib_entry_op_ctx_size; 100 size_t fib_entry_priv_size; 101 void (*fib_entry_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 102 enum mlxsw_sp_l3proto proto, enum mlxsw_sp_fib_entry_op op, 103 u16 virtual_router, u8 prefix_len, unsigned char *addr, 104 struct mlxsw_sp_fib_entry_priv *priv); 105 void (*fib_entry_act_remote_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 106 enum mlxsw_reg_ralue_trap_action trap_action, 107 u16 trap_id, u32 adjacency_index, u16 ecmp_size); 108 void (*fib_entry_act_local_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 109 enum mlxsw_reg_ralue_trap_action trap_action, 110 u16 trap_id, u16 local_erif); 111 void (*fib_entry_act_ip2me_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx); 112 void (*fib_entry_act_ip2me_tun_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 113 u32 tunnel_ptr); 114 int (*fib_entry_commit)(struct mlxsw_sp *mlxsw_sp, 115 struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 116 bool *postponed_for_bulk); 117 bool (*fib_entry_is_committed)(struct mlxsw_sp_fib_entry_priv *priv); 118 }; 119 120 int mlxsw_sp_fib_entry_commit(struct mlxsw_sp *mlxsw_sp, 121 struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 122 const struct mlxsw_sp_router_ll_ops *ll_ops); 123 124 struct mlxsw_sp_rif_ipip_lb; 125 struct mlxsw_sp_rif_ipip_lb_config { 126 enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt; 127 u32 okey; 128 enum mlxsw_sp_l3proto ul_protocol; /* Underlay. */ 129 union mlxsw_sp_l3addr saddr; 130 }; 131 132 enum mlxsw_sp_rif_counter_dir { 133 MLXSW_SP_RIF_COUNTER_INGRESS, 134 MLXSW_SP_RIF_COUNTER_EGRESS, 135 }; 136 137 struct mlxsw_sp_neigh_entry; 138 struct mlxsw_sp_nexthop; 139 struct mlxsw_sp_ipip_entry; 140 141 struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp, 142 u16 rif_index); 143 u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif); 144 u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *rif); 145 u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *rif); 146 u16 mlxsw_sp_ipip_lb_ul_rif_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif); 147 u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev); 148 int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif); 149 const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif); 150 int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp, 151 struct mlxsw_sp_rif *rif, 152 enum mlxsw_sp_rif_counter_dir dir, 153 u64 *cnt); 154 void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp, 155 struct mlxsw_sp_rif *rif, 156 enum mlxsw_sp_rif_counter_dir dir); 157 int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp, 158 struct mlxsw_sp_rif *rif, 159 enum mlxsw_sp_rif_counter_dir dir); 160 struct mlxsw_sp_neigh_entry * 161 mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif, 162 struct mlxsw_sp_neigh_entry *neigh_entry); 163 int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry); 164 unsigned char * 165 mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry); 166 u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry); 167 struct in6_addr * 168 mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry); 169 170 #define mlxsw_sp_rif_neigh_for_each(neigh_entry, rif) \ 171 for (neigh_entry = mlxsw_sp_rif_neigh_next(rif, NULL); neigh_entry; \ 172 neigh_entry = mlxsw_sp_rif_neigh_next(rif, neigh_entry)) 173 int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp, 174 struct mlxsw_sp_neigh_entry *neigh_entry, 175 u64 *p_counter); 176 void 177 mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp, 178 struct mlxsw_sp_neigh_entry *neigh_entry, 179 bool adding); 180 bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry); 181 int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp, 182 struct mlxsw_sp_ipip_entry *ipip_entry, 183 bool recreate_loopback, 184 bool keep_encap, 185 bool update_nexthops, 186 struct netlink_ext_ack *extack); 187 void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp, 188 struct mlxsw_sp_ipip_entry *ipip_entry); 189 bool 190 mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp, 191 enum mlxsw_sp_l3proto ul_proto, 192 union mlxsw_sp_l3addr saddr, 193 u32 ul_tb_id, 194 const struct mlxsw_sp_ipip_entry *except); 195 struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router, 196 struct mlxsw_sp_nexthop *nh); 197 bool mlxsw_sp_nexthop_offload(struct mlxsw_sp_nexthop *nh); 198 unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh); 199 int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index, 200 u32 *p_adj_size, u32 *p_adj_hash_index); 201 struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh); 202 bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh); 203 #define mlxsw_sp_nexthop_for_each(nh, router) \ 204 for (nh = mlxsw_sp_nexthop_next(router, NULL); nh; \ 205 nh = mlxsw_sp_nexthop_next(router, nh)) 206 int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp, 207 struct mlxsw_sp_nexthop *nh, u64 *p_counter); 208 int mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index, 209 struct mlxsw_sp_nexthop *nh); 210 void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp, 211 struct mlxsw_sp_nexthop *nh); 212 void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp, 213 struct mlxsw_sp_nexthop *nh); 214 215 static inline bool mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr *addr1, 216 const union mlxsw_sp_l3addr *addr2) 217 { 218 return !memcmp(addr1, addr2, sizeof(*addr1)); 219 } 220 221 int mlxsw_sp_ipip_ecn_encap_init(struct mlxsw_sp *mlxsw_sp); 222 int mlxsw_sp_ipip_ecn_decap_init(struct mlxsw_sp *mlxsw_sp); 223 224 #endif /* _MLXSW_ROUTER_H_*/ 225