1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
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.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
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.
23  *
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
31  * SOFTWARE.
32  */
33 
34 #ifndef __NFP_FLOWER_H__
35 #define __NFP_FLOWER_H__ 1
36 
37 #include "cmsg.h"
38 
39 #include <linux/circ_buf.h>
40 #include <linux/hashtable.h>
41 #include <linux/time64.h>
42 #include <linux/types.h>
43 #include <net/pkt_cls.h>
44 #include <net/tcp.h>
45 #include <linux/workqueue.h>
46 #include <linux/idr.h>
47 
48 struct nfp_fl_pre_lag;
49 struct net_device;
50 struct nfp_app;
51 
52 #define NFP_FL_STATS_CTX_DONT_CARE	cpu_to_be32(0xffffffff)
53 #define NFP_FL_STATS_ENTRY_RS		BIT(20)
54 #define NFP_FL_STATS_ELEM_RS		4
55 #define NFP_FL_REPEATED_HASH_MAX	BIT(17)
56 #define NFP_FLOWER_HASH_BITS		19
57 #define NFP_FLOWER_MASK_ENTRY_RS	256
58 #define NFP_FLOWER_MASK_ELEMENT_RS	1
59 #define NFP_FLOWER_MASK_HASH_BITS	10
60 
61 #define NFP_FL_META_FLAG_MANAGE_MASK	BIT(7)
62 
63 #define NFP_FL_MASK_REUSE_TIME_NS	40000
64 #define NFP_FL_MASK_ID_LOCATION		1
65 
66 #define NFP_FL_VXLAN_PORT		4789
67 #define NFP_FL_GENEVE_PORT		6081
68 
69 /* Extra features bitmap. */
70 #define NFP_FL_FEATS_GENEVE		BIT(0)
71 #define NFP_FL_NBI_MTU_SETTING		BIT(1)
72 #define NFP_FL_FEATS_LAG		BIT(31)
73 
74 struct nfp_fl_mask_id {
75 	struct circ_buf mask_id_free_list;
76 	struct timespec64 *last_used;
77 	u8 init_unallocated;
78 };
79 
80 struct nfp_fl_stats_id {
81 	struct circ_buf free_list;
82 	u32 init_unalloc;
83 	u8 repeated_em_count;
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  * @lag_nb:		Notifier to track master/slave events
105  * @work:		Work queue for writing configs to the HW
106  * @lock:		Lock to protect lag_group_list
107  * @group_list:		List of all master/slave groups offloaded
108  * @ida_handle:		IDA to handle group ids
109  * @pkt_num:		Incremented for each config packet sent
110  * @batch_ver:		Incremented for each batch of config packets
111  * @global_inst:	Instance allocator for groups
112  * @rst_cfg:		Marker to reset HW LAG config
113  * @retrans_skbs:	Cmsgs that could not be processed by HW and require
114  *			retransmission
115  */
116 struct nfp_fl_lag {
117 	struct notifier_block lag_nb;
118 	struct delayed_work work;
119 	struct mutex lock;
120 	struct list_head group_list;
121 	struct ida ida_handle;
122 	unsigned int pkt_num;
123 	unsigned int batch_ver;
124 	u8 global_inst;
125 	bool rst_cfg;
126 	struct sk_buff_head retrans_skbs;
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  * @flow_table:		Hash table used to store flower rules
140  * @cmsg_work:		Workqueue for control messages processing
141  * @cmsg_skbs_high:	List of higher priority skbs for control message
142  *			processing
143  * @cmsg_skbs_low:	List of lower priority skbs for control message
144  *			processing
145  * @nfp_mac_off_list:	List of MAC addresses to offload
146  * @nfp_mac_index_list:	List of unique 8-bit indexes for non NFP netdevs
147  * @nfp_ipv4_off_list:	List of IPv4 addresses to offload
148  * @nfp_neigh_off_list:	List of neighbour offloads
149  * @nfp_mac_off_lock:	Lock for the MAC address list
150  * @nfp_mac_index_lock:	Lock for the MAC index list
151  * @nfp_ipv4_off_lock:	Lock for the IPv4 address list
152  * @nfp_neigh_off_lock:	Lock for the neighbour address list
153  * @nfp_mac_off_ids:	IDA to manage id assignment for offloaded macs
154  * @nfp_mac_off_count:	Number of MACs in address list
155  * @nfp_tun_mac_nb:	Notifier to monitor link state
156  * @nfp_tun_neigh_nb:	Notifier to monitor neighbour state
157  * @reify_replies:	atomically stores the number of replies received
158  *			from firmware for repr reify
159  * @reify_wait_queue:	wait queue for repr reify response counting
160  * @mtu_conf:		Configuration of repr MTU value
161  * @nfp_lag:		Link aggregation data block
162  */
163 struct nfp_flower_priv {
164 	struct nfp_app *app;
165 	struct nfp_net *nn;
166 	u32 mask_id_seed;
167 	u64 flower_version;
168 	u64 flower_ext_feats;
169 	struct nfp_fl_stats_id stats_ids;
170 	struct nfp_fl_mask_id mask_ids;
171 	DECLARE_HASHTABLE(mask_table, NFP_FLOWER_MASK_HASH_BITS);
172 	DECLARE_HASHTABLE(flow_table, NFP_FLOWER_HASH_BITS);
173 	struct work_struct cmsg_work;
174 	struct sk_buff_head cmsg_skbs_high;
175 	struct sk_buff_head cmsg_skbs_low;
176 	struct list_head nfp_mac_off_list;
177 	struct list_head nfp_mac_index_list;
178 	struct list_head nfp_ipv4_off_list;
179 	struct list_head nfp_neigh_off_list;
180 	struct mutex nfp_mac_off_lock;
181 	struct mutex nfp_mac_index_lock;
182 	struct mutex nfp_ipv4_off_lock;
183 	spinlock_t nfp_neigh_off_lock;
184 	struct ida nfp_mac_off_ids;
185 	int nfp_mac_off_count;
186 	struct notifier_block nfp_tun_mac_nb;
187 	struct notifier_block nfp_tun_neigh_nb;
188 	atomic_t reify_replies;
189 	wait_queue_head_t reify_wait_queue;
190 	struct nfp_mtu_conf mtu_conf;
191 	struct nfp_fl_lag nfp_lag;
192 };
193 
194 /**
195  * struct nfp_flower_repr_priv - Flower APP per-repr priv data
196  * @lag_port_flags:	Extended port flags to record lag state of repr
197  */
198 struct nfp_flower_repr_priv {
199 	unsigned long lag_port_flags;
200 };
201 
202 struct nfp_fl_key_ls {
203 	u32 key_layer_two;
204 	u8 key_layer;
205 	int key_size;
206 };
207 
208 struct nfp_fl_rule_metadata {
209 	u8 key_len;
210 	u8 mask_len;
211 	u8 act_len;
212 	u8 flags;
213 	__be32 host_ctx_id;
214 	__be64 host_cookie __packed;
215 	__be64 flow_version __packed;
216 	__be32 shortcut;
217 };
218 
219 struct nfp_fl_stats {
220 	u64 pkts;
221 	u64 bytes;
222 	u64 used;
223 };
224 
225 struct nfp_fl_payload {
226 	struct nfp_fl_rule_metadata meta;
227 	unsigned long tc_flower_cookie;
228 	struct hlist_node link;
229 	struct rcu_head rcu;
230 	spinlock_t lock; /* lock stats */
231 	struct nfp_fl_stats stats;
232 	__be32 nfp_tun_ipv4_addr;
233 	struct net_device *ingress_dev;
234 	char *unmasked_data;
235 	char *mask_data;
236 	char *action_data;
237 	bool ingress_offload;
238 };
239 
240 struct nfp_fl_stats_frame {
241 	__be32 stats_con_id;
242 	__be32 pkt_count;
243 	__be64 byte_count;
244 	__be64 stats_cookie;
245 };
246 
247 int nfp_flower_metadata_init(struct nfp_app *app);
248 void nfp_flower_metadata_cleanup(struct nfp_app *app);
249 
250 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
251 			enum tc_setup_type type, void *type_data);
252 int nfp_flower_compile_flow_match(struct tc_cls_flower_offload *flow,
253 				  struct nfp_fl_key_ls *key_ls,
254 				  struct net_device *netdev,
255 				  struct nfp_fl_payload *nfp_flow,
256 				  enum nfp_flower_tun_type tun_type);
257 int nfp_flower_compile_action(struct nfp_app *app,
258 			      struct tc_cls_flower_offload *flow,
259 			      struct net_device *netdev,
260 			      struct nfp_fl_payload *nfp_flow);
261 int nfp_compile_flow_metadata(struct nfp_app *app,
262 			      struct tc_cls_flower_offload *flow,
263 			      struct nfp_fl_payload *nfp_flow,
264 			      struct net_device *netdev);
265 int nfp_modify_flow_metadata(struct nfp_app *app,
266 			     struct nfp_fl_payload *nfp_flow);
267 
268 struct nfp_fl_payload *
269 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie,
270 			   struct net_device *netdev, __be32 host_ctx);
271 struct nfp_fl_payload *
272 nfp_flower_remove_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie);
273 
274 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb);
275 
276 int nfp_tunnel_config_start(struct nfp_app *app);
277 void nfp_tunnel_config_stop(struct nfp_app *app);
278 void nfp_tunnel_write_macs(struct nfp_app *app);
279 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4);
280 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4);
281 void nfp_tunnel_request_route(struct nfp_app *app, struct sk_buff *skb);
282 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb);
283 int nfp_flower_setup_tc_egress_cb(enum tc_setup_type type, void *type_data,
284 				  void *cb_priv);
285 void nfp_flower_lag_init(struct nfp_fl_lag *lag);
286 void nfp_flower_lag_cleanup(struct nfp_fl_lag *lag);
287 int nfp_flower_lag_reset(struct nfp_fl_lag *lag);
288 bool nfp_flower_lag_unprocessed_msg(struct nfp_app *app, struct sk_buff *skb);
289 int nfp_flower_lag_populate_pre_action(struct nfp_app *app,
290 				       struct net_device *master,
291 				       struct nfp_fl_pre_lag *pre_act);
292 int nfp_flower_lag_get_output_id(struct nfp_app *app,
293 				 struct net_device *master);
294 
295 #endif
296