2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <net/flow_dissector.h>
34 #include <net/pkt_cls.h>
35 #include <net/tc_act/tc_gact.h>
36 #include <net/tc_act/tc_skbedit.h>
37 #include <linux/mlx5/fs.h>
38 #include <linux/mlx5/device.h>
39 #include <linux/rhashtable.h>
40 #include <net/switchdev.h>
41 #include <net/tc_act/tc_mirred.h>
42 #include <net/tc_act/tc_vlan.h>
47 struct mlx5e_tc_flow {
48 struct rhash_head node;
50 struct mlx5_flow_rule *rule;
51 struct mlx5_esw_flow_attr *attr;
54 #define MLX5E_TC_TABLE_NUM_ENTRIES 1024
55 #define MLX5E_TC_TABLE_NUM_GROUPS 4
57 static struct mlx5_flow_rule *mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
58 struct mlx5_flow_spec *spec,
59 u32 action, u32 flow_tag)
61 struct mlx5_core_dev *dev = priv->mdev;
62 struct mlx5_flow_destination dest = { 0 };
63 struct mlx5_fc *counter = NULL;
64 struct mlx5_flow_rule *rule;
65 bool table_created = false;
67 if (action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
68 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
69 dest.ft = priv->fs.vlan.ft.t;
70 } else if (action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
71 counter = mlx5_fc_create(dev, true);
73 return ERR_CAST(counter);
75 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
76 dest.counter = counter;
79 if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
81 mlx5_create_auto_grouped_flow_table(priv->fs.ns,
83 MLX5E_TC_TABLE_NUM_ENTRIES,
84 MLX5E_TC_TABLE_NUM_GROUPS,
86 if (IS_ERR(priv->fs.tc.t)) {
87 netdev_err(priv->netdev,
88 "Failed to create tc offload table\n");
89 rule = ERR_CAST(priv->fs.tc.t);
96 spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
97 rule = mlx5_add_flow_rule(priv->fs.tc.t, spec,
108 mlx5_destroy_flow_table(priv->fs.tc.t);
109 priv->fs.tc.t = NULL;
112 mlx5_fc_destroy(dev, counter);
117 static struct mlx5_flow_rule *mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
118 struct mlx5_flow_spec *spec,
119 struct mlx5_esw_flow_attr *attr)
121 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
123 return mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
126 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
127 struct mlx5_flow_rule *rule)
129 struct mlx5_fc *counter = NULL;
131 counter = mlx5_flow_rule_counter(rule);
133 mlx5_del_flow_rule(rule);
135 mlx5_fc_destroy(priv->mdev, counter);
137 if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
138 mlx5_destroy_flow_table(priv->fs.tc.t);
139 priv->fs.tc.t = NULL;
143 static int parse_cls_flower(struct mlx5e_priv *priv, struct mlx5_flow_spec *spec,
144 struct tc_cls_flower_offload *f)
146 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
148 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
153 if (f->dissector->used_keys &
154 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
155 BIT(FLOW_DISSECTOR_KEY_BASIC) |
156 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
157 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
158 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
159 BIT(FLOW_DISSECTOR_KEY_PORTS))) {
160 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
161 f->dissector->used_keys);
165 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
166 struct flow_dissector_key_control *key =
167 skb_flow_dissector_target(f->dissector,
168 FLOW_DISSECTOR_KEY_CONTROL,
170 addr_type = key->addr_type;
173 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
174 struct flow_dissector_key_basic *key =
175 skb_flow_dissector_target(f->dissector,
176 FLOW_DISSECTOR_KEY_BASIC,
178 struct flow_dissector_key_basic *mask =
179 skb_flow_dissector_target(f->dissector,
180 FLOW_DISSECTOR_KEY_BASIC,
182 ip_proto = key->ip_proto;
184 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
185 ntohs(mask->n_proto));
186 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
187 ntohs(key->n_proto));
189 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
191 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
195 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
196 struct flow_dissector_key_eth_addrs *key =
197 skb_flow_dissector_target(f->dissector,
198 FLOW_DISSECTOR_KEY_ETH_ADDRS,
200 struct flow_dissector_key_eth_addrs *mask =
201 skb_flow_dissector_target(f->dissector,
202 FLOW_DISSECTOR_KEY_ETH_ADDRS,
205 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
208 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
212 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
215 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
220 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
221 struct flow_dissector_key_ipv4_addrs *key =
222 skb_flow_dissector_target(f->dissector,
223 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
225 struct flow_dissector_key_ipv4_addrs *mask =
226 skb_flow_dissector_target(f->dissector,
227 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
230 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
231 src_ipv4_src_ipv6.ipv4_layout.ipv4),
232 &mask->src, sizeof(mask->src));
233 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
234 src_ipv4_src_ipv6.ipv4_layout.ipv4),
235 &key->src, sizeof(key->src));
236 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
237 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
238 &mask->dst, sizeof(mask->dst));
239 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
240 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
241 &key->dst, sizeof(key->dst));
244 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
245 struct flow_dissector_key_ipv6_addrs *key =
246 skb_flow_dissector_target(f->dissector,
247 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
249 struct flow_dissector_key_ipv6_addrs *mask =
250 skb_flow_dissector_target(f->dissector,
251 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
254 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
255 src_ipv4_src_ipv6.ipv6_layout.ipv6),
256 &mask->src, sizeof(mask->src));
257 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
258 src_ipv4_src_ipv6.ipv6_layout.ipv6),
259 &key->src, sizeof(key->src));
261 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
262 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
263 &mask->dst, sizeof(mask->dst));
264 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
265 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
266 &key->dst, sizeof(key->dst));
269 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
270 struct flow_dissector_key_ports *key =
271 skb_flow_dissector_target(f->dissector,
272 FLOW_DISSECTOR_KEY_PORTS,
274 struct flow_dissector_key_ports *mask =
275 skb_flow_dissector_target(f->dissector,
276 FLOW_DISSECTOR_KEY_PORTS,
280 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
281 tcp_sport, ntohs(mask->src));
282 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
283 tcp_sport, ntohs(key->src));
285 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
286 tcp_dport, ntohs(mask->dst));
287 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
288 tcp_dport, ntohs(key->dst));
292 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
293 udp_sport, ntohs(mask->src));
294 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
295 udp_sport, ntohs(key->src));
297 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
298 udp_dport, ntohs(mask->dst));
299 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
300 udp_dport, ntohs(key->dst));
303 netdev_err(priv->netdev,
304 "Only UDP and TCP transport are supported\n");
312 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
313 u32 *action, u32 *flow_tag)
315 const struct tc_action *a;
318 if (tc_no_actions(exts))
321 *flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
324 tcf_exts_to_list(exts, &actions);
325 list_for_each_entry(a, &actions, list) {
326 /* Only support a single action per rule */
330 if (is_tcf_gact_shot(a)) {
331 *action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
332 if (MLX5_CAP_FLOWTABLE(priv->mdev,
333 flow_table_properties_nic_receive.flow_counter))
334 *action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
338 if (is_tcf_skbedit_mark(a)) {
339 u32 mark = tcf_skbedit_mark(a);
341 if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
342 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
348 *action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
358 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
359 struct mlx5_esw_flow_attr *attr)
361 const struct tc_action *a;
364 if (tc_no_actions(exts))
367 memset(attr, 0, sizeof(*attr));
368 attr->in_rep = priv->ppriv;
370 tcf_exts_to_list(exts, &actions);
371 list_for_each_entry(a, &actions, list) {
372 /* Only support a single action per rule */
376 if (is_tcf_gact_shot(a)) {
377 attr->action = MLX5_FLOW_CONTEXT_ACTION_DROP |
378 MLX5_FLOW_CONTEXT_ACTION_COUNT;
382 if (is_tcf_mirred_redirect(a)) {
383 int ifindex = tcf_mirred_ifindex(a);
384 struct net_device *out_dev;
385 struct mlx5e_priv *out_priv;
387 out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
389 if (!switchdev_port_same_parent_id(priv->netdev, out_dev)) {
390 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
391 priv->netdev->name, out_dev->name);
395 attr->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
396 out_priv = netdev_priv(out_dev);
397 attr->out_rep = out_priv->ppriv;
406 int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
407 struct tc_cls_flower_offload *f)
409 struct mlx5e_tc_table *tc = &priv->fs.tc;
411 bool fdb_flow = false;
412 u32 flow_tag, action;
413 struct mlx5e_tc_flow *flow;
414 struct mlx5_flow_spec *spec;
415 struct mlx5_flow_rule *old = NULL;
416 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
418 if (esw && esw->mode == SRIOV_OFFLOADS)
421 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
427 flow = kzalloc(sizeof(*flow) + sizeof(struct mlx5_esw_flow_attr),
430 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
433 spec = mlx5_vzalloc(sizeof(*spec));
434 if (!spec || !flow) {
439 flow->cookie = f->cookie;
441 err = parse_cls_flower(priv, spec, f);
446 flow->attr = (struct mlx5_esw_flow_attr *)(flow + 1);
447 err = parse_tc_fdb_actions(priv, f->exts, flow->attr);
450 flow->rule = mlx5e_tc_add_fdb_flow(priv, spec, flow->attr);
452 err = parse_tc_nic_actions(priv, f->exts, &action, &flow_tag);
455 flow->rule = mlx5e_tc_add_nic_flow(priv, spec, action, flow_tag);
458 if (IS_ERR(flow->rule)) {
459 err = PTR_ERR(flow->rule);
463 err = rhashtable_insert_fast(&tc->ht, &flow->node,
469 mlx5e_tc_del_flow(priv, old);
474 mlx5_del_flow_rule(flow->rule);
484 int mlx5e_delete_flower(struct mlx5e_priv *priv,
485 struct tc_cls_flower_offload *f)
487 struct mlx5e_tc_flow *flow;
488 struct mlx5e_tc_table *tc = &priv->fs.tc;
490 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
495 rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
497 mlx5e_tc_del_flow(priv, flow->rule);
504 int mlx5e_stats_flower(struct mlx5e_priv *priv,
505 struct tc_cls_flower_offload *f)
507 struct mlx5e_tc_table *tc = &priv->fs.tc;
508 struct mlx5e_tc_flow *flow;
510 struct mlx5_fc *counter;
516 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
521 counter = mlx5_flow_rule_counter(flow->rule);
525 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
527 tcf_exts_to_list(f->exts, &actions);
528 list_for_each_entry(a, &actions, list)
529 tcf_action_stats_update(a, bytes, packets, lastuse);
534 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
535 .head_offset = offsetof(struct mlx5e_tc_flow, node),
536 .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
537 .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
538 .automatic_shrinking = true,
541 int mlx5e_tc_init(struct mlx5e_priv *priv)
543 struct mlx5e_tc_table *tc = &priv->fs.tc;
545 tc->ht_params = mlx5e_tc_flow_ht_params;
546 return rhashtable_init(&tc->ht, &tc->ht_params);
549 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
551 struct mlx5e_tc_flow *flow = ptr;
552 struct mlx5e_priv *priv = arg;
554 mlx5e_tc_del_flow(priv, flow->rule);
558 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
560 struct mlx5e_tc_table *tc = &priv->fs.tc;
562 rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
564 if (!IS_ERR_OR_NULL(tc->t)) {
565 mlx5_destroy_flow_table(tc->t);