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_ENC_KEYID) | \
56 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \
57 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | \
58 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
59 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) | \
60 BIT(FLOW_DISSECTOR_KEY_MPLS) | \
61 BIT(FLOW_DISSECTOR_KEY_IP))
63 #define NFP_FLOWER_WHITELIST_TUN_DISSECTOR \
64 (BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
65 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | \
66 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \
67 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | \
68 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))
70 #define NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R \
71 (BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
72 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \
73 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))
76 nfp_flower_xmit_flow(struct net_device *netdev,
77 struct nfp_fl_payload *nfp_flow, u8 mtype)
79 u32 meta_len, key_len, mask_len, act_len, tot_len;
80 struct nfp_repr *priv = netdev_priv(netdev);
84 meta_len = sizeof(struct nfp_fl_rule_metadata);
85 key_len = nfp_flow->meta.key_len;
86 mask_len = nfp_flow->meta.mask_len;
87 act_len = nfp_flow->meta.act_len;
89 tot_len = meta_len + key_len + mask_len + act_len;
91 /* Convert to long words as firmware expects
92 * lengths in units of NFP_FL_LW_SIZ.
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 skb = nfp_flower_cmsg_alloc(priv->app, tot_len, mtype, GFP_KERNEL);
102 msg = nfp_flower_cmsg_get_data(skb);
103 memcpy(msg, &nfp_flow->meta, meta_len);
104 memcpy(&msg[meta_len], nfp_flow->unmasked_data, key_len);
105 memcpy(&msg[meta_len + key_len], nfp_flow->mask_data, mask_len);
106 memcpy(&msg[meta_len + key_len + mask_len],
107 nfp_flow->action_data, act_len);
109 /* Convert back to bytes as software expects
110 * lengths in units of bytes.
112 nfp_flow->meta.key_len <<= NFP_FL_LW_SIZ;
113 nfp_flow->meta.mask_len <<= NFP_FL_LW_SIZ;
114 nfp_flow->meta.act_len <<= NFP_FL_LW_SIZ;
116 nfp_ctrl_tx(priv->app->ctrl, skb);
121 static bool nfp_flower_check_higher_than_mac(struct tc_cls_flower_offload *f)
123 return dissector_uses_key(f->dissector,
124 FLOW_DISSECTOR_KEY_IPV4_ADDRS) ||
125 dissector_uses_key(f->dissector,
126 FLOW_DISSECTOR_KEY_IPV6_ADDRS) ||
127 dissector_uses_key(f->dissector,
128 FLOW_DISSECTOR_KEY_PORTS) ||
129 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ICMP);
133 nfp_flower_calculate_key_layers(struct nfp_app *app,
134 struct nfp_fl_key_ls *ret_key_ls,
135 struct tc_cls_flower_offload *flow,
137 enum nfp_flower_tun_type *tun_type)
139 struct flow_dissector_key_basic *mask_basic = NULL;
140 struct flow_dissector_key_basic *key_basic = NULL;
141 struct nfp_flower_priv *priv = app->priv;
146 if (flow->dissector->used_keys & ~NFP_FLOWER_WHITELIST_DISSECTOR)
149 /* If any tun dissector is used then the required set must be used. */
150 if (flow->dissector->used_keys & NFP_FLOWER_WHITELIST_TUN_DISSECTOR &&
151 (flow->dissector->used_keys & NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R)
152 != NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R)
156 key_layer = NFP_FLOWER_LAYER_PORT;
157 key_size = sizeof(struct nfp_flower_meta_tci) +
158 sizeof(struct nfp_flower_in_port);
160 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS) ||
161 dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_MPLS)) {
162 key_layer |= NFP_FLOWER_LAYER_MAC;
163 key_size += sizeof(struct nfp_flower_mac_mpls);
166 if (dissector_uses_key(flow->dissector,
167 FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
168 struct flow_dissector_key_ipv4_addrs *mask_ipv4 = NULL;
169 struct flow_dissector_key_ports *mask_enc_ports = NULL;
170 struct flow_dissector_key_ports *enc_ports = NULL;
171 struct flow_dissector_key_control *mask_enc_ctl =
172 skb_flow_dissector_target(flow->dissector,
173 FLOW_DISSECTOR_KEY_ENC_CONTROL,
175 struct flow_dissector_key_control *enc_ctl =
176 skb_flow_dissector_target(flow->dissector,
177 FLOW_DISSECTOR_KEY_ENC_CONTROL,
182 if (mask_enc_ctl->addr_type != 0xffff ||
183 enc_ctl->addr_type != FLOW_DISSECTOR_KEY_IPV4_ADDRS)
186 /* These fields are already verified as used. */
188 skb_flow_dissector_target(flow->dissector,
189 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
191 if (mask_ipv4->dst != cpu_to_be32(~0))
195 skb_flow_dissector_target(flow->dissector,
196 FLOW_DISSECTOR_KEY_ENC_PORTS,
199 skb_flow_dissector_target(flow->dissector,
200 FLOW_DISSECTOR_KEY_ENC_PORTS,
203 if (mask_enc_ports->dst != cpu_to_be16(~0))
206 switch (enc_ports->dst) {
207 case htons(NFP_FL_VXLAN_PORT):
208 *tun_type = NFP_FL_TUNNEL_VXLAN;
209 key_layer |= NFP_FLOWER_LAYER_VXLAN;
210 key_size += sizeof(struct nfp_flower_ipv4_udp_tun);
212 case htons(NFP_FL_GENEVE_PORT):
213 if (!(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE))
215 *tun_type = NFP_FL_TUNNEL_GENEVE;
216 key_layer |= NFP_FLOWER_LAYER_EXT_META;
217 key_size += sizeof(struct nfp_flower_ext_meta);
218 key_layer_two |= NFP_FLOWER_LAYER2_GENEVE;
219 key_size += sizeof(struct nfp_flower_ipv4_udp_tun);
225 /* Reject non tunnel matches offloaded to egress repr. */
229 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
230 mask_basic = skb_flow_dissector_target(flow->dissector,
231 FLOW_DISSECTOR_KEY_BASIC,
234 key_basic = skb_flow_dissector_target(flow->dissector,
235 FLOW_DISSECTOR_KEY_BASIC,
239 if (mask_basic && mask_basic->n_proto) {
240 /* Ethernet type is present in the key. */
241 switch (key_basic->n_proto) {
242 case cpu_to_be16(ETH_P_IP):
243 key_layer |= NFP_FLOWER_LAYER_IPV4;
244 key_size += sizeof(struct nfp_flower_ipv4);
247 case cpu_to_be16(ETH_P_IPV6):
248 key_layer |= NFP_FLOWER_LAYER_IPV6;
249 key_size += sizeof(struct nfp_flower_ipv6);
252 /* Currently we do not offload ARP
253 * because we rely on it to get to the host.
255 case cpu_to_be16(ETH_P_ARP):
258 /* Will be included in layer 2. */
259 case cpu_to_be16(ETH_P_8021Q):
263 /* Other ethtype - we need check the masks for the
264 * remainder of the key to ensure we can offload.
266 if (nfp_flower_check_higher_than_mac(flow))
272 if (mask_basic && mask_basic->ip_proto) {
273 /* Ethernet type is present in the key. */
274 switch (key_basic->ip_proto) {
280 key_layer |= NFP_FLOWER_LAYER_TP;
281 key_size += sizeof(struct nfp_flower_tp_ports);
284 /* Other ip proto - we need check the masks for the
285 * remainder of the key to ensure we can offload.
291 ret_key_ls->key_layer = key_layer;
292 ret_key_ls->key_layer_two = key_layer_two;
293 ret_key_ls->key_size = key_size;
298 static struct nfp_fl_payload *
299 nfp_flower_allocate_new(struct nfp_fl_key_ls *key_layer)
301 struct nfp_fl_payload *flow_pay;
303 flow_pay = kmalloc(sizeof(*flow_pay), GFP_KERNEL);
307 flow_pay->meta.key_len = key_layer->key_size;
308 flow_pay->unmasked_data = kmalloc(key_layer->key_size, GFP_KERNEL);
309 if (!flow_pay->unmasked_data)
312 flow_pay->meta.mask_len = key_layer->key_size;
313 flow_pay->mask_data = kmalloc(key_layer->key_size, GFP_KERNEL);
314 if (!flow_pay->mask_data)
315 goto err_free_unmasked;
317 flow_pay->action_data = kmalloc(NFP_FL_MAX_A_SIZ, GFP_KERNEL);
318 if (!flow_pay->action_data)
321 flow_pay->nfp_tun_ipv4_addr = 0;
322 flow_pay->meta.flags = 0;
323 spin_lock_init(&flow_pay->lock);
328 kfree(flow_pay->mask_data);
330 kfree(flow_pay->unmasked_data);
337 * nfp_flower_add_offload() - Adds a new flow to hardware.
338 * @app: Pointer to the APP handle
339 * @netdev: netdev structure.
340 * @flow: TC flower classifier offload structure.
341 * @egress: NFP netdev is the egress.
343 * Adds a new flow to the repeated hash structure and action payload.
345 * Return: negative value on error, 0 if configured successfully.
348 nfp_flower_add_offload(struct nfp_app *app, struct net_device *netdev,
349 struct tc_cls_flower_offload *flow, bool egress)
351 enum nfp_flower_tun_type tun_type = NFP_FL_TUNNEL_NONE;
352 struct nfp_flower_priv *priv = app->priv;
353 struct nfp_fl_payload *flow_pay;
354 struct nfp_fl_key_ls *key_layer;
357 key_layer = kmalloc(sizeof(*key_layer), GFP_KERNEL);
361 err = nfp_flower_calculate_key_layers(app, key_layer, flow, egress,
364 goto err_free_key_ls;
366 flow_pay = nfp_flower_allocate_new(key_layer);
369 goto err_free_key_ls;
372 err = nfp_flower_compile_flow_match(flow, key_layer, netdev, flow_pay,
375 goto err_destroy_flow;
377 err = nfp_flower_compile_action(flow, netdev, flow_pay);
379 goto err_destroy_flow;
381 err = nfp_compile_flow_metadata(app, flow, flow_pay);
383 goto err_destroy_flow;
385 err = nfp_flower_xmit_flow(netdev, flow_pay,
386 NFP_FLOWER_CMSG_TYPE_FLOW_ADD);
388 goto err_destroy_flow;
390 INIT_HLIST_NODE(&flow_pay->link);
391 flow_pay->tc_flower_cookie = flow->cookie;
392 hash_add_rcu(priv->flow_table, &flow_pay->link, flow->cookie);
394 /* Deallocate flow payload when flower rule has been destroyed. */
400 kfree(flow_pay->action_data);
401 kfree(flow_pay->mask_data);
402 kfree(flow_pay->unmasked_data);
410 * nfp_flower_del_offload() - Removes a flow from hardware.
411 * @app: Pointer to the APP handle
412 * @netdev: netdev structure.
413 * @flow: TC flower classifier offload structure
415 * Removes a flow from the repeated hash structure and clears the
418 * Return: negative value on error, 0 if removed successfully.
421 nfp_flower_del_offload(struct nfp_app *app, struct net_device *netdev,
422 struct tc_cls_flower_offload *flow)
424 struct nfp_fl_payload *nfp_flow;
427 nfp_flow = nfp_flower_search_fl_table(app, flow->cookie);
431 err = nfp_modify_flow_metadata(app, nfp_flow);
435 if (nfp_flow->nfp_tun_ipv4_addr)
436 nfp_tunnel_del_ipv4_off(app, nfp_flow->nfp_tun_ipv4_addr);
438 err = nfp_flower_xmit_flow(netdev, nfp_flow,
439 NFP_FLOWER_CMSG_TYPE_FLOW_DEL);
444 hash_del_rcu(&nfp_flow->link);
445 kfree(nfp_flow->action_data);
446 kfree(nfp_flow->mask_data);
447 kfree(nfp_flow->unmasked_data);
448 kfree_rcu(nfp_flow, rcu);
453 * nfp_flower_get_stats() - Populates flow stats obtained from hardware.
454 * @app: Pointer to the APP handle
455 * @flow: TC flower classifier offload structure
457 * Populates a flow statistics structure which which corresponds to a
460 * Return: negative value on error, 0 if stats populated successfully.
463 nfp_flower_get_stats(struct nfp_app *app, struct tc_cls_flower_offload *flow)
465 struct nfp_fl_payload *nfp_flow;
467 nfp_flow = nfp_flower_search_fl_table(app, flow->cookie);
471 spin_lock_bh(&nfp_flow->lock);
472 tcf_exts_stats_update(flow->exts, nfp_flow->stats.bytes,
473 nfp_flow->stats.pkts, nfp_flow->stats.used);
475 nfp_flow->stats.pkts = 0;
476 nfp_flow->stats.bytes = 0;
477 spin_unlock_bh(&nfp_flow->lock);
483 nfp_flower_repr_offload(struct nfp_app *app, struct net_device *netdev,
484 struct tc_cls_flower_offload *flower, bool egress)
486 if (!eth_proto_is_802_3(flower->common.protocol))
489 switch (flower->command) {
490 case TC_CLSFLOWER_REPLACE:
491 return nfp_flower_add_offload(app, netdev, flower, egress);
492 case TC_CLSFLOWER_DESTROY:
493 return nfp_flower_del_offload(app, netdev, flower);
494 case TC_CLSFLOWER_STATS:
495 return nfp_flower_get_stats(app, flower);
501 int nfp_flower_setup_tc_egress_cb(enum tc_setup_type type, void *type_data,
504 struct nfp_repr *repr = cb_priv;
506 if (!tc_cls_can_offload_and_chain0(repr->netdev, type_data))
510 case TC_SETUP_CLSFLOWER:
511 return nfp_flower_repr_offload(repr->app, repr->netdev,
518 static int nfp_flower_setup_tc_block_cb(enum tc_setup_type type,
519 void *type_data, void *cb_priv)
521 struct nfp_repr *repr = cb_priv;
523 if (!tc_cls_can_offload_and_chain0(repr->netdev, type_data))
527 case TC_SETUP_CLSFLOWER:
528 return nfp_flower_repr_offload(repr->app, repr->netdev,
535 static int nfp_flower_setup_tc_block(struct net_device *netdev,
536 struct tc_block_offload *f)
538 struct nfp_repr *repr = netdev_priv(netdev);
540 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
543 switch (f->command) {
545 return tcf_block_cb_register(f->block,
546 nfp_flower_setup_tc_block_cb,
548 case TC_BLOCK_UNBIND:
549 tcf_block_cb_unregister(f->block,
550 nfp_flower_setup_tc_block_cb,
558 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
559 enum tc_setup_type type, void *type_data)
563 return nfp_flower_setup_tc_block(netdev, type_data);