1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2019 Mellanox Technologies.
4 #include <linux/mlx5/fs.h>
9 struct mlx5_termtbl_handle
{
10 struct hlist_node termtbl_hlist
;
12 struct mlx5_flow_table
*termtbl
;
13 struct mlx5_flow_act flow_act
;
14 struct mlx5_flow_destination dest
;
16 struct mlx5_flow_handle
*rule
;
21 mlx5_eswitch_termtbl_hash(struct mlx5_flow_act
*flow_act
,
22 struct mlx5_flow_destination
*dest
)
26 hash
= jhash_1word(flow_act
->action
, 0);
27 hash
= jhash((const void *)&flow_act
->vlan
,
28 sizeof(flow_act
->vlan
), hash
);
29 hash
= jhash((const void *)&dest
->vport
.num
,
30 sizeof(dest
->vport
.num
), hash
);
31 hash
= jhash((const void *)&dest
->vport
.vhca_id
,
32 sizeof(dest
->vport
.num
), hash
);
33 if (dest
->vport
.pkt_reformat
)
34 hash
= jhash(dest
->vport
.pkt_reformat
,
35 sizeof(*dest
->vport
.pkt_reformat
),
41 mlx5_eswitch_termtbl_cmp(struct mlx5_flow_act
*flow_act1
,
42 struct mlx5_flow_destination
*dest1
,
43 struct mlx5_flow_act
*flow_act2
,
44 struct mlx5_flow_destination
*dest2
)
48 ret
= flow_act1
->action
!= flow_act2
->action
||
49 dest1
->vport
.num
!= dest2
->vport
.num
||
50 dest1
->vport
.vhca_id
!= dest2
->vport
.vhca_id
||
51 memcmp(&flow_act1
->vlan
, &flow_act2
->vlan
,
52 sizeof(flow_act1
->vlan
));
56 return dest1
->vport
.pkt_reformat
&& dest2
->vport
.pkt_reformat
?
57 memcmp(dest1
->vport
.pkt_reformat
, dest2
->vport
.pkt_reformat
,
58 sizeof(*dest1
->vport
.pkt_reformat
)) : 0;
62 mlx5_eswitch_termtbl_create(struct mlx5_core_dev
*dev
,
63 struct mlx5_termtbl_handle
*tt
,
64 struct mlx5_flow_act
*flow_act
)
66 struct mlx5_flow_table_attr ft_attr
= {};
67 struct mlx5_flow_namespace
*root_ns
;
70 root_ns
= mlx5_get_flow_namespace(dev
, MLX5_FLOW_NAMESPACE_FDB
);
72 esw_warn(dev
, "Failed to get FDB flow namespace\n");
76 /* As this is the terminating action then the termination table is the
77 * same prio as the slow path
79 ft_attr
.flags
= MLX5_FLOW_TABLE_TERMINATION
| MLX5_FLOW_TABLE_UNMANAGED
|
80 MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT
;
81 ft_attr
.prio
= FDB_TC_OFFLOAD
;
84 ft_attr
.autogroup
.max_num_groups
= 1;
85 tt
->termtbl
= mlx5_create_auto_grouped_flow_table(root_ns
, &ft_attr
);
86 if (IS_ERR(tt
->termtbl
)) {
87 err
= PTR_ERR(tt
->termtbl
);
88 esw_warn(dev
, "Failed to create termination table, err %pe\n", tt
->termtbl
);
92 tt
->rule
= mlx5_add_flow_rules(tt
->termtbl
, NULL
, flow_act
,
94 if (IS_ERR(tt
->rule
)) {
95 err
= PTR_ERR(tt
->rule
);
96 esw_warn(dev
, "Failed to create termination table rule, err %pe\n", tt
->rule
);
102 err2
= mlx5_destroy_flow_table(tt
->termtbl
);
104 esw_warn(dev
, "Failed to destroy termination table, err %d\n", err2
);
109 static struct mlx5_termtbl_handle
*
110 mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch
*esw
,
111 struct mlx5_flow_act
*flow_act
,
112 struct mlx5_flow_destination
*dest
,
113 struct mlx5_esw_flow_attr
*attr
)
115 struct mlx5_termtbl_handle
*tt
;
120 mutex_lock(&esw
->offloads
.termtbl_mutex
);
121 hash_key
= mlx5_eswitch_termtbl_hash(flow_act
, dest
);
122 hash_for_each_possible(esw
->offloads
.termtbl_tbl
, tt
,
123 termtbl_hlist
, hash_key
) {
124 if (!mlx5_eswitch_termtbl_cmp(&tt
->flow_act
, &tt
->dest
,
133 tt
= kzalloc(sizeof(*tt
), GFP_KERNEL
);
139 tt
->dest
.type
= MLX5_FLOW_DESTINATION_TYPE_VPORT
;
140 tt
->dest
.vport
.num
= dest
->vport
.num
;
141 tt
->dest
.vport
.vhca_id
= dest
->vport
.vhca_id
;
142 tt
->dest
.vport
.flags
= dest
->vport
.flags
;
143 memcpy(&tt
->flow_act
, flow_act
, sizeof(*flow_act
));
145 err
= mlx5_eswitch_termtbl_create(esw
->dev
, tt
, flow_act
);
149 hash_add(esw
->offloads
.termtbl_tbl
, &tt
->termtbl_hlist
, hash_key
);
152 mutex_unlock(&esw
->offloads
.termtbl_mutex
);
156 mutex_unlock(&esw
->offloads
.termtbl_mutex
);
161 mlx5_eswitch_termtbl_put(struct mlx5_eswitch
*esw
,
162 struct mlx5_termtbl_handle
*tt
)
164 mutex_lock(&esw
->offloads
.termtbl_mutex
);
165 if (--tt
->ref_count
== 0)
166 hash_del(&tt
->termtbl_hlist
);
167 mutex_unlock(&esw
->offloads
.termtbl_mutex
);
169 if (!tt
->ref_count
) {
170 mlx5_del_flow_rules(tt
->rule
);
171 mlx5_destroy_flow_table(tt
->termtbl
);
177 mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act
*src
,
178 struct mlx5_flow_act
*dst
)
180 if (src
->action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH
) {
181 src
->action
&= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH
;
182 dst
->action
|= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH
;
183 memcpy(&dst
->vlan
[0], &src
->vlan
[0], sizeof(src
->vlan
[0]));
184 memset(&src
->vlan
[0], 0, sizeof(src
->vlan
[0]));
186 if (src
->action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2
) {
187 src
->action
&= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2
;
188 dst
->action
|= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2
;
189 memcpy(&dst
->vlan
[1], &src
->vlan
[1], sizeof(src
->vlan
[1]));
190 memset(&src
->vlan
[1], 0, sizeof(src
->vlan
[1]));
195 static bool mlx5_eswitch_offload_is_uplink_port(const struct mlx5_eswitch
*esw
,
196 const struct mlx5_flow_spec
*spec
)
198 u16 port_mask
, port_value
;
200 if (MLX5_CAP_ESW_FLOWTABLE(esw
->dev
, flow_source
))
201 return spec
->flow_context
.flow_source
==
202 MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK
;
204 port_mask
= MLX5_GET(fte_match_param
, spec
->match_criteria
,
205 misc_parameters
.source_port
);
206 port_value
= MLX5_GET(fte_match_param
, spec
->match_value
,
207 misc_parameters
.source_port
);
208 return (port_mask
& port_value
) == MLX5_VPORT_UPLINK
;
212 mlx5_eswitch_termtbl_required(struct mlx5_eswitch
*esw
,
213 struct mlx5_flow_attr
*attr
,
214 struct mlx5_flow_act
*flow_act
,
215 struct mlx5_flow_spec
*spec
)
217 struct mlx5_esw_flow_attr
*esw_attr
= attr
->esw_attr
;
220 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw
->dev
, termination_table
) ||
221 !MLX5_CAP_ESW_FLOWTABLE_FDB(esw
->dev
, ignore_flow_level
) ||
222 mlx5_esw_attr_flags_skip(attr
->flags
) ||
223 !mlx5_eswitch_offload_is_uplink_port(esw
, spec
))
226 /* push vlan on RX */
227 if (flow_act
->action
& MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH
)
231 for (i
= esw_attr
->split_count
; i
< esw_attr
->out_count
; i
++)
232 if (esw_attr
->dests
[i
].rep
->vport
== MLX5_VPORT_UPLINK
)
238 struct mlx5_flow_handle
*
239 mlx5_eswitch_add_termtbl_rule(struct mlx5_eswitch
*esw
,
240 struct mlx5_flow_table
*fdb
,
241 struct mlx5_flow_spec
*spec
,
242 struct mlx5_esw_flow_attr
*attr
,
243 struct mlx5_flow_act
*flow_act
,
244 struct mlx5_flow_destination
*dest
,
247 struct mlx5_flow_act term_tbl_act
= {};
248 struct mlx5_flow_handle
*rule
= NULL
;
249 bool term_table_created
= false;
250 int num_vport_dests
= 0;
253 mlx5_eswitch_termtbl_actions_move(flow_act
, &term_tbl_act
);
254 term_tbl_act
.action
|= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
256 for (i
= 0; i
< num_dest
; i
++) {
257 struct mlx5_termtbl_handle
*tt
;
259 /* only vport destinations can be terminated */
260 if (dest
[i
].type
!= MLX5_FLOW_DESTINATION_TYPE_VPORT
)
263 if (attr
->dests
[num_vport_dests
].flags
& MLX5_ESW_DEST_ENCAP
) {
264 term_tbl_act
.action
|= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT
;
265 term_tbl_act
.pkt_reformat
= attr
->dests
[num_vport_dests
].pkt_reformat
;
267 term_tbl_act
.action
&= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT
;
268 term_tbl_act
.pkt_reformat
= NULL
;
271 /* get the terminating table for the action list */
272 tt
= mlx5_eswitch_termtbl_get_create(esw
, &term_tbl_act
,
275 esw_warn(esw
->dev
, "Failed to get termination table, err %pe\n", tt
);
278 attr
->dests
[num_vport_dests
].termtbl
= tt
;
281 /* link the destination with the termination table */
282 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
283 dest
[i
].ft
= tt
->termtbl
;
284 term_table_created
= true;
287 /* at least one destination should reference a termination table */
288 if (!term_table_created
)
292 flow_act
->action
&= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT
;
293 flow_act
->pkt_reformat
= NULL
;
294 flow_act
->flags
|= FLOW_ACT_IGNORE_FLOW_LEVEL
;
295 rule
= mlx5_add_flow_rules(fdb
, spec
, flow_act
, dest
, num_dest
);
302 /* revert the changes that were made to the original flow_act
303 * and fall-back to the original rule actions
305 mlx5_eswitch_termtbl_actions_move(&term_tbl_act
, flow_act
);
307 for (curr_dest
= 0; curr_dest
< num_vport_dests
; curr_dest
++) {
308 struct mlx5_termtbl_handle
*tt
= attr
->dests
[curr_dest
].termtbl
;
310 /* search for the destination associated with the
313 for (i
= 0; i
< num_dest
; i
++) {
314 if (dest
[i
].ft
!= tt
->termtbl
)
317 memset(&dest
[i
], 0, sizeof(dest
[i
]));
318 dest
[i
].type
= MLX5_FLOW_DESTINATION_TYPE_VPORT
;
319 dest
[i
].vport
.num
= tt
->dest
.vport
.num
;
320 dest
[i
].vport
.vhca_id
= tt
->dest
.vport
.vhca_id
;
321 mlx5_eswitch_termtbl_put(esw
, tt
);
325 rule
= mlx5_add_flow_rules(fdb
, spec
, flow_act
, dest
, num_dest
);