]>
Commit | Line | Data |
---|---|---|
e8f887ac AV |
1 | /* |
2 | * Copyright (c) 2016, Mellanox Technologies. All rights reserved. | |
3 | * | |
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: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or | |
11 | * without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistributions of source code must retain the above | |
15 | * copyright notice, this list of conditions and the following | |
16 | * disclaimer. | |
17 | * | |
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. | |
22 | * | |
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 | |
30 | * SOFTWARE. | |
31 | */ | |
32 | ||
e3a2b7ed AV |
33 | #include <net/flow_dissector.h> |
34 | #include <net/pkt_cls.h> | |
35 | #include <net/tc_act/tc_gact.h> | |
e8f887ac AV |
36 | #include <linux/mlx5/fs.h> |
37 | #include <linux/mlx5/device.h> | |
38 | #include <linux/rhashtable.h> | |
39 | #include "en.h" | |
40 | #include "en_tc.h" | |
41 | ||
42 | struct mlx5e_tc_flow { | |
43 | struct rhash_head node; | |
44 | u64 cookie; | |
45 | struct mlx5_flow_rule *rule; | |
46 | }; | |
47 | ||
48 | #define MLX5E_TC_FLOW_TABLE_NUM_ENTRIES 1024 | |
49 | #define MLX5E_TC_FLOW_TABLE_NUM_GROUPS 4 | |
50 | ||
51 | static struct mlx5_flow_rule *mlx5e_tc_add_flow(struct mlx5e_priv *priv, | |
52 | u32 *match_c, u32 *match_v, | |
53 | u32 action, u32 flow_tag) | |
54 | { | |
55 | struct mlx5_flow_destination dest = { | |
56 | .type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE, | |
57 | {.ft = priv->fts.vlan.t}, | |
58 | }; | |
59 | struct mlx5_flow_rule *rule; | |
60 | bool table_created = false; | |
61 | ||
62 | if (IS_ERR_OR_NULL(priv->fts.tc.t)) { | |
63 | priv->fts.tc.t = | |
64 | mlx5_create_auto_grouped_flow_table(priv->fts.ns, 0, | |
65 | MLX5E_TC_FLOW_TABLE_NUM_ENTRIES, | |
66 | MLX5E_TC_FLOW_TABLE_NUM_GROUPS); | |
67 | if (IS_ERR(priv->fts.tc.t)) { | |
68 | netdev_err(priv->netdev, | |
69 | "Failed to create tc offload table\n"); | |
70 | return ERR_CAST(priv->fts.tc.t); | |
71 | } | |
72 | ||
73 | table_created = true; | |
74 | } | |
75 | ||
76 | rule = mlx5_add_flow_rule(priv->fts.tc.t, MLX5_MATCH_OUTER_HEADERS, | |
77 | match_c, match_v, | |
78 | action, flow_tag, | |
79 | action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ? &dest : NULL); | |
80 | ||
81 | if (IS_ERR(rule) && table_created) { | |
82 | mlx5_destroy_flow_table(priv->fts.tc.t); | |
83 | priv->fts.tc.t = NULL; | |
84 | } | |
85 | ||
86 | return rule; | |
87 | } | |
88 | ||
89 | static void mlx5e_tc_del_flow(struct mlx5e_priv *priv, | |
90 | struct mlx5_flow_rule *rule) | |
91 | { | |
92 | mlx5_del_flow_rule(rule); | |
93 | ||
94 | if (!mlx5e_tc_num_filters(priv)) { | |
95 | mlx5_destroy_flow_table(priv->fts.tc.t); | |
96 | priv->fts.tc.t = NULL; | |
97 | } | |
98 | } | |
99 | ||
e3a2b7ed AV |
100 | static int parse_cls_flower(struct mlx5e_priv *priv, |
101 | u32 *match_c, u32 *match_v, | |
102 | struct tc_cls_flower_offload *f) | |
103 | { | |
104 | void *headers_c = MLX5_ADDR_OF(fte_match_param, match_c, outer_headers); | |
105 | void *headers_v = MLX5_ADDR_OF(fte_match_param, match_v, outer_headers); | |
106 | u16 addr_type = 0; | |
107 | u8 ip_proto = 0; | |
108 | ||
109 | if (f->dissector->used_keys & | |
110 | ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) | | |
111 | BIT(FLOW_DISSECTOR_KEY_BASIC) | | |
112 | BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) | | |
113 | BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) | | |
114 | BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) | | |
115 | BIT(FLOW_DISSECTOR_KEY_PORTS))) { | |
116 | netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n", | |
117 | f->dissector->used_keys); | |
118 | return -EOPNOTSUPP; | |
119 | } | |
120 | ||
121 | if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) { | |
122 | struct flow_dissector_key_control *key = | |
123 | skb_flow_dissector_target(f->dissector, | |
124 | FLOW_DISSECTOR_KEY_BASIC, | |
125 | f->key); | |
126 | addr_type = key->addr_type; | |
127 | } | |
128 | ||
129 | if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) { | |
130 | struct flow_dissector_key_basic *key = | |
131 | skb_flow_dissector_target(f->dissector, | |
132 | FLOW_DISSECTOR_KEY_BASIC, | |
133 | f->key); | |
134 | struct flow_dissector_key_basic *mask = | |
135 | skb_flow_dissector_target(f->dissector, | |
136 | FLOW_DISSECTOR_KEY_BASIC, | |
137 | f->mask); | |
138 | ip_proto = key->ip_proto; | |
139 | ||
140 | MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype, | |
141 | ntohs(mask->n_proto)); | |
142 | MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, | |
143 | ntohs(key->n_proto)); | |
144 | ||
145 | MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, | |
146 | mask->ip_proto); | |
147 | MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, | |
148 | key->ip_proto); | |
149 | } | |
150 | ||
151 | if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) { | |
152 | struct flow_dissector_key_eth_addrs *key = | |
153 | skb_flow_dissector_target(f->dissector, | |
154 | FLOW_DISSECTOR_KEY_ETH_ADDRS, | |
155 | f->key); | |
156 | struct flow_dissector_key_eth_addrs *mask = | |
157 | skb_flow_dissector_target(f->dissector, | |
158 | FLOW_DISSECTOR_KEY_ETH_ADDRS, | |
159 | f->mask); | |
160 | ||
161 | ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, | |
162 | dmac_47_16), | |
163 | mask->dst); | |
164 | ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, | |
165 | dmac_47_16), | |
166 | key->dst); | |
167 | ||
168 | ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, | |
169 | smac_47_16), | |
170 | mask->src); | |
171 | ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, | |
172 | smac_47_16), | |
173 | key->src); | |
174 | } | |
175 | ||
176 | if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { | |
177 | struct flow_dissector_key_ipv4_addrs *key = | |
178 | skb_flow_dissector_target(f->dissector, | |
179 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, | |
180 | f->key); | |
181 | struct flow_dissector_key_ipv4_addrs *mask = | |
182 | skb_flow_dissector_target(f->dissector, | |
183 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, | |
184 | f->mask); | |
185 | ||
186 | memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, | |
187 | src_ipv4_src_ipv6.ipv4_layout.ipv4), | |
188 | &mask->src, sizeof(mask->src)); | |
189 | memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, | |
190 | src_ipv4_src_ipv6.ipv4_layout.ipv4), | |
191 | &key->src, sizeof(key->src)); | |
192 | memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, | |
193 | dst_ipv4_dst_ipv6.ipv4_layout.ipv4), | |
194 | &mask->dst, sizeof(mask->dst)); | |
195 | memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, | |
196 | dst_ipv4_dst_ipv6.ipv4_layout.ipv4), | |
197 | &key->dst, sizeof(key->dst)); | |
198 | } | |
199 | ||
200 | if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { | |
201 | struct flow_dissector_key_ipv6_addrs *key = | |
202 | skb_flow_dissector_target(f->dissector, | |
203 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, | |
204 | f->key); | |
205 | struct flow_dissector_key_ipv6_addrs *mask = | |
206 | skb_flow_dissector_target(f->dissector, | |
207 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, | |
208 | f->mask); | |
209 | ||
210 | memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, | |
211 | src_ipv4_src_ipv6.ipv6_layout.ipv6), | |
212 | &mask->src, sizeof(mask->src)); | |
213 | memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, | |
214 | src_ipv4_src_ipv6.ipv6_layout.ipv6), | |
215 | &key->src, sizeof(key->src)); | |
216 | ||
217 | memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, | |
218 | dst_ipv4_dst_ipv6.ipv6_layout.ipv6), | |
219 | &mask->dst, sizeof(mask->dst)); | |
220 | memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, | |
221 | dst_ipv4_dst_ipv6.ipv6_layout.ipv6), | |
222 | &key->dst, sizeof(key->dst)); | |
223 | } | |
224 | ||
225 | if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) { | |
226 | struct flow_dissector_key_ports *key = | |
227 | skb_flow_dissector_target(f->dissector, | |
228 | FLOW_DISSECTOR_KEY_PORTS, | |
229 | f->key); | |
230 | struct flow_dissector_key_ports *mask = | |
231 | skb_flow_dissector_target(f->dissector, | |
232 | FLOW_DISSECTOR_KEY_PORTS, | |
233 | f->mask); | |
234 | switch (ip_proto) { | |
235 | case IPPROTO_TCP: | |
236 | MLX5_SET(fte_match_set_lyr_2_4, headers_c, | |
237 | tcp_sport, ntohs(mask->src)); | |
238 | MLX5_SET(fte_match_set_lyr_2_4, headers_v, | |
239 | tcp_sport, ntohs(key->src)); | |
240 | ||
241 | MLX5_SET(fte_match_set_lyr_2_4, headers_c, | |
242 | tcp_dport, ntohs(mask->dst)); | |
243 | MLX5_SET(fte_match_set_lyr_2_4, headers_v, | |
244 | tcp_dport, ntohs(key->dst)); | |
245 | break; | |
246 | ||
247 | case IPPROTO_UDP: | |
248 | MLX5_SET(fte_match_set_lyr_2_4, headers_c, | |
249 | udp_sport, ntohs(mask->src)); | |
250 | MLX5_SET(fte_match_set_lyr_2_4, headers_v, | |
251 | udp_sport, ntohs(key->src)); | |
252 | ||
253 | MLX5_SET(fte_match_set_lyr_2_4, headers_c, | |
254 | udp_dport, ntohs(mask->dst)); | |
255 | MLX5_SET(fte_match_set_lyr_2_4, headers_v, | |
256 | udp_dport, ntohs(key->dst)); | |
257 | break; | |
258 | default: | |
259 | netdev_err(priv->netdev, | |
260 | "Only UDP and TCP transport are supported\n"); | |
261 | return -EINVAL; | |
262 | } | |
263 | } | |
264 | ||
265 | return 0; | |
266 | } | |
267 | ||
268 | static int parse_tc_actions(struct mlx5e_priv *priv, struct tcf_exts *exts, | |
269 | u32 *action, u32 *flow_tag) | |
270 | { | |
271 | const struct tc_action *a; | |
272 | ||
273 | if (tc_no_actions(exts)) | |
274 | return -EINVAL; | |
275 | ||
276 | *flow_tag = MLX5_FS_DEFAULT_FLOW_TAG; | |
277 | *action = 0; | |
278 | ||
279 | tc_for_each_action(a, exts) { | |
280 | /* Only support a single action per rule */ | |
281 | if (*action) | |
282 | return -EINVAL; | |
283 | ||
284 | if (is_tcf_gact_shot(a)) { | |
285 | *action |= MLX5_FLOW_CONTEXT_ACTION_DROP; | |
286 | continue; | |
287 | } | |
288 | ||
289 | if (is_tcf_skbedit_mark(a)) { | |
290 | u32 mark = tcf_skbedit_mark(a); | |
291 | ||
292 | if (mark & ~MLX5E_TC_FLOW_ID_MASK) { | |
293 | netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n", | |
294 | mark); | |
295 | return -EINVAL; | |
296 | } | |
297 | ||
298 | *flow_tag = mark; | |
299 | *action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST; | |
300 | continue; | |
301 | } | |
302 | ||
303 | return -EINVAL; | |
304 | } | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
309 | int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol, | |
310 | struct tc_cls_flower_offload *f) | |
311 | { | |
312 | struct mlx5e_tc_flow_table *tc = &priv->fts.tc; | |
313 | u32 *match_c; | |
314 | u32 *match_v; | |
315 | int err = 0; | |
316 | u32 flow_tag; | |
317 | u32 action; | |
318 | struct mlx5e_tc_flow *flow; | |
319 | struct mlx5_flow_rule *old = NULL; | |
320 | ||
321 | flow = rhashtable_lookup_fast(&tc->ht, &f->cookie, | |
322 | tc->ht_params); | |
323 | if (flow) | |
324 | old = flow->rule; | |
325 | else | |
326 | flow = kzalloc(sizeof(*flow), GFP_KERNEL); | |
327 | ||
328 | match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL); | |
329 | match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL); | |
330 | if (!match_c || !match_v || !flow) { | |
331 | err = -ENOMEM; | |
332 | goto err_free; | |
333 | } | |
334 | ||
335 | flow->cookie = f->cookie; | |
336 | ||
337 | err = parse_cls_flower(priv, match_c, match_v, f); | |
338 | if (err < 0) | |
339 | goto err_free; | |
340 | ||
341 | err = parse_tc_actions(priv, f->exts, &action, &flow_tag); | |
342 | if (err < 0) | |
343 | goto err_free; | |
344 | ||
345 | err = rhashtable_insert_fast(&tc->ht, &flow->node, | |
346 | tc->ht_params); | |
347 | if (err) | |
348 | goto err_free; | |
349 | ||
350 | flow->rule = mlx5e_tc_add_flow(priv, match_c, match_v, action, | |
351 | flow_tag); | |
352 | if (IS_ERR(flow->rule)) { | |
353 | err = PTR_ERR(flow->rule); | |
354 | goto err_hash_del; | |
355 | } | |
356 | ||
357 | if (old) | |
358 | mlx5e_tc_del_flow(priv, old); | |
359 | ||
360 | goto out; | |
361 | ||
362 | err_hash_del: | |
363 | rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params); | |
364 | ||
365 | err_free: | |
366 | if (!old) | |
367 | kfree(flow); | |
368 | out: | |
369 | kfree(match_c); | |
370 | kfree(match_v); | |
371 | return err; | |
372 | } | |
373 | ||
374 | int mlx5e_delete_flower(struct mlx5e_priv *priv, | |
375 | struct tc_cls_flower_offload *f) | |
376 | { | |
377 | struct mlx5e_tc_flow *flow; | |
378 | struct mlx5e_tc_flow_table *tc = &priv->fts.tc; | |
379 | ||
380 | flow = rhashtable_lookup_fast(&tc->ht, &f->cookie, | |
381 | tc->ht_params); | |
382 | if (!flow) | |
383 | return -EINVAL; | |
384 | ||
385 | rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params); | |
386 | ||
387 | mlx5e_tc_del_flow(priv, flow->rule); | |
388 | ||
389 | kfree(flow); | |
390 | ||
391 | return 0; | |
392 | } | |
393 | ||
e8f887ac AV |
394 | static const struct rhashtable_params mlx5e_tc_flow_ht_params = { |
395 | .head_offset = offsetof(struct mlx5e_tc_flow, node), | |
396 | .key_offset = offsetof(struct mlx5e_tc_flow, cookie), | |
397 | .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie), | |
398 | .automatic_shrinking = true, | |
399 | }; | |
400 | ||
401 | int mlx5e_tc_init(struct mlx5e_priv *priv) | |
402 | { | |
403 | struct mlx5e_tc_flow_table *tc = &priv->fts.tc; | |
404 | ||
405 | tc->ht_params = mlx5e_tc_flow_ht_params; | |
406 | return rhashtable_init(&tc->ht, &tc->ht_params); | |
407 | } | |
408 | ||
409 | static void _mlx5e_tc_del_flow(void *ptr, void *arg) | |
410 | { | |
411 | struct mlx5e_tc_flow *flow = ptr; | |
412 | struct mlx5e_priv *priv = arg; | |
413 | ||
414 | mlx5e_tc_del_flow(priv, flow->rule); | |
415 | kfree(flow); | |
416 | } | |
417 | ||
418 | void mlx5e_tc_cleanup(struct mlx5e_priv *priv) | |
419 | { | |
420 | struct mlx5e_tc_flow_table *tc = &priv->fts.tc; | |
421 | ||
422 | rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv); | |
423 | ||
424 | if (!IS_ERR_OR_NULL(priv->fts.tc.t)) { | |
425 | mlx5_destroy_flow_table(priv->fts.tc.t); | |
426 | priv->fts.tc.t = NULL; | |
427 | } | |
428 | } |