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