1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2018 Mellanox Technologies. */
3 
4 #ifndef __MLX5_EN_TC_CT_H__
5 #define __MLX5_EN_TC_CT_H__
6 
7 #include <net/pkt_cls.h>
8 #include <linux/mlx5/fs.h>
9 #include <net/tc_act/tc_ct.h>
10 
11 #include "en.h"
12 
13 struct mlx5_flow_attr;
14 struct mlx5e_tc_mod_hdr_acts;
15 struct mlx5_rep_uplink_priv;
16 struct mlx5e_tc_flow;
17 struct mlx5e_priv;
18 
19 struct mlx5_fs_chains;
20 struct mlx5_tc_ct_priv;
21 struct mlx5_ct_flow;
22 
23 struct nf_flowtable;
24 
25 struct mlx5_ct_attr {
26 	u16 zone;
27 	u16 ct_action;
28 	struct mlx5_ct_flow *ct_flow;
29 	struct nf_flowtable *nf_ft;
30 	u32 ct_labels_id;
31 };
32 
33 #define zone_to_reg_ct {\
34 	.mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_2,\
35 	.moffset = 0,\
36 	.mlen = 2,\
37 	.soffset = MLX5_BYTE_OFF(fte_match_param,\
38 				 misc_parameters_2.metadata_reg_c_2) + 2,\
39 }
40 
41 #define ctstate_to_reg_ct {\
42 	.mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_2,\
43 	.moffset = 2,\
44 	.mlen = 2,\
45 	.soffset = MLX5_BYTE_OFF(fte_match_param,\
46 				 misc_parameters_2.metadata_reg_c_2),\
47 }
48 
49 #define mark_to_reg_ct {\
50 	.mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_3,\
51 	.moffset = 0,\
52 	.mlen = 4,\
53 	.soffset = MLX5_BYTE_OFF(fte_match_param,\
54 				 misc_parameters_2.metadata_reg_c_3),\
55 }
56 
57 #define labels_to_reg_ct {\
58 	.mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_4,\
59 	.moffset = 0,\
60 	.mlen = 4,\
61 	.soffset = MLX5_BYTE_OFF(fte_match_param,\
62 				 misc_parameters_2.metadata_reg_c_4),\
63 }
64 
65 #define fteid_to_reg_ct {\
66 	.mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_5,\
67 	.moffset = 0,\
68 	.mlen = 4,\
69 	.soffset = MLX5_BYTE_OFF(fte_match_param,\
70 				 misc_parameters_2.metadata_reg_c_5),\
71 }
72 
73 #define zone_restore_to_reg_ct {\
74 	.mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_1,\
75 	.moffset = 0,\
76 	.mlen = (ESW_ZONE_ID_BITS / 8),\
77 	.soffset = MLX5_BYTE_OFF(fte_match_param,\
78 				 misc_parameters_2.metadata_reg_c_1) + 3,\
79 }
80 
81 #define nic_zone_restore_to_reg_ct {\
82 	.mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_B,\
83 	.moffset = 2,\
84 	.mlen = (ESW_ZONE_ID_BITS / 8),\
85 }
86 
87 #define REG_MAPPING_MLEN(reg) (mlx5e_tc_attr_to_reg_mappings[reg].mlen)
88 #define REG_MAPPING_MOFFSET(reg) (mlx5e_tc_attr_to_reg_mappings[reg].moffset)
89 #define REG_MAPPING_SHIFT(reg) (REG_MAPPING_MOFFSET(reg) * 8)
90 
91 #if IS_ENABLED(CONFIG_MLX5_TC_CT)
92 
93 struct mlx5_tc_ct_priv *
94 mlx5_tc_ct_init(struct mlx5e_priv *priv, struct mlx5_fs_chains *chains,
95 		struct mod_hdr_tbl *mod_hdr,
96 		enum mlx5_flow_namespace_type ns_type);
97 void
98 mlx5_tc_ct_clean(struct mlx5_tc_ct_priv *ct_priv);
99 
100 void
101 mlx5_tc_ct_match_del(struct mlx5_tc_ct_priv *priv, struct mlx5_ct_attr *ct_attr);
102 
103 int
104 mlx5_tc_ct_match_add(struct mlx5_tc_ct_priv *priv,
105 		     struct mlx5_flow_spec *spec,
106 		     struct flow_cls_offload *f,
107 		     struct mlx5_ct_attr *ct_attr,
108 		     struct netlink_ext_ack *extack);
109 int mlx5_tc_ct_add_no_trk_match(struct mlx5_flow_spec *spec);
110 int
111 mlx5_tc_ct_parse_action(struct mlx5_tc_ct_priv *priv,
112 			struct mlx5_flow_attr *attr,
113 			const struct flow_action_entry *act,
114 			struct netlink_ext_ack *extack);
115 
116 struct mlx5_flow_handle *
117 mlx5_tc_ct_flow_offload(struct mlx5_tc_ct_priv *priv,
118 			struct mlx5e_tc_flow *flow,
119 			struct mlx5_flow_spec *spec,
120 			struct mlx5_flow_attr *attr,
121 			struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts);
122 void
123 mlx5_tc_ct_delete_flow(struct mlx5_tc_ct_priv *priv,
124 		       struct mlx5e_tc_flow *flow,
125 		       struct mlx5_flow_attr *attr);
126 
127 bool
128 mlx5e_tc_ct_restore_flow(struct mlx5_tc_ct_priv *ct_priv,
129 			 struct sk_buff *skb, u8 zone_restore_id);
130 
131 #else /* CONFIG_MLX5_TC_CT */
132 
133 static inline struct mlx5_tc_ct_priv *
134 mlx5_tc_ct_init(struct mlx5e_priv *priv, struct mlx5_fs_chains *chains,
135 		struct mod_hdr_tbl *mod_hdr,
136 		enum mlx5_flow_namespace_type ns_type)
137 {
138 	return NULL;
139 }
140 
141 static inline void
142 mlx5_tc_ct_clean(struct mlx5_tc_ct_priv *ct_priv)
143 {
144 }
145 
146 static inline void
147 mlx5_tc_ct_match_del(struct mlx5_tc_ct_priv *priv, struct mlx5_ct_attr *ct_attr) {}
148 
149 static inline int
150 mlx5_tc_ct_match_add(struct mlx5_tc_ct_priv *priv,
151 		     struct mlx5_flow_spec *spec,
152 		     struct flow_cls_offload *f,
153 		     struct mlx5_ct_attr *ct_attr,
154 		     struct netlink_ext_ack *extack)
155 {
156 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
157 
158 	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CT))
159 		return 0;
160 
161 	NL_SET_ERR_MSG_MOD(extack, "mlx5 tc ct offload isn't enabled.");
162 	return -EOPNOTSUPP;
163 }
164 
165 static inline int
166 mlx5_tc_ct_add_no_trk_match(struct mlx5_flow_spec *spec)
167 {
168 	return 0;
169 }
170 
171 static inline int
172 mlx5_tc_ct_parse_action(struct mlx5_tc_ct_priv *priv,
173 			struct mlx5_flow_attr *attr,
174 			const struct flow_action_entry *act,
175 			struct netlink_ext_ack *extack)
176 {
177 	NL_SET_ERR_MSG_MOD(extack, "mlx5 tc ct offload isn't enabled.");
178 	return -EOPNOTSUPP;
179 }
180 
181 static inline struct mlx5_flow_handle *
182 mlx5_tc_ct_flow_offload(struct mlx5_tc_ct_priv *priv,
183 			struct mlx5e_tc_flow *flow,
184 			struct mlx5_flow_spec *spec,
185 			struct mlx5_flow_attr *attr,
186 			struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts)
187 {
188 	return ERR_PTR(-EOPNOTSUPP);
189 }
190 
191 static inline void
192 mlx5_tc_ct_delete_flow(struct mlx5_tc_ct_priv *priv,
193 		       struct mlx5e_tc_flow *flow,
194 		       struct mlx5_flow_attr *attr)
195 {
196 }
197 
198 static inline bool
199 mlx5e_tc_ct_restore_flow(struct mlx5_tc_ct_priv *ct_priv,
200 			 struct sk_buff *skb, u8 zone_restore_id)
201 {
202 	if (!zone_restore_id)
203 		return true;
204 
205 	return false;
206 }
207 
208 #endif /* !IS_ENABLED(CONFIG_MLX5_TC_CT) */
209 #endif /* __MLX5_EN_TC_CT_H__ */
210