mlxsw: spectrum_flower: Add support for ip ttl
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_flower.c
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/spectrum_flower.c
3  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the names of the copyright holders nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/netdevice.h>
38 #include <net/flow_dissector.h>
39 #include <net/pkt_cls.h>
40 #include <net/tc_act/tc_gact.h>
41 #include <net/tc_act/tc_mirred.h>
42 #include <net/tc_act/tc_vlan.h>
43
44 #include "spectrum.h"
45 #include "core_acl_flex_keys.h"
46
47 static int mlxsw_sp_flower_parse_actions(struct mlxsw_sp *mlxsw_sp,
48                                          struct net_device *dev,
49                                          struct mlxsw_sp_acl_rule_info *rulei,
50                                          struct tcf_exts *exts)
51 {
52         const struct tc_action *a;
53         LIST_HEAD(actions);
54         int err;
55
56         if (tc_no_actions(exts))
57                 return 0;
58
59         /* Count action is inserted first */
60         err = mlxsw_sp_acl_rulei_act_count(mlxsw_sp, rulei);
61         if (err)
62                 return err;
63
64         tcf_exts_to_list(exts, &actions);
65         list_for_each_entry(a, &actions, list) {
66                 if (is_tcf_gact_shot(a)) {
67                         err = mlxsw_sp_acl_rulei_act_drop(rulei);
68                         if (err)
69                                 return err;
70                 } else if (is_tcf_gact_trap(a)) {
71                         err = mlxsw_sp_acl_rulei_act_trap(rulei);
72                         if (err)
73                                 return err;
74                 } else if (is_tcf_mirred_egress_redirect(a)) {
75                         int ifindex = tcf_mirred_ifindex(a);
76                         struct net_device *out_dev;
77                         struct mlxsw_sp_fid *fid;
78                         u16 fid_index;
79
80                         fid = mlxsw_sp_acl_dummy_fid(mlxsw_sp);
81                         fid_index = mlxsw_sp_fid_index(fid);
82                         err = mlxsw_sp_acl_rulei_act_fid_set(mlxsw_sp, rulei,
83                                                              fid_index);
84                         if (err)
85                                 return err;
86
87                         out_dev = __dev_get_by_index(dev_net(dev), ifindex);
88                         if (out_dev == dev)
89                                 out_dev = NULL;
90
91                         err = mlxsw_sp_acl_rulei_act_fwd(mlxsw_sp, rulei,
92                                                          out_dev);
93                         if (err)
94                                 return err;
95                 } else if (is_tcf_vlan(a)) {
96                         u16 proto = be16_to_cpu(tcf_vlan_push_proto(a));
97                         u32 action = tcf_vlan_action(a);
98                         u8 prio = tcf_vlan_push_prio(a);
99                         u16 vid = tcf_vlan_push_vid(a);
100
101                         return mlxsw_sp_acl_rulei_act_vlan(mlxsw_sp, rulei,
102                                                            action, vid,
103                                                            proto, prio);
104                 } else {
105                         dev_err(mlxsw_sp->bus_info->dev, "Unsupported action\n");
106                         return -EOPNOTSUPP;
107                 }
108         }
109         return 0;
110 }
111
112 static void mlxsw_sp_flower_parse_ipv4(struct mlxsw_sp_acl_rule_info *rulei,
113                                        struct tc_cls_flower_offload *f)
114 {
115         struct flow_dissector_key_ipv4_addrs *key =
116                 skb_flow_dissector_target(f->dissector,
117                                           FLOW_DISSECTOR_KEY_IPV4_ADDRS,
118                                           f->key);
119         struct flow_dissector_key_ipv4_addrs *mask =
120                 skb_flow_dissector_target(f->dissector,
121                                           FLOW_DISSECTOR_KEY_IPV4_ADDRS,
122                                           f->mask);
123
124         mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_SRC_IP4,
125                                        ntohl(key->src), ntohl(mask->src));
126         mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_DST_IP4,
127                                        ntohl(key->dst), ntohl(mask->dst));
128 }
129
130 static void mlxsw_sp_flower_parse_ipv6(struct mlxsw_sp_acl_rule_info *rulei,
131                                        struct tc_cls_flower_offload *f)
132 {
133         struct flow_dissector_key_ipv6_addrs *key =
134                 skb_flow_dissector_target(f->dissector,
135                                           FLOW_DISSECTOR_KEY_IPV6_ADDRS,
136                                           f->key);
137         struct flow_dissector_key_ipv6_addrs *mask =
138                 skb_flow_dissector_target(f->dissector,
139                                           FLOW_DISSECTOR_KEY_IPV6_ADDRS,
140                                           f->mask);
141         size_t addr_half_size = sizeof(key->src) / 2;
142
143         mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP6_HI,
144                                        &key->src.s6_addr[0],
145                                        &mask->src.s6_addr[0],
146                                        addr_half_size);
147         mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_SRC_IP6_LO,
148                                        &key->src.s6_addr[addr_half_size],
149                                        &mask->src.s6_addr[addr_half_size],
150                                        addr_half_size);
151         mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP6_HI,
152                                        &key->dst.s6_addr[0],
153                                        &mask->dst.s6_addr[0],
154                                        addr_half_size);
155         mlxsw_sp_acl_rulei_keymask_buf(rulei, MLXSW_AFK_ELEMENT_DST_IP6_LO,
156                                        &key->dst.s6_addr[addr_half_size],
157                                        &mask->dst.s6_addr[addr_half_size],
158                                        addr_half_size);
159 }
160
161 static int mlxsw_sp_flower_parse_ports(struct mlxsw_sp *mlxsw_sp,
162                                        struct mlxsw_sp_acl_rule_info *rulei,
163                                        struct tc_cls_flower_offload *f,
164                                        u8 ip_proto)
165 {
166         struct flow_dissector_key_ports *key, *mask;
167
168         if (!dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS))
169                 return 0;
170
171         if (ip_proto != IPPROTO_TCP && ip_proto != IPPROTO_UDP) {
172                 dev_err(mlxsw_sp->bus_info->dev, "Only UDP and TCP keys are supported\n");
173                 return -EINVAL;
174         }
175
176         key = skb_flow_dissector_target(f->dissector,
177                                         FLOW_DISSECTOR_KEY_PORTS,
178                                         f->key);
179         mask = skb_flow_dissector_target(f->dissector,
180                                          FLOW_DISSECTOR_KEY_PORTS,
181                                          f->mask);
182         mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_DST_L4_PORT,
183                                        ntohs(key->dst), ntohs(mask->dst));
184         mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_SRC_L4_PORT,
185                                        ntohs(key->src), ntohs(mask->src));
186         return 0;
187 }
188
189 static int mlxsw_sp_flower_parse_tcp(struct mlxsw_sp *mlxsw_sp,
190                                      struct mlxsw_sp_acl_rule_info *rulei,
191                                      struct tc_cls_flower_offload *f,
192                                      u8 ip_proto)
193 {
194         struct flow_dissector_key_tcp *key, *mask;
195
196         if (!dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_TCP))
197                 return 0;
198
199         if (ip_proto != IPPROTO_TCP) {
200                 dev_err(mlxsw_sp->bus_info->dev, "TCP keys supported only for TCP\n");
201                 return -EINVAL;
202         }
203
204         key = skb_flow_dissector_target(f->dissector,
205                                         FLOW_DISSECTOR_KEY_TCP,
206                                         f->key);
207         mask = skb_flow_dissector_target(f->dissector,
208                                          FLOW_DISSECTOR_KEY_TCP,
209                                          f->mask);
210         mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_TCP_FLAGS,
211                                        ntohs(key->flags), ntohs(mask->flags));
212         return 0;
213 }
214
215 static int mlxsw_sp_flower_parse_ip(struct mlxsw_sp *mlxsw_sp,
216                                     struct mlxsw_sp_acl_rule_info *rulei,
217                                     struct tc_cls_flower_offload *f,
218                                     u16 n_proto)
219 {
220         struct flow_dissector_key_ip *key, *mask;
221
222         if (!dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_IP))
223                 return 0;
224
225         if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6) {
226                 dev_err(mlxsw_sp->bus_info->dev, "IP keys supported only for IPv4/6\n");
227                 return -EINVAL;
228         }
229
230         key = skb_flow_dissector_target(f->dissector,
231                                         FLOW_DISSECTOR_KEY_IP,
232                                         f->key);
233         mask = skb_flow_dissector_target(f->dissector,
234                                          FLOW_DISSECTOR_KEY_IP,
235                                          f->mask);
236         mlxsw_sp_acl_rulei_keymask_u32(rulei, MLXSW_AFK_ELEMENT_IP_TTL_,
237                                        key->ttl, mask->ttl);
238         return 0;
239 }
240
241 static int mlxsw_sp_flower_parse(struct mlxsw_sp *mlxsw_sp,
242                                  struct net_device *dev,
243                                  struct mlxsw_sp_acl_rule_info *rulei,
244                                  struct tc_cls_flower_offload *f)
245 {
246         u16 n_proto_mask = 0;
247         u16 n_proto_key = 0;
248         u16 addr_type = 0;
249         u8 ip_proto = 0;
250         int err;
251
252         if (f->dissector->used_keys &
253             ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
254               BIT(FLOW_DISSECTOR_KEY_BASIC) |
255               BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
256               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
257               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
258               BIT(FLOW_DISSECTOR_KEY_PORTS) |
259               BIT(FLOW_DISSECTOR_KEY_TCP) |
260               BIT(FLOW_DISSECTOR_KEY_IP) |
261               BIT(FLOW_DISSECTOR_KEY_VLAN))) {
262                 dev_err(mlxsw_sp->bus_info->dev, "Unsupported key\n");
263                 return -EOPNOTSUPP;
264         }
265
266         mlxsw_sp_acl_rulei_priority(rulei, f->prio);
267
268         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
269                 struct flow_dissector_key_control *key =
270                         skb_flow_dissector_target(f->dissector,
271                                                   FLOW_DISSECTOR_KEY_CONTROL,
272                                                   f->key);
273                 addr_type = key->addr_type;
274         }
275
276         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
277                 struct flow_dissector_key_basic *key =
278                         skb_flow_dissector_target(f->dissector,
279                                                   FLOW_DISSECTOR_KEY_BASIC,
280                                                   f->key);
281                 struct flow_dissector_key_basic *mask =
282                         skb_flow_dissector_target(f->dissector,
283                                                   FLOW_DISSECTOR_KEY_BASIC,
284                                                   f->mask);
285                 n_proto_key = ntohs(key->n_proto);
286                 n_proto_mask = ntohs(mask->n_proto);
287
288                 if (n_proto_key == ETH_P_ALL) {
289                         n_proto_key = 0;
290                         n_proto_mask = 0;
291                 }
292                 mlxsw_sp_acl_rulei_keymask_u32(rulei,
293                                                MLXSW_AFK_ELEMENT_ETHERTYPE,
294                                                n_proto_key, n_proto_mask);
295
296                 ip_proto = key->ip_proto;
297                 mlxsw_sp_acl_rulei_keymask_u32(rulei,
298                                                MLXSW_AFK_ELEMENT_IP_PROTO,
299                                                key->ip_proto, mask->ip_proto);
300         }
301
302         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
303                 struct flow_dissector_key_eth_addrs *key =
304                         skb_flow_dissector_target(f->dissector,
305                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
306                                                   f->key);
307                 struct flow_dissector_key_eth_addrs *mask =
308                         skb_flow_dissector_target(f->dissector,
309                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
310                                                   f->mask);
311
312                 mlxsw_sp_acl_rulei_keymask_buf(rulei,
313                                                MLXSW_AFK_ELEMENT_DMAC,
314                                                key->dst, mask->dst,
315                                                sizeof(key->dst));
316                 mlxsw_sp_acl_rulei_keymask_buf(rulei,
317                                                MLXSW_AFK_ELEMENT_SMAC,
318                                                key->src, mask->src,
319                                                sizeof(key->src));
320         }
321
322         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
323                 struct flow_dissector_key_vlan *key =
324                         skb_flow_dissector_target(f->dissector,
325                                                   FLOW_DISSECTOR_KEY_VLAN,
326                                                   f->key);
327                 struct flow_dissector_key_vlan *mask =
328                         skb_flow_dissector_target(f->dissector,
329                                                   FLOW_DISSECTOR_KEY_VLAN,
330                                                   f->mask);
331                 if (mask->vlan_id != 0)
332                         mlxsw_sp_acl_rulei_keymask_u32(rulei,
333                                                        MLXSW_AFK_ELEMENT_VID,
334                                                        key->vlan_id,
335                                                        mask->vlan_id);
336                 if (mask->vlan_priority != 0)
337                         mlxsw_sp_acl_rulei_keymask_u32(rulei,
338                                                        MLXSW_AFK_ELEMENT_PCP,
339                                                        key->vlan_priority,
340                                                        mask->vlan_priority);
341         }
342
343         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS)
344                 mlxsw_sp_flower_parse_ipv4(rulei, f);
345
346         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS)
347                 mlxsw_sp_flower_parse_ipv6(rulei, f);
348
349         err = mlxsw_sp_flower_parse_ports(mlxsw_sp, rulei, f, ip_proto);
350         if (err)
351                 return err;
352         err = mlxsw_sp_flower_parse_tcp(mlxsw_sp, rulei, f, ip_proto);
353         if (err)
354                 return err;
355
356         err = mlxsw_sp_flower_parse_ip(mlxsw_sp, rulei, f, n_proto_key & n_proto_mask);
357         if (err)
358                 return err;
359
360         return mlxsw_sp_flower_parse_actions(mlxsw_sp, dev, rulei, f->exts);
361 }
362
363 int mlxsw_sp_flower_replace(struct mlxsw_sp_port *mlxsw_sp_port, bool ingress,
364                             __be16 protocol, struct tc_cls_flower_offload *f)
365 {
366         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
367         struct net_device *dev = mlxsw_sp_port->dev;
368         struct mlxsw_sp_acl_rule_info *rulei;
369         struct mlxsw_sp_acl_ruleset *ruleset;
370         struct mlxsw_sp_acl_rule *rule;
371         int err;
372
373         ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, dev, ingress,
374                                            MLXSW_SP_ACL_PROFILE_FLOWER);
375         if (IS_ERR(ruleset))
376                 return PTR_ERR(ruleset);
377
378         rule = mlxsw_sp_acl_rule_create(mlxsw_sp, ruleset, f->cookie);
379         if (IS_ERR(rule)) {
380                 err = PTR_ERR(rule);
381                 goto err_rule_create;
382         }
383
384         rulei = mlxsw_sp_acl_rule_rulei(rule);
385         err = mlxsw_sp_flower_parse(mlxsw_sp, dev, rulei, f);
386         if (err)
387                 goto err_flower_parse;
388
389         err = mlxsw_sp_acl_rulei_commit(rulei);
390         if (err)
391                 goto err_rulei_commit;
392
393         err = mlxsw_sp_acl_rule_add(mlxsw_sp, rule);
394         if (err)
395                 goto err_rule_add;
396
397         mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
398         return 0;
399
400 err_rule_add:
401 err_rulei_commit:
402 err_flower_parse:
403         mlxsw_sp_acl_rule_destroy(mlxsw_sp, rule);
404 err_rule_create:
405         mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
406         return err;
407 }
408
409 void mlxsw_sp_flower_destroy(struct mlxsw_sp_port *mlxsw_sp_port, bool ingress,
410                              struct tc_cls_flower_offload *f)
411 {
412         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
413         struct mlxsw_sp_acl_ruleset *ruleset;
414         struct mlxsw_sp_acl_rule *rule;
415
416         ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, mlxsw_sp_port->dev,
417                                            ingress,
418                                            MLXSW_SP_ACL_PROFILE_FLOWER);
419         if (IS_ERR(ruleset))
420                 return;
421
422         rule = mlxsw_sp_acl_rule_lookup(mlxsw_sp, ruleset, f->cookie);
423         if (rule) {
424                 mlxsw_sp_acl_rule_del(mlxsw_sp, rule);
425                 mlxsw_sp_acl_rule_destroy(mlxsw_sp, rule);
426         }
427
428         mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
429 }
430
431 int mlxsw_sp_flower_stats(struct mlxsw_sp_port *mlxsw_sp_port, bool ingress,
432                           struct tc_cls_flower_offload *f)
433 {
434         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
435         struct mlxsw_sp_acl_ruleset *ruleset;
436         struct mlxsw_sp_acl_rule *rule;
437         u64 packets;
438         u64 lastuse;
439         u64 bytes;
440         int err;
441
442         ruleset = mlxsw_sp_acl_ruleset_get(mlxsw_sp, mlxsw_sp_port->dev,
443                                            ingress,
444                                            MLXSW_SP_ACL_PROFILE_FLOWER);
445         if (WARN_ON(IS_ERR(ruleset)))
446                 return -EINVAL;
447
448         rule = mlxsw_sp_acl_rule_lookup(mlxsw_sp, ruleset, f->cookie);
449         if (!rule)
450                 return -EINVAL;
451
452         err = mlxsw_sp_acl_rule_get_stats(mlxsw_sp, rule, &packets, &bytes,
453                                           &lastuse);
454         if (err)
455                 goto err_rule_get_stats;
456
457         tcf_exts_stats_update(f->exts, bytes, packets, lastuse);
458
459         mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
460         return 0;
461
462 err_rule_get_stats:
463         mlxsw_sp_acl_ruleset_put(mlxsw_sp, ruleset);
464         return err;
465 }