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