net/mlx5e: Support l3/l4 flow type specs in ethtool flow steering
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_fs_ethtool.c
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
33 #include <linux/mlx5/fs.h>
34 #include "en.h"
35
36 struct mlx5e_ethtool_rule {
37         struct list_head             list;
38         struct ethtool_rx_flow_spec  flow_spec;
39         struct mlx5_flow_rule        *rule;
40         struct mlx5e_ethtool_table   *eth_ft;
41 };
42
43 static void put_flow_table(struct mlx5e_ethtool_table *eth_ft)
44 {
45         if (!--eth_ft->num_rules) {
46                 mlx5_destroy_flow_table(eth_ft->ft);
47                 eth_ft->ft = NULL;
48         }
49 }
50
51 #define MLX5E_ETHTOOL_L3_L4_PRIO 0
52 #define MLX5E_ETHTOOL_L2_PRIO (MLX5E_ETHTOOL_L3_L4_PRIO + ETHTOOL_NUM_L3_L4_FTS)
53 #define MLX5E_ETHTOOL_NUM_ENTRIES 64000
54 #define MLX5E_ETHTOOL_NUM_GROUPS  10
55 static struct mlx5e_ethtool_table *get_flow_table(struct mlx5e_priv *priv,
56                                                   struct ethtool_rx_flow_spec *fs,
57                                                   int num_tuples)
58 {
59         struct mlx5e_ethtool_table *eth_ft;
60         struct mlx5_flow_namespace *ns;
61         struct mlx5_flow_table *ft;
62         int max_tuples;
63         int table_size;
64         int prio;
65
66         switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
67         case TCP_V4_FLOW:
68         case UDP_V4_FLOW:
69                 max_tuples = ETHTOOL_NUM_L3_L4_FTS;
70                 prio = MLX5E_ETHTOOL_L3_L4_PRIO + (max_tuples - num_tuples);
71                 eth_ft = &priv->fs.ethtool.l3_l4_ft[prio];
72                 break;
73         case IP_USER_FLOW:
74                 max_tuples = ETHTOOL_NUM_L3_L4_FTS;
75                 prio = MLX5E_ETHTOOL_L3_L4_PRIO + (max_tuples - num_tuples);
76                 eth_ft = &priv->fs.ethtool.l3_l4_ft[prio];
77                 break;
78         case ETHER_FLOW:
79                 max_tuples = ETHTOOL_NUM_L2_FTS;
80                 prio = max_tuples - num_tuples;
81                 eth_ft = &priv->fs.ethtool.l2_ft[prio];
82                 prio += MLX5E_ETHTOOL_L2_PRIO;
83                 break;
84         default:
85                 return ERR_PTR(-EINVAL);
86         }
87
88         eth_ft->num_rules++;
89         if (eth_ft->ft)
90                 return eth_ft;
91
92         ns = mlx5_get_flow_namespace(priv->mdev,
93                                      MLX5_FLOW_NAMESPACE_ETHTOOL);
94         if (!ns)
95                 return ERR_PTR(-ENOTSUPP);
96
97         table_size = min_t(u32, BIT(MLX5_CAP_FLOWTABLE(priv->mdev,
98                                                        flow_table_properties_nic_receive.log_max_ft_size)),
99                            MLX5E_ETHTOOL_NUM_ENTRIES);
100         ft = mlx5_create_auto_grouped_flow_table(ns, prio,
101                                                  table_size,
102                                                  MLX5E_ETHTOOL_NUM_GROUPS, 0);
103         if (IS_ERR(ft))
104                 return (void *)ft;
105
106         eth_ft->ft = ft;
107         return eth_ft;
108 }
109
110 static void mask_spec(u8 *mask, u8 *val, size_t size)
111 {
112         unsigned int i;
113
114         for (i = 0; i < size; i++, mask++, val++)
115                 *((u8 *)val) = *((u8 *)mask) & *((u8 *)val);
116 }
117
118 static void set_ips(void *outer_headers_v, void *outer_headers_c, __be32 ip4src_m,
119                     __be32 ip4src_v, __be32 ip4dst_m, __be32 ip4dst_v)
120 {
121         if (ip4src_m) {
122                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
123                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
124                        &ip4src_v, sizeof(ip4src_v));
125                 memset(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
126                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
127                        0xff, sizeof(ip4src_m));
128         }
129         if (ip4dst_m) {
130                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
131                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
132                        &ip4dst_v, sizeof(ip4dst_v));
133                 memset(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
134                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
135                        0xff, sizeof(ip4dst_m));
136         }
137         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
138                  ethertype, ETH_P_IP);
139         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
140                  ethertype, 0xffff);
141 }
142
143 static int set_flow_attrs(u32 *match_c, u32 *match_v,
144                           struct ethtool_rx_flow_spec *fs)
145 {
146         void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
147                                              outer_headers);
148         void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
149                                              outer_headers);
150         u32 flow_type = fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT);
151         struct ethtool_tcpip4_spec *l4_mask;
152         struct ethtool_tcpip4_spec *l4_val;
153         struct ethtool_usrip4_spec *l3_mask;
154         struct ethtool_usrip4_spec *l3_val;
155         struct ethhdr *eth_val;
156         struct ethhdr *eth_mask;
157
158         switch (flow_type) {
159         case TCP_V4_FLOW:
160                 l4_mask = &fs->m_u.tcp_ip4_spec;
161                 l4_val = &fs->h_u.tcp_ip4_spec;
162                 set_ips(outer_headers_v, outer_headers_c, l4_mask->ip4src,
163                         l4_val->ip4src, l4_mask->ip4dst, l4_val->ip4dst);
164
165                 if (l4_mask->psrc) {
166                         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport,
167                                  0xffff);
168                         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport,
169                                  ntohs(l4_val->psrc));
170                 }
171                 if (l4_mask->pdst) {
172                         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport,
173                                  0xffff);
174                         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport,
175                                  ntohs(l4_val->pdst));
176                 }
177                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
178                          0xffff);
179                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
180                          IPPROTO_TCP);
181                 break;
182         case UDP_V4_FLOW:
183                 l4_mask = &fs->m_u.tcp_ip4_spec;
184                 l4_val = &fs->h_u.tcp_ip4_spec;
185                 set_ips(outer_headers_v, outer_headers_c, l4_mask->ip4src,
186                         l4_val->ip4src, l4_mask->ip4dst, l4_val->ip4dst);
187
188                 if (l4_mask->psrc) {
189                         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport,
190                                  0xffff);
191                         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport,
192                                  ntohs(l4_val->psrc));
193                 }
194                 if (l4_mask->pdst) {
195                         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport,
196                                  0xffff);
197                         MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport,
198                                  ntohs(l4_val->pdst));
199                 }
200                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
201                          0xffff);
202                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
203                          IPPROTO_UDP);
204                 break;
205         case IP_USER_FLOW:
206                 l3_mask = &fs->m_u.usr_ip4_spec;
207                 l3_val = &fs->h_u.usr_ip4_spec;
208                 set_ips(outer_headers_v, outer_headers_c, l3_mask->ip4src,
209                         l3_val->ip4src, l3_mask->ip4dst, l3_val->ip4dst);
210                 break;
211         case ETHER_FLOW:
212                 eth_mask = &fs->m_u.ether_spec;
213                 eth_val = &fs->h_u.ether_spec;
214
215                 mask_spec((u8 *)eth_mask, (u8 *)eth_val, sizeof(*eth_mask));
216                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4,
217                                              outer_headers_c, smac_47_16),
218                                 eth_mask->h_source);
219                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4,
220                                              outer_headers_v, smac_47_16),
221                                 eth_val->h_source);
222                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4,
223                                              outer_headers_c, dmac_47_16),
224                                 eth_mask->h_dest);
225                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4,
226                                              outer_headers_v, dmac_47_16),
227                                 eth_val->h_dest);
228                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ethertype,
229                          ntohs(eth_mask->h_proto));
230                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ethertype,
231                          ntohs(eth_val->h_proto));
232                 break;
233         default:
234                 return -EINVAL;
235         }
236
237         if ((fs->flow_type & FLOW_EXT) &&
238             (fs->m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK))) {
239                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
240                          vlan_tag, 1);
241                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
242                          vlan_tag, 1);
243                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
244                          first_vid, 0xfff);
245                 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
246                          first_vid, ntohs(fs->h_ext.vlan_tci));
247         }
248         if (fs->flow_type & FLOW_MAC_EXT &&
249             !is_zero_ether_addr(fs->m_ext.h_dest)) {
250                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4,
251                                              outer_headers_c, dmac_47_16),
252                                 fs->m_ext.h_dest);
253                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4,
254                                              outer_headers_v, dmac_47_16),
255                                 fs->h_ext.h_dest);
256         }
257
258         return 0;
259 }
260
261 static void add_rule_to_list(struct mlx5e_priv *priv,
262                              struct mlx5e_ethtool_rule *rule)
263 {
264         struct mlx5e_ethtool_rule *iter;
265         struct list_head *head = &priv->fs.ethtool.rules;
266
267         list_for_each_entry(iter, &priv->fs.ethtool.rules, list) {
268                 if (iter->flow_spec.location > rule->flow_spec.location)
269                         break;
270                 head = &iter->list;
271         }
272         priv->fs.ethtool.tot_num_rules++;
273         list_add(&rule->list, head);
274 }
275
276 static bool outer_header_zero(u32 *match_criteria)
277 {
278         int size = MLX5_ST_SZ_BYTES(fte_match_param);
279         char *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_criteria,
280                                              outer_headers);
281
282         return outer_headers_c[0] == 0 && !memcmp(outer_headers_c,
283                                                   outer_headers_c + 1,
284                                                   size - 1);
285 }
286
287 static struct mlx5_flow_rule *add_ethtool_flow_rule(struct mlx5e_priv *priv,
288                                                     struct mlx5_flow_table *ft,
289                                                     struct ethtool_rx_flow_spec *fs)
290 {
291         struct mlx5_flow_destination *dst = NULL;
292         struct mlx5_flow_spec *spec;
293         struct mlx5_flow_rule *rule;
294         int err = 0;
295         u32 action;
296
297         spec = mlx5_vzalloc(sizeof(*spec));
298         if (!spec)
299                 return ERR_PTR(-ENOMEM);
300         err = set_flow_attrs(spec->match_criteria, spec->match_value,
301                              fs);
302         if (err)
303                 goto free;
304
305         if (fs->ring_cookie == RX_CLS_FLOW_DISC) {
306                 action = MLX5_FLOW_CONTEXT_ACTION_DROP;
307         } else {
308                 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
309                 if (!dst) {
310                         err = -ENOMEM;
311                         goto free;
312                 }
313
314                 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
315                 dst->tir_num = priv->direct_tir[fs->ring_cookie].tirn;
316                 action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
317         }
318
319         spec->match_criteria_enable = (!outer_header_zero(spec->match_criteria));
320         rule = mlx5_add_flow_rule(ft, spec, action,
321                                   MLX5_FS_DEFAULT_FLOW_TAG, dst);
322         if (IS_ERR(rule)) {
323                 err = PTR_ERR(rule);
324                 netdev_err(priv->netdev, "%s: failed to add ethtool steering rule: %d\n",
325                            __func__, err);
326                 goto free;
327         }
328 free:
329         kvfree(spec);
330         kfree(dst);
331         return err ? ERR_PTR(err) : rule;
332 }
333
334 static void del_ethtool_rule(struct mlx5e_priv *priv,
335                              struct mlx5e_ethtool_rule *eth_rule)
336 {
337         if (eth_rule->rule)
338                 mlx5_del_flow_rule(eth_rule->rule);
339         list_del(&eth_rule->list);
340         priv->fs.ethtool.tot_num_rules--;
341         put_flow_table(eth_rule->eth_ft);
342         kfree(eth_rule);
343 }
344
345 static struct mlx5e_ethtool_rule *find_ethtool_rule(struct mlx5e_priv *priv,
346                                                     int location)
347 {
348         struct mlx5e_ethtool_rule *iter;
349
350         list_for_each_entry(iter, &priv->fs.ethtool.rules, list) {
351                 if (iter->flow_spec.location == location)
352                         return iter;
353         }
354         return NULL;
355 }
356
357 static struct mlx5e_ethtool_rule *get_ethtool_rule(struct mlx5e_priv *priv,
358                                                    int location)
359 {
360         struct mlx5e_ethtool_rule *eth_rule;
361
362         eth_rule = find_ethtool_rule(priv, location);
363         if (eth_rule)
364                 del_ethtool_rule(priv, eth_rule);
365
366         eth_rule = kzalloc(sizeof(*eth_rule), GFP_KERNEL);
367         if (!eth_rule)
368                 return ERR_PTR(-ENOMEM);
369
370         add_rule_to_list(priv, eth_rule);
371         return eth_rule;
372 }
373
374 #define MAX_NUM_OF_ETHTOOL_RULES BIT(10)
375
376 #define all_ones(field) (field == (__force typeof(field))-1)
377 #define all_zeros_or_all_ones(field)            \
378         ((field) == 0 || (field) == (__force typeof(field))-1)
379
380 static int validate_flow(struct mlx5e_priv *priv,
381                          struct ethtool_rx_flow_spec *fs)
382 {
383         struct ethtool_tcpip4_spec *l4_mask;
384         struct ethtool_usrip4_spec *l3_mask;
385         struct ethhdr *eth_mask;
386         int num_tuples = 0;
387
388         if (fs->location >= MAX_NUM_OF_ETHTOOL_RULES)
389                 return -EINVAL;
390
391         if (fs->ring_cookie >= priv->params.num_channels &&
392             fs->ring_cookie != RX_CLS_FLOW_DISC)
393                 return -EINVAL;
394
395         switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
396         case ETHER_FLOW:
397                 eth_mask = &fs->m_u.ether_spec;
398                 if (!is_zero_ether_addr(eth_mask->h_dest))
399                         num_tuples++;
400                 if (!is_zero_ether_addr(eth_mask->h_source))
401                         num_tuples++;
402                 if (eth_mask->h_proto)
403                         num_tuples++;
404                 break;
405         case TCP_V4_FLOW:
406         case UDP_V4_FLOW:
407                 if (fs->m_u.tcp_ip4_spec.tos)
408                         return -EINVAL;
409                 l4_mask = &fs->m_u.tcp_ip4_spec;
410                 if (l4_mask->ip4src) {
411                         if (!all_ones(l4_mask->ip4src))
412                                 return -EINVAL;
413                         num_tuples++;
414                 }
415                 if (l4_mask->ip4dst) {
416                         if (!all_ones(l4_mask->ip4dst))
417                                 return -EINVAL;
418                         num_tuples++;
419                 }
420                 if (l4_mask->psrc) {
421                         if (!all_ones(l4_mask->psrc))
422                                 return -EINVAL;
423                         num_tuples++;
424                 }
425                 if (l4_mask->pdst) {
426                         if (!all_ones(l4_mask->pdst))
427                                 return -EINVAL;
428                         num_tuples++;
429                 }
430                 /* Flow is TCP/UDP */
431                 num_tuples++;
432                 break;
433         case IP_USER_FLOW:
434                 l3_mask = &fs->m_u.usr_ip4_spec;
435                 if (l3_mask->l4_4_bytes || l3_mask->tos || l3_mask->proto ||
436                     fs->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
437                         return -EINVAL;
438                 if (l3_mask->ip4src) {
439                         if (!all_ones(l3_mask->ip4src))
440                                 return -EINVAL;
441                         num_tuples++;
442                 }
443                 if (l3_mask->ip4dst) {
444                         if (!all_ones(l3_mask->ip4dst))
445                                 return -EINVAL;
446                         num_tuples++;
447                 }
448                 /* Flow is IPv4 */
449                 num_tuples++;
450                 break;
451         default:
452                 return -EINVAL;
453         }
454         if ((fs->flow_type & FLOW_EXT)) {
455                 if (fs->m_ext.vlan_etype ||
456                     (fs->m_ext.vlan_tci != cpu_to_be16(VLAN_VID_MASK)))
457                         return -EINVAL;
458
459                 if (fs->m_ext.vlan_tci) {
460                         if (be16_to_cpu(fs->h_ext.vlan_tci) >= VLAN_N_VID)
461                                 return -EINVAL;
462                 }
463                 num_tuples++;
464         }
465
466         if (fs->flow_type & FLOW_MAC_EXT &&
467             !is_zero_ether_addr(fs->m_ext.h_dest))
468                 num_tuples++;
469
470         return num_tuples;
471 }
472
473 int mlx5e_ethtool_flow_replace(struct mlx5e_priv *priv,
474                                struct ethtool_rx_flow_spec *fs)
475 {
476         struct mlx5e_ethtool_table *eth_ft;
477         struct mlx5e_ethtool_rule *eth_rule;
478         struct mlx5_flow_rule *rule;
479         int num_tuples;
480         int err;
481
482         num_tuples = validate_flow(priv, fs);
483         if (num_tuples <= 0) {
484                 netdev_warn(priv->netdev, "%s: flow is not valid\n",  __func__);
485                 return -EINVAL;
486         }
487
488         eth_ft = get_flow_table(priv, fs, num_tuples);
489         if (IS_ERR(eth_ft))
490                 return PTR_ERR(eth_ft);
491
492         eth_rule = get_ethtool_rule(priv, fs->location);
493         if (IS_ERR(eth_rule)) {
494                 put_flow_table(eth_ft);
495                 return PTR_ERR(eth_rule);
496         }
497
498         eth_rule->flow_spec = *fs;
499         eth_rule->eth_ft = eth_ft;
500         if (!eth_ft->ft) {
501                 err = -EINVAL;
502                 goto del_ethtool_rule;
503         }
504         rule = add_ethtool_flow_rule(priv, eth_ft->ft, fs);
505         if (IS_ERR(rule)) {
506                 err = PTR_ERR(rule);
507                 goto del_ethtool_rule;
508         }
509
510         eth_rule->rule = rule;
511
512         return 0;
513
514 del_ethtool_rule:
515         del_ethtool_rule(priv, eth_rule);
516
517         return err;
518 }
519
520 int mlx5e_ethtool_flow_remove(struct mlx5e_priv *priv,
521                               int location)
522 {
523         struct mlx5e_ethtool_rule *eth_rule;
524         int err = 0;
525
526         if (location >= MAX_NUM_OF_ETHTOOL_RULES)
527                 return -ENOSPC;
528
529         eth_rule = find_ethtool_rule(priv, location);
530         if (!eth_rule) {
531                 err =  -ENOENT;
532                 goto out;
533         }
534
535         del_ethtool_rule(priv, eth_rule);
536 out:
537         return err;
538 }
539
540 void mlx5e_ethtool_cleanup_steering(struct mlx5e_priv *priv)
541 {
542         struct mlx5e_ethtool_rule *iter;
543         struct mlx5e_ethtool_rule *temp;
544
545         list_for_each_entry_safe(iter, temp, &priv->fs.ethtool.rules, list)
546                 del_ethtool_rule(priv, iter);
547 }
548
549 void mlx5e_ethtool_init_steering(struct mlx5e_priv *priv)
550 {
551         INIT_LIST_HEAD(&priv->fs.ethtool.rules);
552 }