1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/spectrum_mr.h
3  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the names of the copyright holders nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _MLXSW_SPECTRUM_MCROUTER_H
36 #define _MLXSW_SPECTRUM_MCROUTER_H
37 
38 #include <linux/mroute.h>
39 #include <linux/mroute6.h>
40 #include "spectrum_router.h"
41 #include "spectrum.h"
42 
43 enum mlxsw_sp_mr_route_action {
44 	MLXSW_SP_MR_ROUTE_ACTION_FORWARD,
45 	MLXSW_SP_MR_ROUTE_ACTION_TRAP,
46 	MLXSW_SP_MR_ROUTE_ACTION_TRAP_AND_FORWARD,
47 };
48 
49 struct mlxsw_sp_mr_route_key {
50 	int vrid;
51 	enum mlxsw_sp_l3proto proto;
52 	union mlxsw_sp_l3addr group;
53 	union mlxsw_sp_l3addr group_mask;
54 	union mlxsw_sp_l3addr source;
55 	union mlxsw_sp_l3addr source_mask;
56 };
57 
58 struct mlxsw_sp_mr_route_info {
59 	enum mlxsw_sp_mr_route_action route_action;
60 	u16 irif_index;
61 	u16 *erif_indices;
62 	size_t erif_num;
63 	u16 min_mtu;
64 };
65 
66 struct mlxsw_sp_mr_route_params {
67 	struct mlxsw_sp_mr_route_key key;
68 	struct mlxsw_sp_mr_route_info value;
69 	enum mlxsw_sp_mr_route_prio prio;
70 };
71 
72 struct mlxsw_sp_mr_ops {
73 	int priv_size;
74 	int route_priv_size;
75 	int (*init)(struct mlxsw_sp *mlxsw_sp, void *priv);
76 	int (*route_create)(struct mlxsw_sp *mlxsw_sp, void *priv,
77 			    void *route_priv,
78 			    struct mlxsw_sp_mr_route_params *route_params);
79 	int (*route_update)(struct mlxsw_sp *mlxsw_sp, void *route_priv,
80 			    struct mlxsw_sp_mr_route_info *route_info);
81 	int (*route_stats)(struct mlxsw_sp *mlxsw_sp, void *route_priv,
82 			   u64 *packets, u64 *bytes);
83 	int (*route_action_update)(struct mlxsw_sp *mlxsw_sp, void *route_priv,
84 				   enum mlxsw_sp_mr_route_action route_action);
85 	int (*route_min_mtu_update)(struct mlxsw_sp *mlxsw_sp, void *route_priv,
86 				    u16 min_mtu);
87 	int (*route_irif_update)(struct mlxsw_sp *mlxsw_sp, void *route_priv,
88 				 u16 irif_index);
89 	int (*route_erif_add)(struct mlxsw_sp *mlxsw_sp, void *route_priv,
90 			      u16 erif_index);
91 	int (*route_erif_del)(struct mlxsw_sp *mlxsw_sp, void *route_priv,
92 			      u16 erif_index);
93 	void (*route_destroy)(struct mlxsw_sp *mlxsw_sp, void *priv,
94 			      void *route_priv);
95 	void (*fini)(struct mlxsw_sp *mlxsw_sp, void *priv);
96 };
97 
98 struct mlxsw_sp_mr;
99 struct mlxsw_sp_mr_table;
100 
101 int mlxsw_sp_mr_init(struct mlxsw_sp *mlxsw_sp,
102 		     const struct mlxsw_sp_mr_ops *mr_ops);
103 void mlxsw_sp_mr_fini(struct mlxsw_sp *mlxsw_sp);
104 int mlxsw_sp_mr_route_add(struct mlxsw_sp_mr_table *mr_table,
105 			  struct mr_mfc *mfc, bool replace);
106 void mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table *mr_table,
107 			   struct mr_mfc *mfc);
108 int mlxsw_sp_mr_vif_add(struct mlxsw_sp_mr_table *mr_table,
109 			struct net_device *dev, vifi_t vif_index,
110 			unsigned long vif_flags,
111 			const struct mlxsw_sp_rif *rif);
112 void mlxsw_sp_mr_vif_del(struct mlxsw_sp_mr_table *mr_table, vifi_t vif_index);
113 int mlxsw_sp_mr_rif_add(struct mlxsw_sp_mr_table *mr_table,
114 			const struct mlxsw_sp_rif *rif);
115 void mlxsw_sp_mr_rif_del(struct mlxsw_sp_mr_table *mr_table,
116 			 const struct mlxsw_sp_rif *rif);
117 void mlxsw_sp_mr_rif_mtu_update(struct mlxsw_sp_mr_table *mr_table,
118 				const struct mlxsw_sp_rif *rif, int mtu);
119 struct mlxsw_sp_mr_table *mlxsw_sp_mr_table_create(struct mlxsw_sp *mlxsw_sp,
120 						   u32 tb_id,
121 						   enum mlxsw_sp_l3proto proto);
122 void mlxsw_sp_mr_table_destroy(struct mlxsw_sp_mr_table *mr_table);
123 void mlxsw_sp_mr_table_flush(struct mlxsw_sp_mr_table *mr_table);
124 bool mlxsw_sp_mr_table_empty(const struct mlxsw_sp_mr_table *mr_table);
125 
126 #endif
127