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 enum mlxsw_sp_l3proto {
11 	MLXSW_SP_L3_PROTO_IPV4,
12 	MLXSW_SP_L3_PROTO_IPV6,
13 #define MLXSW_SP_L3_PROTO_MAX	(MLXSW_SP_L3_PROTO_IPV6 + 1)
14 };
15 
16 union mlxsw_sp_l3addr {
17 	__be32 addr4;
18 	struct in6_addr addr6;
19 };
20 
21 struct mlxsw_sp_rif_ipip_lb;
22 struct mlxsw_sp_rif_ipip_lb_config {
23 	enum mlxsw_reg_ritr_loopback_ipip_type lb_ipipt;
24 	u32 okey;
25 	enum mlxsw_sp_l3proto ul_protocol; /* Underlay. */
26 	union mlxsw_sp_l3addr saddr;
27 };
28 
29 enum mlxsw_sp_rif_counter_dir {
30 	MLXSW_SP_RIF_COUNTER_INGRESS,
31 	MLXSW_SP_RIF_COUNTER_EGRESS,
32 };
33 
34 struct mlxsw_sp_neigh_entry;
35 struct mlxsw_sp_nexthop;
36 struct mlxsw_sp_ipip_entry;
37 
38 struct mlxsw_sp_rif *mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
39 					      const struct net_device *dev);
40 struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
41 					   u16 rif_index);
42 u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif);
43 u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *rif);
44 u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *rif);
45 u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev);
46 int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif);
47 u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp);
48 const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif);
49 struct mlxsw_sp_fid *mlxsw_sp_rif_fid(const struct mlxsw_sp_rif *rif);
50 int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
51 				   struct mlxsw_sp_rif *rif,
52 				   enum mlxsw_sp_rif_counter_dir dir,
53 				   u64 *cnt);
54 void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
55 			       struct mlxsw_sp_rif *rif,
56 			       enum mlxsw_sp_rif_counter_dir dir);
57 int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
58 			       struct mlxsw_sp_rif *rif,
59 			       enum mlxsw_sp_rif_counter_dir dir);
60 struct mlxsw_sp_neigh_entry *
61 mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif,
62 			struct mlxsw_sp_neigh_entry *neigh_entry);
63 int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry);
64 unsigned char *
65 mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry);
66 u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry);
67 struct in6_addr *
68 mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry);
69 
70 #define mlxsw_sp_rif_neigh_for_each(neigh_entry, rif)				\
71 	for (neigh_entry = mlxsw_sp_rif_neigh_next(rif, NULL); neigh_entry;	\
72 	     neigh_entry = mlxsw_sp_rif_neigh_next(rif, neigh_entry))
73 int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp,
74 			       struct mlxsw_sp_neigh_entry *neigh_entry,
75 			       u64 *p_counter);
76 void
77 mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp,
78 				    struct mlxsw_sp_neigh_entry *neigh_entry,
79 				    bool adding);
80 bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry);
81 int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp,
82 					struct mlxsw_sp_ipip_entry *ipip_entry,
83 					bool recreate_loopback,
84 					bool keep_encap,
85 					bool update_nexthops,
86 					struct netlink_ext_ack *extack);
87 void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp,
88 				       struct mlxsw_sp_ipip_entry *ipip_entry);
89 bool
90 mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp,
91 				     enum mlxsw_sp_l3proto ul_proto,
92 				     union mlxsw_sp_l3addr saddr,
93 				     u32 ul_tb_id,
94 				     const struct mlxsw_sp_ipip_entry *except);
95 struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router,
96 					       struct mlxsw_sp_nexthop *nh);
97 bool mlxsw_sp_nexthop_offload(struct mlxsw_sp_nexthop *nh);
98 unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh);
99 int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index,
100 			     u32 *p_adj_size, u32 *p_adj_hash_index);
101 struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh);
102 bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh);
103 #define mlxsw_sp_nexthop_for_each(nh, router)				\
104 	for (nh = mlxsw_sp_nexthop_next(router, NULL); nh;		\
105 	     nh = mlxsw_sp_nexthop_next(router, nh))
106 int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp,
107 				 struct mlxsw_sp_nexthop *nh, u64 *p_counter);
108 int mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
109 			    struct mlxsw_sp_nexthop *nh);
110 void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp,
111 				    struct mlxsw_sp_nexthop *nh);
112 void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp,
113 				   struct mlxsw_sp_nexthop *nh);
114 
mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr * addr1,const union mlxsw_sp_l3addr * addr2)115 static inline bool mlxsw_sp_l3addr_eq(const union mlxsw_sp_l3addr *addr1,
116 				      const union mlxsw_sp_l3addr *addr2)
117 {
118 	return !memcmp(addr1, addr2, sizeof(*addr1));
119 }
120 
121 #endif /* _MLXSW_ROUTER_H_*/
122