xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/flower/main.h (revision 28efb0046512e8a13ed9f9bdf0d68d10bbfbe9cf)
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 <linux/circ_buf.h>
38 #include <linux/hashtable.h>
39 #include <linux/time64.h>
40 #include <linux/types.h>
41 #include <net/pkt_cls.h>
42 #include <linux/workqueue.h>
43 
44 struct net_device;
45 struct nfp_app;
46 
47 #define NFP_FL_STATS_ENTRY_RS		BIT(20)
48 #define NFP_FL_STATS_ELEM_RS		4
49 #define NFP_FL_REPEATED_HASH_MAX	BIT(17)
50 #define NFP_FLOWER_HASH_BITS		19
51 #define NFP_FLOWER_MASK_ENTRY_RS	256
52 #define NFP_FLOWER_MASK_ELEMENT_RS	1
53 #define NFP_FLOWER_MASK_HASH_BITS	10
54 
55 #define NFP_FL_META_FLAG_NEW_MASK	128
56 #define NFP_FL_META_FLAG_LAST_MASK	1
57 
58 #define NFP_FL_MASK_REUSE_TIME_NS	40000
59 #define NFP_FL_MASK_ID_LOCATION		1
60 
61 #define NFP_FL_VXLAN_PORT		4789
62 
63 struct nfp_fl_mask_id {
64 	struct circ_buf mask_id_free_list;
65 	struct timespec64 *last_used;
66 	u8 init_unallocated;
67 };
68 
69 struct nfp_fl_stats_id {
70 	struct circ_buf free_list;
71 	u32 init_unalloc;
72 	u8 repeated_em_count;
73 };
74 
75 /**
76  * struct nfp_flower_priv - Flower APP per-vNIC priv data
77  * @app:		Back pointer to app
78  * @nn:			Pointer to vNIC
79  * @mask_id_seed:	Seed used for mask hash table
80  * @flower_version:	HW version of flower
81  * @stats_ids:		List of free stats ids
82  * @mask_ids:		List of free mask ids
83  * @mask_table:		Hash table used to store masks
84  * @flow_table:		Hash table used to store flower rules
85  * @cmsg_work:		Workqueue for control messages processing
86  * @cmsg_skbs:		List of skbs for control message processing
87  * @nfp_mac_off_list:	List of MAC addresses to offload
88  * @nfp_mac_index_list:	List of unique 8-bit indexes for non NFP netdevs
89  * @nfp_ipv4_off_list:	List of IPv4 addresses to offload
90  * @nfp_neigh_off_list:	List of neighbour offloads
91  * @nfp_mac_off_lock:	Lock for the MAC address list
92  * @nfp_mac_index_lock:	Lock for the MAC index list
93  * @nfp_ipv4_off_lock:	Lock for the IPv4 address list
94  * @nfp_neigh_off_lock:	Lock for the neighbour address list
95  * @nfp_mac_off_ids:	IDA to manage id assignment for offloaded macs
96  * @nfp_mac_off_count:	Number of MACs in address list
97  * @nfp_tun_mac_nb:	Notifier to monitor link state
98  * @nfp_tun_neigh_nb:	Notifier to monitor neighbour state
99  */
100 struct nfp_flower_priv {
101 	struct nfp_app *app;
102 	struct nfp_net *nn;
103 	u32 mask_id_seed;
104 	u64 flower_version;
105 	struct nfp_fl_stats_id stats_ids;
106 	struct nfp_fl_mask_id mask_ids;
107 	DECLARE_HASHTABLE(mask_table, NFP_FLOWER_MASK_HASH_BITS);
108 	DECLARE_HASHTABLE(flow_table, NFP_FLOWER_HASH_BITS);
109 	struct work_struct cmsg_work;
110 	struct sk_buff_head cmsg_skbs;
111 	struct list_head nfp_mac_off_list;
112 	struct list_head nfp_mac_index_list;
113 	struct list_head nfp_ipv4_off_list;
114 	struct list_head nfp_neigh_off_list;
115 	struct mutex nfp_mac_off_lock;
116 	struct mutex nfp_mac_index_lock;
117 	struct mutex nfp_ipv4_off_lock;
118 	struct mutex nfp_neigh_off_lock;
119 	struct ida nfp_mac_off_ids;
120 	int nfp_mac_off_count;
121 	struct notifier_block nfp_tun_mac_nb;
122 	struct notifier_block nfp_tun_neigh_nb;
123 };
124 
125 struct nfp_fl_key_ls {
126 	u32 key_layer_two;
127 	u8 key_layer;
128 	int key_size;
129 };
130 
131 struct nfp_fl_rule_metadata {
132 	u8 key_len;
133 	u8 mask_len;
134 	u8 act_len;
135 	u8 flags;
136 	__be32 host_ctx_id;
137 	__be64 host_cookie __packed;
138 	__be64 flow_version __packed;
139 	__be32 shortcut;
140 };
141 
142 struct nfp_fl_stats {
143 	u64 pkts;
144 	u64 bytes;
145 	u64 used;
146 };
147 
148 struct nfp_fl_payload {
149 	struct nfp_fl_rule_metadata meta;
150 	unsigned long tc_flower_cookie;
151 	struct hlist_node link;
152 	struct rcu_head rcu;
153 	spinlock_t lock; /* lock stats */
154 	struct nfp_fl_stats stats;
155 	__be32 nfp_tun_ipv4_addr;
156 	char *unmasked_data;
157 	char *mask_data;
158 	char *action_data;
159 };
160 
161 struct nfp_fl_stats_frame {
162 	__be32 stats_con_id;
163 	__be32 pkt_count;
164 	__be64 byte_count;
165 	__be64 stats_cookie;
166 };
167 
168 int nfp_flower_metadata_init(struct nfp_app *app);
169 void nfp_flower_metadata_cleanup(struct nfp_app *app);
170 
171 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
172 			enum tc_setup_type type, void *type_data);
173 int nfp_flower_compile_flow_match(struct tc_cls_flower_offload *flow,
174 				  struct nfp_fl_key_ls *key_ls,
175 				  struct net_device *netdev,
176 				  struct nfp_fl_payload *nfp_flow);
177 int nfp_flower_compile_action(struct tc_cls_flower_offload *flow,
178 			      struct net_device *netdev,
179 			      struct nfp_fl_payload *nfp_flow);
180 int nfp_compile_flow_metadata(struct nfp_app *app,
181 			      struct tc_cls_flower_offload *flow,
182 			      struct nfp_fl_payload *nfp_flow);
183 int nfp_modify_flow_metadata(struct nfp_app *app,
184 			     struct nfp_fl_payload *nfp_flow);
185 
186 struct nfp_fl_payload *
187 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie);
188 struct nfp_fl_payload *
189 nfp_flower_remove_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie);
190 
191 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb);
192 
193 int nfp_tunnel_config_start(struct nfp_app *app);
194 void nfp_tunnel_config_stop(struct nfp_app *app);
195 void nfp_tunnel_write_macs(struct nfp_app *app);
196 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4);
197 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4);
198 void nfp_tunnel_request_route(struct nfp_app *app, struct sk_buff *skb);
199 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb);
200 
201 #endif
202