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 
47 struct net_device;
48 struct nfp_app;
49 
50 #define NFP_FL_STATS_CTX_DONT_CARE	cpu_to_be32(0xffffffff)
51 #define NFP_FL_STATS_ENTRY_RS		BIT(20)
52 #define NFP_FL_STATS_ELEM_RS		4
53 #define NFP_FL_REPEATED_HASH_MAX	BIT(17)
54 #define NFP_FLOWER_HASH_BITS		19
55 #define NFP_FLOWER_MASK_ENTRY_RS	256
56 #define NFP_FLOWER_MASK_ELEMENT_RS	1
57 #define NFP_FLOWER_MASK_HASH_BITS	10
58 
59 #define NFP_FL_META_FLAG_MANAGE_MASK	BIT(7)
60 
61 #define NFP_FL_MASK_REUSE_TIME_NS	40000
62 #define NFP_FL_MASK_ID_LOCATION		1
63 
64 #define NFP_FL_VXLAN_PORT		4789
65 #define NFP_FL_GENEVE_PORT		6081
66 
67 /* Extra features bitmap. */
68 #define NFP_FL_FEATS_GENEVE		BIT(0)
69 #define NFP_FL_NBI_MTU_SETTING		BIT(1)
70 
71 struct nfp_fl_mask_id {
72 	struct circ_buf mask_id_free_list;
73 	struct timespec64 *last_used;
74 	u8 init_unallocated;
75 };
76 
77 struct nfp_fl_stats_id {
78 	struct circ_buf free_list;
79 	u32 init_unalloc;
80 	u8 repeated_em_count;
81 };
82 
83 /**
84  * struct nfp_mtu_conf - manage MTU setting
85  * @portnum:		NFP port number of repr with requested MTU change
86  * @requested_val:	MTU value requested for repr
87  * @ack:		Received ack that MTU has been correctly set
88  * @wait_q:		Wait queue for MTU acknowledgements
89  * @lock:		Lock for setting/reading MTU variables
90  */
91 struct nfp_mtu_conf {
92 	u32 portnum;
93 	unsigned int requested_val;
94 	bool ack;
95 	wait_queue_head_t wait_q;
96 	spinlock_t lock;
97 };
98 
99 /**
100  * struct nfp_flower_priv - Flower APP per-vNIC priv data
101  * @app:		Back pointer to app
102  * @nn:			Pointer to vNIC
103  * @mask_id_seed:	Seed used for mask hash table
104  * @flower_version:	HW version of flower
105  * @flower_ext_feats:	Bitmap of extra features the HW supports
106  * @stats_ids:		List of free stats ids
107  * @mask_ids:		List of free mask ids
108  * @mask_table:		Hash table used to store masks
109  * @flow_table:		Hash table used to store flower rules
110  * @cmsg_work:		Workqueue for control messages processing
111  * @cmsg_skbs_high:	List of higher priority skbs for control message
112  *			processing
113  * @cmsg_skbs_low:	List of lower priority skbs for control message
114  *			processing
115  * @nfp_mac_off_list:	List of MAC addresses to offload
116  * @nfp_mac_index_list:	List of unique 8-bit indexes for non NFP netdevs
117  * @nfp_ipv4_off_list:	List of IPv4 addresses to offload
118  * @nfp_neigh_off_list:	List of neighbour offloads
119  * @nfp_mac_off_lock:	Lock for the MAC address list
120  * @nfp_mac_index_lock:	Lock for the MAC index list
121  * @nfp_ipv4_off_lock:	Lock for the IPv4 address list
122  * @nfp_neigh_off_lock:	Lock for the neighbour address list
123  * @nfp_mac_off_ids:	IDA to manage id assignment for offloaded macs
124  * @nfp_mac_off_count:	Number of MACs in address list
125  * @nfp_tun_mac_nb:	Notifier to monitor link state
126  * @nfp_tun_neigh_nb:	Notifier to monitor neighbour state
127  * @reify_replies:	atomically stores the number of replies received
128  *			from firmware for repr reify
129  * @reify_wait_queue:	wait queue for repr reify response counting
130  * @mtu_conf:		Configuration of repr MTU value
131  */
132 struct nfp_flower_priv {
133 	struct nfp_app *app;
134 	struct nfp_net *nn;
135 	u32 mask_id_seed;
136 	u64 flower_version;
137 	u64 flower_ext_feats;
138 	struct nfp_fl_stats_id stats_ids;
139 	struct nfp_fl_mask_id mask_ids;
140 	DECLARE_HASHTABLE(mask_table, NFP_FLOWER_MASK_HASH_BITS);
141 	DECLARE_HASHTABLE(flow_table, NFP_FLOWER_HASH_BITS);
142 	struct work_struct cmsg_work;
143 	struct sk_buff_head cmsg_skbs_high;
144 	struct sk_buff_head cmsg_skbs_low;
145 	struct list_head nfp_mac_off_list;
146 	struct list_head nfp_mac_index_list;
147 	struct list_head nfp_ipv4_off_list;
148 	struct list_head nfp_neigh_off_list;
149 	struct mutex nfp_mac_off_lock;
150 	struct mutex nfp_mac_index_lock;
151 	struct mutex nfp_ipv4_off_lock;
152 	spinlock_t nfp_neigh_off_lock;
153 	struct ida nfp_mac_off_ids;
154 	int nfp_mac_off_count;
155 	struct notifier_block nfp_tun_mac_nb;
156 	struct notifier_block nfp_tun_neigh_nb;
157 	atomic_t reify_replies;
158 	wait_queue_head_t reify_wait_queue;
159 	struct nfp_mtu_conf mtu_conf;
160 };
161 
162 struct nfp_fl_key_ls {
163 	u32 key_layer_two;
164 	u8 key_layer;
165 	int key_size;
166 };
167 
168 struct nfp_fl_rule_metadata {
169 	u8 key_len;
170 	u8 mask_len;
171 	u8 act_len;
172 	u8 flags;
173 	__be32 host_ctx_id;
174 	__be64 host_cookie __packed;
175 	__be64 flow_version __packed;
176 	__be32 shortcut;
177 };
178 
179 struct nfp_fl_stats {
180 	u64 pkts;
181 	u64 bytes;
182 	u64 used;
183 };
184 
185 struct nfp_fl_payload {
186 	struct nfp_fl_rule_metadata meta;
187 	unsigned long tc_flower_cookie;
188 	struct hlist_node link;
189 	struct rcu_head rcu;
190 	spinlock_t lock; /* lock stats */
191 	struct nfp_fl_stats stats;
192 	__be32 nfp_tun_ipv4_addr;
193 	struct net_device *ingress_dev;
194 	char *unmasked_data;
195 	char *mask_data;
196 	char *action_data;
197 	bool ingress_offload;
198 };
199 
200 struct nfp_fl_stats_frame {
201 	__be32 stats_con_id;
202 	__be32 pkt_count;
203 	__be64 byte_count;
204 	__be64 stats_cookie;
205 };
206 
207 int nfp_flower_metadata_init(struct nfp_app *app);
208 void nfp_flower_metadata_cleanup(struct nfp_app *app);
209 
210 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
211 			enum tc_setup_type type, void *type_data);
212 int nfp_flower_compile_flow_match(struct tc_cls_flower_offload *flow,
213 				  struct nfp_fl_key_ls *key_ls,
214 				  struct net_device *netdev,
215 				  struct nfp_fl_payload *nfp_flow,
216 				  enum nfp_flower_tun_type tun_type);
217 int nfp_flower_compile_action(struct tc_cls_flower_offload *flow,
218 			      struct net_device *netdev,
219 			      struct nfp_fl_payload *nfp_flow);
220 int nfp_compile_flow_metadata(struct nfp_app *app,
221 			      struct tc_cls_flower_offload *flow,
222 			      struct nfp_fl_payload *nfp_flow,
223 			      struct net_device *netdev);
224 int nfp_modify_flow_metadata(struct nfp_app *app,
225 			     struct nfp_fl_payload *nfp_flow);
226 
227 struct nfp_fl_payload *
228 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie,
229 			   struct net_device *netdev, __be32 host_ctx);
230 struct nfp_fl_payload *
231 nfp_flower_remove_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie);
232 
233 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb);
234 
235 int nfp_tunnel_config_start(struct nfp_app *app);
236 void nfp_tunnel_config_stop(struct nfp_app *app);
237 void nfp_tunnel_write_macs(struct nfp_app *app);
238 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4);
239 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4);
240 void nfp_tunnel_request_route(struct nfp_app *app, struct sk_buff *skb);
241 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb);
242 int nfp_flower_setup_tc_egress_cb(enum tc_setup_type type, void *type_data,
243 				  void *cb_priv);
244 
245 #endif
246