19948a064SJiri Pirko /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */ 29948a064SJiri Pirko /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */ 3e0c0afd8SArkadi Sharshevsky 4e0c0afd8SArkadi Sharshevsky #ifndef _MLXSW_ROUTER_H_ 5e0c0afd8SArkadi Sharshevsky #define _MLXSW_ROUTER_H_ 6e0c0afd8SArkadi Sharshevsky 7e0c0afd8SArkadi Sharshevsky #include "spectrum.h" 86ddb7426SPetr Machata #include "reg.h" 9e0c0afd8SArkadi Sharshevsky 108e18d85eSIdo Schimmel struct mlxsw_sp_router_nve_decap { 118e18d85eSIdo Schimmel u32 ul_tb_id; 128e18d85eSIdo Schimmel u32 tunnel_index; 138e18d85eSIdo Schimmel enum mlxsw_sp_l3proto ul_proto; 148e18d85eSIdo Schimmel union mlxsw_sp_l3addr ul_sip; 158e18d85eSIdo Schimmel u8 valid:1; 168e18d85eSIdo Schimmel }; 178e18d85eSIdo Schimmel 18*91d20d71SJiri Pirko struct mlxsw_sp_fib_entry_op_ctx { 19*91d20d71SJiri Pirko u8 bulk_ok:1, /* Indicate to the low-level op it is ok to bulk 20*91d20d71SJiri Pirko * the actual entry with the one that is the next 21*91d20d71SJiri Pirko * in queue. 22*91d20d71SJiri Pirko */ 23*91d20d71SJiri Pirko initialized:1; /* Bit that the low-level op sets in case 24*91d20d71SJiri Pirko * the context priv is initialized. 25*91d20d71SJiri Pirko */ 26*91d20d71SJiri Pirko unsigned long ll_priv[]; 27*91d20d71SJiri Pirko }; 28*91d20d71SJiri Pirko 29*91d20d71SJiri Pirko static inline void 30*91d20d71SJiri Pirko mlxsw_sp_fib_entry_op_ctx_clear(struct mlxsw_sp_fib_entry_op_ctx *op_ctx) 31*91d20d71SJiri Pirko { 32*91d20d71SJiri Pirko memset(op_ctx, 0, sizeof(*op_ctx)); 33*91d20d71SJiri Pirko } 34*91d20d71SJiri Pirko 352a60c460SIdo Schimmel struct mlxsw_sp_router { 362a60c460SIdo Schimmel struct mlxsw_sp *mlxsw_sp; 372a60c460SIdo Schimmel struct mlxsw_sp_rif **rifs; 382a60c460SIdo Schimmel struct mlxsw_sp_vr *vrs; 392a60c460SIdo Schimmel struct rhashtable neigh_ht; 402a60c460SIdo Schimmel struct rhashtable nexthop_group_ht; 412a60c460SIdo Schimmel struct rhashtable nexthop_ht; 422a60c460SIdo Schimmel struct list_head nexthop_list; 432a60c460SIdo Schimmel struct { 442a60c460SIdo Schimmel /* One tree for each protocol: IPv4 and IPv6 */ 452a60c460SIdo Schimmel struct mlxsw_sp_lpm_tree *proto_trees[2]; 462a60c460SIdo Schimmel struct mlxsw_sp_lpm_tree *trees; 472a60c460SIdo Schimmel unsigned int tree_count; 482a60c460SIdo Schimmel } lpm; 492a60c460SIdo Schimmel struct { 502a60c460SIdo Schimmel struct delayed_work dw; 512a60c460SIdo Schimmel unsigned long interval; /* ms */ 522a60c460SIdo Schimmel } neighs_update; 532a60c460SIdo Schimmel struct delayed_work nexthop_probe_dw; 542a60c460SIdo Schimmel #define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */ 552a60c460SIdo Schimmel struct list_head nexthop_neighs_list; 562a60c460SIdo Schimmel struct list_head ipip_list; 572a60c460SIdo Schimmel bool aborted; 582a60c460SIdo Schimmel struct notifier_block fib_nb; 592a60c460SIdo Schimmel struct notifier_block netevent_nb; 602a60c460SIdo Schimmel struct notifier_block inetaddr_nb; 612a60c460SIdo Schimmel struct notifier_block inet6addr_nb; 622a60c460SIdo Schimmel const struct mlxsw_sp_rif_ops **rif_ops_arr; 632a60c460SIdo Schimmel const struct mlxsw_sp_ipip_ops **ipip_ops_arr; 642a60c460SIdo Schimmel u32 adj_discard_index; 652a60c460SIdo Schimmel bool adj_discard_index_valid; 668e18d85eSIdo Schimmel struct mlxsw_sp_router_nve_decap nve_decap_config; 6720bf5d82SIdo Schimmel struct mutex lock; /* Protects shared router resources */ 68c1b290d5SJiri Pirko struct work_struct fib_event_work; 69c1b290d5SJiri Pirko struct list_head fib_event_queue; 70c1b290d5SJiri Pirko spinlock_t fib_event_queue_lock; /* Protects fib event queue list */ 71803be108SJiri Pirko /* One set of ops for each protocol: IPv4 and IPv6 */ 72803be108SJiri Pirko const struct mlxsw_sp_router_ll_ops *proto_ll_ops[MLXSW_SP_L3_PROTO_MAX]; 73*91d20d71SJiri Pirko struct mlxsw_sp_fib_entry_op_ctx *ll_op_ctx; 74803be108SJiri Pirko }; 75803be108SJiri Pirko 762d5bd7a1SJiri Pirko enum mlxsw_sp_fib_entry_op { 772d5bd7a1SJiri Pirko MLXSW_SP_FIB_ENTRY_OP_WRITE, 782d5bd7a1SJiri Pirko MLXSW_SP_FIB_ENTRY_OP_DELETE, 792d5bd7a1SJiri Pirko }; 802d5bd7a1SJiri Pirko 81803be108SJiri Pirko /* Low-level router ops. Basically this is to handle the different 82803be108SJiri Pirko * register sets to work with ordinary and XM trees and FIB entries. 83803be108SJiri Pirko */ 84803be108SJiri Pirko struct mlxsw_sp_router_ll_ops { 85803be108SJiri Pirko int (*ralta_write)(struct mlxsw_sp *mlxsw_sp, char *xralta_pl); 86803be108SJiri Pirko int (*ralst_write)(struct mlxsw_sp *mlxsw_sp, char *xralst_pl); 87803be108SJiri Pirko int (*raltb_write)(struct mlxsw_sp *mlxsw_sp, char *xraltb_pl); 88*91d20d71SJiri Pirko size_t fib_entry_op_ctx_size; 897f5c4090SJiri Pirko void (*fib_entry_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 907f5c4090SJiri Pirko enum mlxsw_sp_l3proto proto, enum mlxsw_sp_fib_entry_op op, 917f5c4090SJiri Pirko u16 virtual_router, u8 prefix_len, unsigned char *addr); 927f5c4090SJiri Pirko void (*fib_entry_act_remote_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 937f5c4090SJiri Pirko enum mlxsw_reg_ralue_trap_action trap_action, 947f5c4090SJiri Pirko u16 trap_id, u32 adjacency_index, u16 ecmp_size); 957f5c4090SJiri Pirko void (*fib_entry_act_local_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 967f5c4090SJiri Pirko enum mlxsw_reg_ralue_trap_action trap_action, 977f5c4090SJiri Pirko u16 trap_id, u16 local_erif); 987f5c4090SJiri Pirko void (*fib_entry_act_ip2me_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx); 997f5c4090SJiri Pirko void (*fib_entry_act_ip2me_tun_pack)(struct mlxsw_sp_fib_entry_op_ctx *op_ctx, 1007f5c4090SJiri Pirko u32 tunnel_ptr); 1017f5c4090SJiri Pirko int (*fib_entry_commit)(struct mlxsw_sp *mlxsw_sp, 1027f5c4090SJiri Pirko struct mlxsw_sp_fib_entry_op_ctx *op_ctx); 1032a60c460SIdo Schimmel }; 1042a60c460SIdo Schimmel 10592107cfbSPetr Machata struct mlxsw_sp_rif_ipip_lb; 1066ddb7426SPetr Machata struct mlxsw_sp_rif_ipip_lb_config { 1076ddb7426SPetr Machata enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt; 1086ddb7426SPetr Machata u32 okey; 1096ddb7426SPetr Machata enum mlxsw_sp_l3proto ul_protocol; /* Underlay. */ 1106ddb7426SPetr Machata union mlxsw_sp_l3addr saddr; 1116ddb7426SPetr Machata }; 1126ddb7426SPetr Machata 113e0c0afd8SArkadi Sharshevsky enum mlxsw_sp_rif_counter_dir { 114e0c0afd8SArkadi Sharshevsky MLXSW_SP_RIF_COUNTER_INGRESS, 115e0c0afd8SArkadi Sharshevsky MLXSW_SP_RIF_COUNTER_EGRESS, 116e0c0afd8SArkadi Sharshevsky }; 117e0c0afd8SArkadi Sharshevsky 118f17cc84dSArkadi Sharshevsky struct mlxsw_sp_neigh_entry; 119c556cd28SArkadi Sharshevsky struct mlxsw_sp_nexthop; 1200c5f1cd5SPetr Machata struct mlxsw_sp_ipip_entry; 121f17cc84dSArkadi Sharshevsky 1225f9efffbSIdo Schimmel struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp, 1235f9efffbSIdo Schimmel u16 rif_index); 124fd1b9d41SArkadi Sharshevsky u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif); 12592107cfbSPetr Machata u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *rif); 12692107cfbSPetr Machata u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *rif); 127311596f5SNir Dotan u16 mlxsw_sp_ipip_lb_ul_rif_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif); 1284cf04f3fSPetr Machata u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev); 129fd1b9d41SArkadi Sharshevsky int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif); 13091e4d59aSYotam Gigi const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif); 131e0c0afd8SArkadi Sharshevsky int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp, 132e0c0afd8SArkadi Sharshevsky struct mlxsw_sp_rif *rif, 133e0c0afd8SArkadi Sharshevsky enum mlxsw_sp_rif_counter_dir dir, 134e0c0afd8SArkadi Sharshevsky u64 *cnt); 135e0c0afd8SArkadi Sharshevsky void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp, 136e0c0afd8SArkadi Sharshevsky struct mlxsw_sp_rif *rif, 137e0c0afd8SArkadi Sharshevsky enum mlxsw_sp_rif_counter_dir dir); 138e0c0afd8SArkadi Sharshevsky int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp, 139e0c0afd8SArkadi Sharshevsky struct mlxsw_sp_rif *rif, 140e0c0afd8SArkadi Sharshevsky enum mlxsw_sp_rif_counter_dir dir); 141f17cc84dSArkadi Sharshevsky struct mlxsw_sp_neigh_entry * 142f17cc84dSArkadi Sharshevsky mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif, 143f17cc84dSArkadi Sharshevsky struct mlxsw_sp_neigh_entry *neigh_entry); 144f17cc84dSArkadi Sharshevsky int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry); 145f17cc84dSArkadi Sharshevsky unsigned char * 146f17cc84dSArkadi Sharshevsky mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry); 147f17cc84dSArkadi Sharshevsky u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry); 1480250768cSArkadi Sharshevsky struct in6_addr * 1490250768cSArkadi Sharshevsky mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry); 150f17cc84dSArkadi Sharshevsky 151f17cc84dSArkadi Sharshevsky #define mlxsw_sp_rif_neigh_for_each(neigh_entry, rif) \ 152f17cc84dSArkadi Sharshevsky for (neigh_entry = mlxsw_sp_rif_neigh_next(rif, NULL); neigh_entry; \ 153f17cc84dSArkadi Sharshevsky neigh_entry = mlxsw_sp_rif_neigh_next(rif, neigh_entry)) 1547cfcbc75SArkadi Sharshevsky int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp, 1557cfcbc75SArkadi Sharshevsky struct mlxsw_sp_neigh_entry *neigh_entry, 1567cfcbc75SArkadi Sharshevsky u64 *p_counter); 157a481d713SArkadi Sharshevsky void 158a481d713SArkadi Sharshevsky mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp, 159a481d713SArkadi Sharshevsky struct mlxsw_sp_neigh_entry *neigh_entry, 160a481d713SArkadi Sharshevsky bool adding); 1611d1056d8SArkadi Sharshevsky bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry); 1620c5f1cd5SPetr Machata int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp, 1630c5f1cd5SPetr Machata struct mlxsw_sp_ipip_entry *ipip_entry, 1640c5f1cd5SPetr Machata bool recreate_loopback, 1650c5f1cd5SPetr Machata bool keep_encap, 1660c5f1cd5SPetr Machata bool update_nexthops, 1670c5f1cd5SPetr Machata struct netlink_ext_ack *extack); 168af641713SPetr Machata void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp, 169af641713SPetr Machata struct mlxsw_sp_ipip_entry *ipip_entry); 170af641713SPetr Machata bool 171af641713SPetr Machata mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp, 172af641713SPetr Machata enum mlxsw_sp_l3proto ul_proto, 173af641713SPetr Machata union mlxsw_sp_l3addr saddr, 174af641713SPetr Machata u32 ul_tb_id, 175af641713SPetr Machata const struct mlxsw_sp_ipip_entry *except); 176c556cd28SArkadi Sharshevsky struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router, 177c556cd28SArkadi Sharshevsky struct mlxsw_sp_nexthop *nh); 178c556cd28SArkadi Sharshevsky bool mlxsw_sp_nexthop_offload(struct mlxsw_sp_nexthop *nh); 179c556cd28SArkadi Sharshevsky unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh); 180c556cd28SArkadi Sharshevsky int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index, 181e69cd9d7SIdo Schimmel u32 *p_adj_size, u32 *p_adj_hash_index); 182c556cd28SArkadi Sharshevsky struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh); 183c556cd28SArkadi Sharshevsky bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh); 184c556cd28SArkadi Sharshevsky #define mlxsw_sp_nexthop_for_each(nh, router) \ 185c556cd28SArkadi Sharshevsky for (nh = mlxsw_sp_nexthop_next(router, NULL); nh; \ 186c556cd28SArkadi Sharshevsky nh = mlxsw_sp_nexthop_next(router, nh)) 187a5390278SArkadi Sharshevsky int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp, 188a5390278SArkadi Sharshevsky struct mlxsw_sp_nexthop *nh, u64 *p_counter); 189427e652aSArkadi Sharshevsky int mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index, 190427e652aSArkadi Sharshevsky struct mlxsw_sp_nexthop *nh); 191427e652aSArkadi Sharshevsky void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp, 192427e652aSArkadi Sharshevsky struct mlxsw_sp_nexthop *nh); 193427e652aSArkadi Sharshevsky void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp, 194427e652aSArkadi Sharshevsky struct mlxsw_sp_nexthop *nh); 195e0c0afd8SArkadi Sharshevsky 1964cf04f3fSPetr Machata static inline bool mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr *addr1, 1974cf04f3fSPetr Machata const union mlxsw_sp_l3addr *addr2) 1984cf04f3fSPetr Machata { 1994cf04f3fSPetr Machata return !memcmp(addr1, addr2, sizeof(*addr1)); 2004cf04f3fSPetr Machata } 2014cf04f3fSPetr Machata 2024a44ee67SAmit Cohen int mlxsw_sp_ipip_ecn_encap_init(struct mlxsw_sp *mlxsw_sp); 2034a44ee67SAmit Cohen int mlxsw_sp_ipip_ecn_decap_init(struct mlxsw_sp *mlxsw_sp); 2044a44ee67SAmit Cohen 205e0c0afd8SArkadi Sharshevsky #endif /* _MLXSW_ROUTER_H_*/ 206