nfp: flower: handle ipv6 tunnel no neigh request
[linux-2.6-microblaze.git] / drivers / net / ethernet / netronome / nfp / flower / main.h
1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
4 #ifndef __NFP_FLOWER_H__
5 #define __NFP_FLOWER_H__ 1
6
7 #include "cmsg.h"
8 #include "../nfp_net.h"
9
10 #include <linux/circ_buf.h>
11 #include <linux/hashtable.h>
12 #include <linux/rhashtable.h>
13 #include <linux/time64.h>
14 #include <linux/types.h>
15 #include <net/pkt_cls.h>
16 #include <net/tcp.h>
17 #include <linux/workqueue.h>
18 #include <linux/idr.h>
19
20 struct nfp_fl_pre_lag;
21 struct net_device;
22 struct nfp_app;
23
24 #define NFP_FL_STAT_ID_MU_NUM           GENMASK(31, 22)
25 #define NFP_FL_STAT_ID_STAT             GENMASK(21, 0)
26
27 #define NFP_FL_STATS_ELEM_RS            FIELD_SIZEOF(struct nfp_fl_stats_id, \
28                                                      init_unalloc)
29 #define NFP_FLOWER_MASK_ENTRY_RS        256
30 #define NFP_FLOWER_MASK_ELEMENT_RS      1
31 #define NFP_FLOWER_MASK_HASH_BITS       10
32
33 #define NFP_FL_META_FLAG_MANAGE_MASK    BIT(7)
34
35 #define NFP_FL_MASK_REUSE_TIME_NS       40000
36 #define NFP_FL_MASK_ID_LOCATION         1
37
38 /* Extra features bitmap. */
39 #define NFP_FL_FEATS_GENEVE             BIT(0)
40 #define NFP_FL_NBI_MTU_SETTING          BIT(1)
41 #define NFP_FL_FEATS_GENEVE_OPT         BIT(2)
42 #define NFP_FL_FEATS_VLAN_PCP           BIT(3)
43 #define NFP_FL_FEATS_VF_RLIM            BIT(4)
44 #define NFP_FL_FEATS_FLOW_MOD           BIT(5)
45 #define NFP_FL_FEATS_PRE_TUN_RULES      BIT(6)
46 #define NFP_FL_FEATS_IPV6_TUN           BIT(7)
47 #define NFP_FL_FEATS_FLOW_MERGE         BIT(30)
48 #define NFP_FL_FEATS_LAG                BIT(31)
49
50 struct nfp_fl_mask_id {
51         struct circ_buf mask_id_free_list;
52         ktime_t *last_used;
53         u8 init_unallocated;
54 };
55
56 struct nfp_fl_stats_id {
57         struct circ_buf free_list;
58         u32 init_unalloc;
59         u8 repeated_em_count;
60 };
61
62 /**
63  * struct nfp_fl_tunnel_offloads - priv data for tunnel offloads
64  * @offloaded_macs:     Hashtable of the offloaded MAC addresses
65  * @ipv4_off_list:      List of IPv4 addresses to offload
66  * @ipv6_off_list:      List of IPv6 addresses to offload
67  * @neigh_off_list:     List of neighbour offloads
68  * @ipv4_off_lock:      Lock for the IPv4 address list
69  * @ipv6_off_lock:      Lock for the IPv6 address list
70  * @neigh_off_lock:     Lock for the neighbour address list
71  * @mac_off_ids:        IDA to manage id assignment for offloaded MACs
72  * @neigh_nb:           Notifier to monitor neighbour state
73  */
74 struct nfp_fl_tunnel_offloads {
75         struct rhashtable offloaded_macs;
76         struct list_head ipv4_off_list;
77         struct list_head ipv6_off_list;
78         struct list_head neigh_off_list;
79         struct mutex ipv4_off_lock;
80         struct mutex ipv6_off_lock;
81         spinlock_t neigh_off_lock;
82         struct ida mac_off_ids;
83         struct notifier_block neigh_nb;
84 };
85
86 /**
87  * struct nfp_mtu_conf - manage MTU setting
88  * @portnum:            NFP port number of repr with requested MTU change
89  * @requested_val:      MTU value requested for repr
90  * @ack:                Received ack that MTU has been correctly set
91  * @wait_q:             Wait queue for MTU acknowledgements
92  * @lock:               Lock for setting/reading MTU variables
93  */
94 struct nfp_mtu_conf {
95         u32 portnum;
96         unsigned int requested_val;
97         bool ack;
98         wait_queue_head_t wait_q;
99         spinlock_t lock;
100 };
101
102 /**
103  * struct nfp_fl_lag - Flower APP priv data for link aggregation
104  * @work:               Work queue for writing configs to the HW
105  * @lock:               Lock to protect lag_group_list
106  * @group_list:         List of all master/slave groups offloaded
107  * @ida_handle:         IDA to handle group ids
108  * @pkt_num:            Incremented for each config packet sent
109  * @batch_ver:          Incremented for each batch of config packets
110  * @global_inst:        Instance allocator for groups
111  * @rst_cfg:            Marker to reset HW LAG config
112  * @retrans_skbs:       Cmsgs that could not be processed by HW and require
113  *                      retransmission
114  */
115 struct nfp_fl_lag {
116         struct delayed_work work;
117         struct mutex lock;
118         struct list_head group_list;
119         struct ida ida_handle;
120         unsigned int pkt_num;
121         unsigned int batch_ver;
122         u8 global_inst;
123         bool rst_cfg;
124         struct sk_buff_head retrans_skbs;
125 };
126
127 /**
128  * struct nfp_fl_internal_ports - Flower APP priv data for additional ports
129  * @port_ids:   Assignment of ids to any additional ports
130  * @lock:       Lock for extra ports list
131  */
132 struct nfp_fl_internal_ports {
133         struct idr port_ids;
134         spinlock_t lock;
135 };
136
137 /**
138  * struct nfp_flower_priv - Flower APP per-vNIC priv data
139  * @app:                Back pointer to app
140  * @nn:                 Pointer to vNIC
141  * @mask_id_seed:       Seed used for mask hash table
142  * @flower_version:     HW version of flower
143  * @flower_ext_feats:   Bitmap of extra features the HW supports
144  * @stats_ids:          List of free stats ids
145  * @mask_ids:           List of free mask ids
146  * @mask_table:         Hash table used to store masks
147  * @stats_ring_size:    Maximum number of allowed stats ids
148  * @flow_table:         Hash table used to store flower rules
149  * @stats:              Stored stats updates for flower rules
150  * @stats_lock:         Lock for flower rule stats updates
151  * @stats_ctx_table:    Hash table to map stats contexts to its flow rule
152  * @cmsg_work:          Workqueue for control messages processing
153  * @cmsg_skbs_high:     List of higher priority skbs for control message
154  *                      processing
155  * @cmsg_skbs_low:      List of lower priority skbs for control message
156  *                      processing
157  * @tun:                Tunnel offload data
158  * @reify_replies:      atomically stores the number of replies received
159  *                      from firmware for repr reify
160  * @reify_wait_queue:   wait queue for repr reify response counting
161  * @mtu_conf:           Configuration of repr MTU value
162  * @nfp_lag:            Link aggregation data block
163  * @indr_block_cb_priv: List of priv data passed to indirect block cbs
164  * @non_repr_priv:      List of offloaded non-repr ports and their priv data
165  * @active_mem_unit:    Current active memory unit for flower rules
166  * @total_mem_units:    Total number of available memory units for flower rules
167  * @internal_ports:     Internal port ids used in offloaded rules
168  * @qos_stats_work:     Workqueue for qos stats processing
169  * @qos_rate_limiters:  Current active qos rate limiters
170  * @qos_stats_lock:     Lock on qos stats updates
171  * @pre_tun_rule_cnt:   Number of pre-tunnel rules offloaded
172  */
173 struct nfp_flower_priv {
174         struct nfp_app *app;
175         struct nfp_net *nn;
176         u32 mask_id_seed;
177         u64 flower_version;
178         u64 flower_ext_feats;
179         struct nfp_fl_stats_id stats_ids;
180         struct nfp_fl_mask_id mask_ids;
181         DECLARE_HASHTABLE(mask_table, NFP_FLOWER_MASK_HASH_BITS);
182         u32 stats_ring_size;
183         struct rhashtable flow_table;
184         struct nfp_fl_stats *stats;
185         spinlock_t stats_lock; /* lock stats */
186         struct rhashtable stats_ctx_table;
187         struct work_struct cmsg_work;
188         struct sk_buff_head cmsg_skbs_high;
189         struct sk_buff_head cmsg_skbs_low;
190         struct nfp_fl_tunnel_offloads tun;
191         atomic_t reify_replies;
192         wait_queue_head_t reify_wait_queue;
193         struct nfp_mtu_conf mtu_conf;
194         struct nfp_fl_lag nfp_lag;
195         struct list_head indr_block_cb_priv;
196         struct list_head non_repr_priv;
197         unsigned int active_mem_unit;
198         unsigned int total_mem_units;
199         struct nfp_fl_internal_ports internal_ports;
200         struct delayed_work qos_stats_work;
201         unsigned int qos_rate_limiters;
202         spinlock_t qos_stats_lock; /* Protect the qos stats */
203         int pre_tun_rule_cnt;
204 };
205
206 /**
207  * struct nfp_fl_qos - Flower APP priv data for quality of service
208  * @netdev_port_id:     NFP port number of repr with qos info
209  * @curr_stats:         Currently stored stats updates for qos info
210  * @prev_stats:         Previously stored updates for qos info
211  * @last_update:        Stored time when last stats were updated
212  */
213 struct nfp_fl_qos {
214         u32 netdev_port_id;
215         struct nfp_stat_pair curr_stats;
216         struct nfp_stat_pair prev_stats;
217         u64 last_update;
218 };
219
220 /**
221  * struct nfp_flower_repr_priv - Flower APP per-repr priv data
222  * @nfp_repr:           Back pointer to nfp_repr
223  * @lag_port_flags:     Extended port flags to record lag state of repr
224  * @mac_offloaded:      Flag indicating a MAC address is offloaded for repr
225  * @offloaded_mac_addr: MAC address that has been offloaded for repr
226  * @block_shared:       Flag indicating if offload applies to shared blocks
227  * @mac_list:           List entry of reprs that share the same offloaded MAC
228  * @qos_table:          Stored info on filters implementing qos
229  * @on_bridge:          Indicates if the repr is attached to a bridge
230  */
231 struct nfp_flower_repr_priv {
232         struct nfp_repr *nfp_repr;
233         unsigned long lag_port_flags;
234         bool mac_offloaded;
235         u8 offloaded_mac_addr[ETH_ALEN];
236         bool block_shared;
237         struct list_head mac_list;
238         struct nfp_fl_qos qos_table;
239         bool on_bridge;
240 };
241
242 /**
243  * struct nfp_flower_non_repr_priv - Priv data for non-repr offloaded ports
244  * @list:               List entry of offloaded reprs
245  * @netdev:             Pointer to non-repr net_device
246  * @ref_count:          Number of references held for this priv data
247  * @mac_offloaded:      Flag indicating a MAC address is offloaded for device
248  * @offloaded_mac_addr: MAC address that has been offloaded for dev
249  */
250 struct nfp_flower_non_repr_priv {
251         struct list_head list;
252         struct net_device *netdev;
253         int ref_count;
254         bool mac_offloaded;
255         u8 offloaded_mac_addr[ETH_ALEN];
256 };
257
258 struct nfp_fl_key_ls {
259         u32 key_layer_two;
260         u8 key_layer;
261         int key_size;
262 };
263
264 struct nfp_fl_rule_metadata {
265         u8 key_len;
266         u8 mask_len;
267         u8 act_len;
268         u8 flags;
269         __be32 host_ctx_id;
270         __be64 host_cookie __packed;
271         __be64 flow_version __packed;
272         __be32 shortcut;
273 };
274
275 struct nfp_fl_stats {
276         u64 pkts;
277         u64 bytes;
278         u64 used;
279 };
280
281 /**
282  * struct nfp_ipv6_addr_entry - cached IPv6 addresses
283  * @ipv6_addr:  IP address
284  * @ref_count:  number of rules currently using this IP
285  * @list:       list pointer
286  */
287 struct nfp_ipv6_addr_entry {
288         struct in6_addr ipv6_addr;
289         int ref_count;
290         struct list_head list;
291 };
292
293 struct nfp_fl_payload {
294         struct nfp_fl_rule_metadata meta;
295         unsigned long tc_flower_cookie;
296         struct rhash_head fl_node;
297         struct rcu_head rcu;
298         __be32 nfp_tun_ipv4_addr;
299         struct nfp_ipv6_addr_entry *nfp_tun_ipv6;
300         struct net_device *ingress_dev;
301         char *unmasked_data;
302         char *mask_data;
303         char *action_data;
304         struct list_head linked_flows;
305         bool in_hw;
306         struct {
307                 struct net_device *dev;
308                 __be16 vlan_tci;
309                 __be16 port_idx;
310         } pre_tun_rule;
311 };
312
313 struct nfp_fl_payload_link {
314         /* A link contains a pointer to a merge flow and an associated sub_flow.
315          * Each merge flow will feature in 2 links to its underlying sub_flows.
316          * A sub_flow will have at least 1 link to a merge flow or more if it
317          * has been used to create multiple merge flows.
318          *
319          * For a merge flow, 'linked_flows' in its nfp_fl_payload struct lists
320          * all links to sub_flows (sub_flow.flow) via merge.list.
321          * For a sub_flow, 'linked_flows' gives all links to merge flows it has
322          * formed (merge_flow.flow) via sub_flow.list.
323          */
324         struct {
325                 struct list_head list;
326                 struct nfp_fl_payload *flow;
327         } merge_flow, sub_flow;
328 };
329
330 extern const struct rhashtable_params nfp_flower_table_params;
331
332 struct nfp_fl_stats_frame {
333         __be32 stats_con_id;
334         __be32 pkt_count;
335         __be64 byte_count;
336         __be64 stats_cookie;
337 };
338
339 static inline bool
340 nfp_flower_internal_port_can_offload(struct nfp_app *app,
341                                      struct net_device *netdev)
342 {
343         struct nfp_flower_priv *app_priv = app->priv;
344
345         if (!(app_priv->flower_ext_feats & NFP_FL_FEATS_FLOW_MERGE))
346                 return false;
347         if (!netdev->rtnl_link_ops)
348                 return false;
349         if (!strcmp(netdev->rtnl_link_ops->kind, "openvswitch"))
350                 return true;
351
352         return false;
353 }
354
355 /* The address of the merged flow acts as its cookie.
356  * Cookies supplied to us by TC flower are also addresses to allocated
357  * memory and thus this scheme should not generate any collisions.
358  */
359 static inline bool nfp_flower_is_merge_flow(struct nfp_fl_payload *flow_pay)
360 {
361         return flow_pay->tc_flower_cookie == (unsigned long)flow_pay;
362 }
363
364 static inline bool nfp_flower_is_supported_bridge(struct net_device *netdev)
365 {
366         return netif_is_ovs_master(netdev);
367 }
368
369 int nfp_flower_metadata_init(struct nfp_app *app, u64 host_ctx_count,
370                              unsigned int host_ctx_split);
371 void nfp_flower_metadata_cleanup(struct nfp_app *app);
372
373 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
374                         enum tc_setup_type type, void *type_data);
375 int nfp_flower_merge_offloaded_flows(struct nfp_app *app,
376                                      struct nfp_fl_payload *sub_flow1,
377                                      struct nfp_fl_payload *sub_flow2);
378 int nfp_flower_compile_flow_match(struct nfp_app *app,
379                                   struct flow_cls_offload *flow,
380                                   struct nfp_fl_key_ls *key_ls,
381                                   struct net_device *netdev,
382                                   struct nfp_fl_payload *nfp_flow,
383                                   enum nfp_flower_tun_type tun_type,
384                                   struct netlink_ext_ack *extack);
385 int nfp_flower_compile_action(struct nfp_app *app,
386                               struct flow_cls_offload *flow,
387                               struct net_device *netdev,
388                               struct nfp_fl_payload *nfp_flow,
389                               struct netlink_ext_ack *extack);
390 int nfp_compile_flow_metadata(struct nfp_app *app,
391                               struct flow_cls_offload *flow,
392                               struct nfp_fl_payload *nfp_flow,
393                               struct net_device *netdev,
394                               struct netlink_ext_ack *extack);
395 void __nfp_modify_flow_metadata(struct nfp_flower_priv *priv,
396                                 struct nfp_fl_payload *nfp_flow);
397 int nfp_modify_flow_metadata(struct nfp_app *app,
398                              struct nfp_fl_payload *nfp_flow);
399
400 struct nfp_fl_payload *
401 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie,
402                            struct net_device *netdev);
403 struct nfp_fl_payload *
404 nfp_flower_get_fl_payload_from_ctx(struct nfp_app *app, u32 ctx_id);
405 struct nfp_fl_payload *
406 nfp_flower_remove_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie);
407
408 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb);
409
410 int nfp_tunnel_config_start(struct nfp_app *app);
411 void nfp_tunnel_config_stop(struct nfp_app *app);
412 int nfp_tunnel_mac_event_handler(struct nfp_app *app,
413                                  struct net_device *netdev,
414                                  unsigned long event, void *ptr);
415 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4);
416 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4);
417 void
418 nfp_tunnel_put_ipv6_off(struct nfp_app *app, struct nfp_ipv6_addr_entry *entry);
419 struct nfp_ipv6_addr_entry *
420 nfp_tunnel_add_ipv6_off(struct nfp_app *app, struct in6_addr *ipv6);
421 void nfp_tunnel_request_route_v4(struct nfp_app *app, struct sk_buff *skb);
422 void nfp_tunnel_request_route_v6(struct nfp_app *app, struct sk_buff *skb);
423 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb);
424 void nfp_flower_lag_init(struct nfp_fl_lag *lag);
425 void nfp_flower_lag_cleanup(struct nfp_fl_lag *lag);
426 int nfp_flower_lag_reset(struct nfp_fl_lag *lag);
427 int nfp_flower_lag_netdev_event(struct nfp_flower_priv *priv,
428                                 struct net_device *netdev,
429                                 unsigned long event, void *ptr);
430 bool nfp_flower_lag_unprocessed_msg(struct nfp_app *app, struct sk_buff *skb);
431 int nfp_flower_lag_populate_pre_action(struct nfp_app *app,
432                                        struct net_device *master,
433                                        struct nfp_fl_pre_lag *pre_act,
434                                        struct netlink_ext_ack *extack);
435 int nfp_flower_lag_get_output_id(struct nfp_app *app,
436                                  struct net_device *master);
437 void nfp_flower_qos_init(struct nfp_app *app);
438 void nfp_flower_qos_cleanup(struct nfp_app *app);
439 int nfp_flower_setup_qos_offload(struct nfp_app *app, struct net_device *netdev,
440                                  struct tc_cls_matchall_offload *flow);
441 void nfp_flower_stats_rlim_reply(struct nfp_app *app, struct sk_buff *skb);
442 int nfp_flower_reg_indir_block_handler(struct nfp_app *app,
443                                        struct net_device *netdev,
444                                        unsigned long event);
445
446 void
447 __nfp_flower_non_repr_priv_get(struct nfp_flower_non_repr_priv *non_repr_priv);
448 struct nfp_flower_non_repr_priv *
449 nfp_flower_non_repr_priv_get(struct nfp_app *app, struct net_device *netdev);
450 void
451 __nfp_flower_non_repr_priv_put(struct nfp_flower_non_repr_priv *non_repr_priv);
452 void
453 nfp_flower_non_repr_priv_put(struct nfp_app *app, struct net_device *netdev);
454 u32 nfp_flower_get_port_id_from_netdev(struct nfp_app *app,
455                                        struct net_device *netdev);
456 int nfp_flower_xmit_pre_tun_flow(struct nfp_app *app,
457                                  struct nfp_fl_payload *flow);
458 int nfp_flower_xmit_pre_tun_del_flow(struct nfp_app *app,
459                                      struct nfp_fl_payload *flow);
460 #endif