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_GENEVE_OPT		BIT(2)
73 #define NFP_FL_FEATS_VLAN_PCP		BIT(3)
74 #define NFP_FL_FEATS_LAG		BIT(31)
75 
76 struct nfp_fl_mask_id {
77 	struct circ_buf mask_id_free_list;
78 	ktime_t *last_used;
79 	u8 init_unallocated;
80 };
81 
82 struct nfp_fl_stats_id {
83 	struct circ_buf free_list;
84 	u32 init_unalloc;
85 	u8 repeated_em_count;
86 };
87 
88 /**
89  * struct nfp_mtu_conf - manage MTU setting
90  * @portnum:		NFP port number of repr with requested MTU change
91  * @requested_val:	MTU value requested for repr
92  * @ack:		Received ack that MTU has been correctly set
93  * @wait_q:		Wait queue for MTU acknowledgements
94  * @lock:		Lock for setting/reading MTU variables
95  */
96 struct nfp_mtu_conf {
97 	u32 portnum;
98 	unsigned int requested_val;
99 	bool ack;
100 	wait_queue_head_t wait_q;
101 	spinlock_t lock;
102 };
103 
104 /**
105  * struct nfp_fl_lag - Flower APP priv data for link aggregation
106  * @lag_nb:		Notifier to track master/slave events
107  * @work:		Work queue for writing configs to the HW
108  * @lock:		Lock to protect lag_group_list
109  * @group_list:		List of all master/slave groups offloaded
110  * @ida_handle:		IDA to handle group ids
111  * @pkt_num:		Incremented for each config packet sent
112  * @batch_ver:		Incremented for each batch of config packets
113  * @global_inst:	Instance allocator for groups
114  * @rst_cfg:		Marker to reset HW LAG config
115  * @retrans_skbs:	Cmsgs that could not be processed by HW and require
116  *			retransmission
117  */
118 struct nfp_fl_lag {
119 	struct notifier_block lag_nb;
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_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  * @flow_table:		Hash table used to store flower rules
142  * @cmsg_work:		Workqueue for control messages processing
143  * @cmsg_skbs_high:	List of higher priority skbs for control message
144  *			processing
145  * @cmsg_skbs_low:	List of lower priority skbs for control message
146  *			processing
147  * @nfp_mac_off_list:	List of MAC addresses to offload
148  * @nfp_mac_index_list:	List of unique 8-bit indexes for non NFP netdevs
149  * @nfp_ipv4_off_list:	List of IPv4 addresses to offload
150  * @nfp_neigh_off_list:	List of neighbour offloads
151  * @nfp_mac_off_lock:	Lock for the MAC address list
152  * @nfp_mac_index_lock:	Lock for the MAC index list
153  * @nfp_ipv4_off_lock:	Lock for the IPv4 address list
154  * @nfp_neigh_off_lock:	Lock for the neighbour address list
155  * @nfp_mac_off_ids:	IDA to manage id assignment for offloaded macs
156  * @nfp_mac_off_count:	Number of MACs in address list
157  * @nfp_tun_mac_nb:	Notifier to monitor link state
158  * @nfp_tun_neigh_nb:	Notifier to monitor neighbour state
159  * @reify_replies:	atomically stores the number of replies received
160  *			from firmware for repr reify
161  * @reify_wait_queue:	wait queue for repr reify response counting
162  * @mtu_conf:		Configuration of repr MTU value
163  * @nfp_lag:		Link aggregation data block
164  */
165 struct nfp_flower_priv {
166 	struct nfp_app *app;
167 	struct nfp_net *nn;
168 	u32 mask_id_seed;
169 	u64 flower_version;
170 	u64 flower_ext_feats;
171 	struct nfp_fl_stats_id stats_ids;
172 	struct nfp_fl_mask_id mask_ids;
173 	DECLARE_HASHTABLE(mask_table, NFP_FLOWER_MASK_HASH_BITS);
174 	DECLARE_HASHTABLE(flow_table, NFP_FLOWER_HASH_BITS);
175 	struct work_struct cmsg_work;
176 	struct sk_buff_head cmsg_skbs_high;
177 	struct sk_buff_head cmsg_skbs_low;
178 	struct list_head nfp_mac_off_list;
179 	struct list_head nfp_mac_index_list;
180 	struct list_head nfp_ipv4_off_list;
181 	struct list_head nfp_neigh_off_list;
182 	struct mutex nfp_mac_off_lock;
183 	struct mutex nfp_mac_index_lock;
184 	struct mutex nfp_ipv4_off_lock;
185 	spinlock_t nfp_neigh_off_lock;
186 	struct ida nfp_mac_off_ids;
187 	int nfp_mac_off_count;
188 	struct notifier_block nfp_tun_mac_nb;
189 	struct notifier_block nfp_tun_neigh_nb;
190 	atomic_t reify_replies;
191 	wait_queue_head_t reify_wait_queue;
192 	struct nfp_mtu_conf mtu_conf;
193 	struct nfp_fl_lag nfp_lag;
194 };
195 
196 /**
197  * struct nfp_flower_repr_priv - Flower APP per-repr priv data
198  * @lag_port_flags:	Extended port flags to record lag state of repr
199  */
200 struct nfp_flower_repr_priv {
201 	unsigned long lag_port_flags;
202 };
203 
204 struct nfp_fl_key_ls {
205 	u32 key_layer_two;
206 	u8 key_layer;
207 	int key_size;
208 };
209 
210 struct nfp_fl_rule_metadata {
211 	u8 key_len;
212 	u8 mask_len;
213 	u8 act_len;
214 	u8 flags;
215 	__be32 host_ctx_id;
216 	__be64 host_cookie __packed;
217 	__be64 flow_version __packed;
218 	__be32 shortcut;
219 };
220 
221 struct nfp_fl_stats {
222 	u64 pkts;
223 	u64 bytes;
224 	u64 used;
225 };
226 
227 struct nfp_fl_payload {
228 	struct nfp_fl_rule_metadata meta;
229 	unsigned long tc_flower_cookie;
230 	struct hlist_node link;
231 	struct rcu_head rcu;
232 	spinlock_t lock; /* lock stats */
233 	struct nfp_fl_stats stats;
234 	__be32 nfp_tun_ipv4_addr;
235 	struct net_device *ingress_dev;
236 	char *unmasked_data;
237 	char *mask_data;
238 	char *action_data;
239 	bool ingress_offload;
240 };
241 
242 struct nfp_fl_stats_frame {
243 	__be32 stats_con_id;
244 	__be32 pkt_count;
245 	__be64 byte_count;
246 	__be64 stats_cookie;
247 };
248 
249 int nfp_flower_metadata_init(struct nfp_app *app);
250 void nfp_flower_metadata_cleanup(struct nfp_app *app);
251 
252 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
253 			enum tc_setup_type type, void *type_data);
254 int nfp_flower_compile_flow_match(struct tc_cls_flower_offload *flow,
255 				  struct nfp_fl_key_ls *key_ls,
256 				  struct net_device *netdev,
257 				  struct nfp_fl_payload *nfp_flow,
258 				  enum nfp_flower_tun_type tun_type);
259 int nfp_flower_compile_action(struct nfp_app *app,
260 			      struct tc_cls_flower_offload *flow,
261 			      struct net_device *netdev,
262 			      struct nfp_fl_payload *nfp_flow);
263 int nfp_compile_flow_metadata(struct nfp_app *app,
264 			      struct tc_cls_flower_offload *flow,
265 			      struct nfp_fl_payload *nfp_flow,
266 			      struct net_device *netdev);
267 int nfp_modify_flow_metadata(struct nfp_app *app,
268 			     struct nfp_fl_payload *nfp_flow);
269 
270 struct nfp_fl_payload *
271 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie,
272 			   struct net_device *netdev, __be32 host_ctx);
273 struct nfp_fl_payload *
274 nfp_flower_remove_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie);
275 
276 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb);
277 
278 int nfp_tunnel_config_start(struct nfp_app *app);
279 void nfp_tunnel_config_stop(struct nfp_app *app);
280 void nfp_tunnel_write_macs(struct nfp_app *app);
281 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4);
282 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4);
283 void nfp_tunnel_request_route(struct nfp_app *app, struct sk_buff *skb);
284 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb);
285 int nfp_flower_setup_tc_egress_cb(enum tc_setup_type type, void *type_data,
286 				  void *cb_priv);
287 void nfp_flower_lag_init(struct nfp_fl_lag *lag);
288 void nfp_flower_lag_cleanup(struct nfp_fl_lag *lag);
289 int nfp_flower_lag_reset(struct nfp_fl_lag *lag);
290 bool nfp_flower_lag_unprocessed_msg(struct nfp_app *app, struct sk_buff *skb);
291 int nfp_flower_lag_populate_pre_action(struct nfp_app *app,
292 				       struct net_device *master,
293 				       struct nfp_fl_pre_lag *pre_act);
294 int nfp_flower_lag_get_output_id(struct nfp_app *app,
295 				 struct net_device *master);
296 
297 #endif
298