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