2 * Copyright (C) 2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/skbuff.h>
35 #include <net/devlink.h>
36 #include <net/pkt_cls.h>
40 #include "../nfpcore/nfp_cpp.h"
41 #include "../nfpcore/nfp_nsp.h"
42 #include "../nfp_app.h"
43 #include "../nfp_main.h"
44 #include "../nfp_net.h"
45 #include "../nfp_port.h"
47 #define NFP_FLOWER_WHITELIST_DISSECTOR \
48 (BIT(FLOW_DISSECTOR_KEY_CONTROL) | \
49 BIT(FLOW_DISSECTOR_KEY_BASIC) | \
50 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) | \
51 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) | \
52 BIT(FLOW_DISSECTOR_KEY_PORTS) | \
53 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) | \
54 BIT(FLOW_DISSECTOR_KEY_VLAN) | \
55 BIT(FLOW_DISSECTOR_KEY_IP))
58 nfp_flower_xmit_flow(struct net_device *netdev,
59 struct nfp_fl_payload *nfp_flow, u8 mtype)
61 u32 meta_len, key_len, mask_len, act_len, tot_len;
62 struct nfp_repr *priv = netdev_priv(netdev);
66 meta_len = sizeof(struct nfp_fl_rule_metadata);
67 key_len = nfp_flow->meta.key_len;
68 mask_len = nfp_flow->meta.mask_len;
69 act_len = nfp_flow->meta.act_len;
71 tot_len = meta_len + key_len + mask_len + act_len;
73 /* Convert to long words as firmware expects
74 * lengths in units of NFP_FL_LW_SIZ.
76 nfp_flow->meta.key_len >>= NFP_FL_LW_SIZ;
77 nfp_flow->meta.mask_len >>= NFP_FL_LW_SIZ;
78 nfp_flow->meta.act_len >>= NFP_FL_LW_SIZ;
80 skb = nfp_flower_cmsg_alloc(priv->app, tot_len, mtype);
84 msg = nfp_flower_cmsg_get_data(skb);
85 memcpy(msg, &nfp_flow->meta, meta_len);
86 memcpy(&msg[meta_len], nfp_flow->unmasked_data, key_len);
87 memcpy(&msg[meta_len + key_len], nfp_flow->mask_data, mask_len);
88 memcpy(&msg[meta_len + key_len + mask_len],
89 nfp_flow->action_data, act_len);
91 /* Convert back to bytes as software expects
92 * lengths in units of bytes.
94 nfp_flow->meta.key_len <<= NFP_FL_LW_SIZ;
95 nfp_flow->meta.mask_len <<= NFP_FL_LW_SIZ;
96 nfp_flow->meta.act_len <<= NFP_FL_LW_SIZ;
98 nfp_ctrl_tx(priv->app->ctrl, skb);
103 static bool nfp_flower_check_higher_than_mac(struct tc_cls_flower_offload *f)
105 return dissector_uses_key(f->dissector,
106 FLOW_DISSECTOR_KEY_IPV4_ADDRS) ||
107 dissector_uses_key(f->dissector,
108 FLOW_DISSECTOR_KEY_IPV6_ADDRS) ||
109 dissector_uses_key(f->dissector,
110 FLOW_DISSECTOR_KEY_PORTS) ||
111 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ICMP);
115 nfp_flower_calculate_key_layers(struct nfp_fl_key_ls *ret_key_ls,
116 struct tc_cls_flower_offload *flow)
118 struct flow_dissector_key_basic *mask_basic = NULL;
119 struct flow_dissector_key_basic *key_basic = NULL;
120 struct flow_dissector_key_ip *mask_ip = NULL;
125 if (flow->dissector->used_keys & ~NFP_FLOWER_WHITELIST_DISSECTOR)
128 if (dissector_uses_key(flow->dissector,
129 FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
130 struct flow_dissector_key_control *mask_enc_ctl =
131 skb_flow_dissector_target(flow->dissector,
132 FLOW_DISSECTOR_KEY_ENC_CONTROL,
134 /* We are expecting a tunnel. For now we ignore offloading. */
135 if (mask_enc_ctl->addr_type)
139 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
140 mask_basic = skb_flow_dissector_target(flow->dissector,
141 FLOW_DISSECTOR_KEY_BASIC,
144 key_basic = skb_flow_dissector_target(flow->dissector,
145 FLOW_DISSECTOR_KEY_BASIC,
149 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_IP))
150 mask_ip = skb_flow_dissector_target(flow->dissector,
151 FLOW_DISSECTOR_KEY_IP,
155 key_layer = NFP_FLOWER_LAYER_PORT | NFP_FLOWER_LAYER_MAC;
156 key_size = sizeof(struct nfp_flower_meta_one) +
157 sizeof(struct nfp_flower_in_port) +
158 sizeof(struct nfp_flower_mac_mpls);
160 if (mask_basic && mask_basic->n_proto) {
161 /* Ethernet type is present in the key. */
162 switch (key_basic->n_proto) {
163 case cpu_to_be16(ETH_P_IP):
164 if (mask_ip && mask_ip->tos)
166 if (mask_ip && mask_ip->ttl)
168 key_layer |= NFP_FLOWER_LAYER_IPV4;
169 key_size += sizeof(struct nfp_flower_ipv4);
172 case cpu_to_be16(ETH_P_IPV6):
173 if (mask_ip && mask_ip->tos)
175 if (mask_ip && mask_ip->ttl)
177 key_layer |= NFP_FLOWER_LAYER_IPV6;
178 key_size += sizeof(struct nfp_flower_ipv6);
181 /* Currently we do not offload ARP
182 * because we rely on it to get to the host.
184 case cpu_to_be16(ETH_P_ARP):
187 /* Currently we do not offload MPLS. */
188 case cpu_to_be16(ETH_P_MPLS_UC):
189 case cpu_to_be16(ETH_P_MPLS_MC):
192 /* Will be included in layer 2. */
193 case cpu_to_be16(ETH_P_8021Q):
197 /* Other ethtype - we need check the masks for the
198 * remainder of the key to ensure we can offload.
200 if (nfp_flower_check_higher_than_mac(flow))
206 if (mask_basic && mask_basic->ip_proto) {
207 /* Ethernet type is present in the key. */
208 switch (key_basic->ip_proto) {
214 key_layer |= NFP_FLOWER_LAYER_TP;
215 key_size += sizeof(struct nfp_flower_tp_ports);
218 /* Other ip proto - we need check the masks for the
219 * remainder of the key to ensure we can offload.
225 ret_key_ls->key_layer = key_layer;
226 ret_key_ls->key_layer_two = key_layer_two;
227 ret_key_ls->key_size = key_size;
232 static struct nfp_fl_payload *
233 nfp_flower_allocate_new(struct nfp_fl_key_ls *key_layer)
235 struct nfp_fl_payload *flow_pay;
237 flow_pay = kmalloc(sizeof(*flow_pay), GFP_KERNEL);
241 flow_pay->meta.key_len = key_layer->key_size;
242 flow_pay->unmasked_data = kmalloc(key_layer->key_size, GFP_KERNEL);
243 if (!flow_pay->unmasked_data)
246 flow_pay->meta.mask_len = key_layer->key_size;
247 flow_pay->mask_data = kmalloc(key_layer->key_size, GFP_KERNEL);
248 if (!flow_pay->mask_data)
249 goto err_free_unmasked;
251 flow_pay->action_data = kmalloc(NFP_FL_MAX_A_SIZ, GFP_KERNEL);
252 if (!flow_pay->action_data)
255 flow_pay->meta.flags = 0;
256 spin_lock_init(&flow_pay->lock);
261 kfree(flow_pay->mask_data);
263 kfree(flow_pay->unmasked_data);
270 * nfp_flower_add_offload() - Adds a new flow to hardware.
271 * @app: Pointer to the APP handle
272 * @netdev: netdev structure.
273 * @flow: TC flower classifier offload structure.
275 * Adds a new flow to the repeated hash structure and action payload.
277 * Return: negative value on error, 0 if configured successfully.
280 nfp_flower_add_offload(struct nfp_app *app, struct net_device *netdev,
281 struct tc_cls_flower_offload *flow)
283 struct nfp_flower_priv *priv = app->priv;
284 struct nfp_fl_payload *flow_pay;
285 struct nfp_fl_key_ls *key_layer;
288 key_layer = kmalloc(sizeof(*key_layer), GFP_KERNEL);
292 err = nfp_flower_calculate_key_layers(key_layer, flow);
294 goto err_free_key_ls;
296 flow_pay = nfp_flower_allocate_new(key_layer);
299 goto err_free_key_ls;
302 err = nfp_flower_compile_flow_match(flow, key_layer, netdev, flow_pay);
304 goto err_destroy_flow;
306 err = nfp_flower_compile_action(flow, netdev, flow_pay);
308 goto err_destroy_flow;
310 err = nfp_compile_flow_metadata(app, flow, flow_pay);
312 goto err_destroy_flow;
314 err = nfp_flower_xmit_flow(netdev, flow_pay,
315 NFP_FLOWER_CMSG_TYPE_FLOW_ADD);
317 goto err_destroy_flow;
319 INIT_HLIST_NODE(&flow_pay->link);
320 flow_pay->tc_flower_cookie = flow->cookie;
321 hash_add_rcu(priv->flow_table, &flow_pay->link, flow->cookie);
323 /* Deallocate flow payload when flower rule has been destroyed. */
329 kfree(flow_pay->action_data);
330 kfree(flow_pay->mask_data);
331 kfree(flow_pay->unmasked_data);
339 * nfp_flower_del_offload() - Removes a flow from hardware.
340 * @app: Pointer to the APP handle
341 * @netdev: netdev structure.
342 * @flow: TC flower classifier offload structure
344 * Removes a flow from the repeated hash structure and clears the
347 * Return: negative value on error, 0 if removed successfully.
350 nfp_flower_del_offload(struct nfp_app *app, struct net_device *netdev,
351 struct tc_cls_flower_offload *flow)
353 struct nfp_fl_payload *nfp_flow;
356 nfp_flow = nfp_flower_search_fl_table(app, flow->cookie);
360 err = nfp_modify_flow_metadata(app, nfp_flow);
364 err = nfp_flower_xmit_flow(netdev, nfp_flow,
365 NFP_FLOWER_CMSG_TYPE_FLOW_DEL);
370 hash_del_rcu(&nfp_flow->link);
371 kfree(nfp_flow->action_data);
372 kfree(nfp_flow->mask_data);
373 kfree(nfp_flow->unmasked_data);
374 kfree_rcu(nfp_flow, rcu);
379 * nfp_flower_get_stats() - Populates flow stats obtained from hardware.
380 * @app: Pointer to the APP handle
381 * @flow: TC flower classifier offload structure
383 * Populates a flow statistics structure which which corresponds to a
386 * Return: negative value on error, 0 if stats populated successfully.
389 nfp_flower_get_stats(struct nfp_app *app, struct tc_cls_flower_offload *flow)
391 struct nfp_fl_payload *nfp_flow;
393 nfp_flow = nfp_flower_search_fl_table(app, flow->cookie);
397 spin_lock_bh(&nfp_flow->lock);
398 tcf_exts_stats_update(flow->exts, nfp_flow->stats.bytes,
399 nfp_flow->stats.pkts, nfp_flow->stats.used);
401 nfp_flow->stats.pkts = 0;
402 nfp_flow->stats.bytes = 0;
403 spin_unlock_bh(&nfp_flow->lock);
409 nfp_flower_repr_offload(struct nfp_app *app, struct net_device *netdev,
410 struct tc_cls_flower_offload *flower)
412 switch (flower->command) {
413 case TC_CLSFLOWER_REPLACE:
414 return nfp_flower_add_offload(app, netdev, flower);
415 case TC_CLSFLOWER_DESTROY:
416 return nfp_flower_del_offload(app, netdev, flower);
417 case TC_CLSFLOWER_STATS:
418 return nfp_flower_get_stats(app, flower);
424 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
425 enum tc_setup_type type, void *type_data)
427 struct tc_cls_flower_offload *cls_flower = type_data;
429 if (type != TC_SETUP_CLSFLOWER ||
430 !is_classid_clsact_ingress(cls_flower->common.classid) ||
431 !eth_proto_is_802_3(cls_flower->common.protocol) ||
432 cls_flower->common.chain_index)
435 return nfp_flower_repr_offload(app, netdev, cls_flower);