1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2021 Corigine, Inc. */
3 
4 #ifndef __NFP_FLOWER_CONNTRACK_H__
5 #define __NFP_FLOWER_CONNTRACK_H__ 1
6 
7 #include <net/netfilter/nf_flow_table.h>
8 #include "main.h"
9 
10 #define NFP_FL_CT_NO_TUN	0xff
11 
12 #define COMPARE_UNMASKED_FIELDS(__match1, __match2, __out)	\
13 	do {							\
14 		typeof(__match1) _match1 = (__match1);		\
15 		typeof(__match2) _match2 = (__match2);		\
16 		bool *_out = (__out);		\
17 		int i, size = sizeof(*(_match1).key);		\
18 		char *k1, *m1, *k2, *m2;			\
19 		*_out = false;					\
20 		k1 = (char *)_match1.key;			\
21 		m1 = (char *)_match1.mask;			\
22 		k2 = (char *)_match2.key;			\
23 		m2 = (char *)_match2.mask;			\
24 		for (i = 0; i < size; i++)			\
25 			if ((k1[i] & m1[i] & m2[i]) ^		\
26 			    (k2[i] & m1[i] & m2[i])) {		\
27 				*_out = true;			\
28 				break;				\
29 			}					\
30 	} while (0)						\
31 
32 extern const struct rhashtable_params nfp_zone_table_params;
33 extern const struct rhashtable_params nfp_ct_map_params;
34 extern const struct rhashtable_params nfp_tc_ct_merge_params;
35 extern const struct rhashtable_params nfp_nft_ct_merge_params;
36 
37 /**
38  * struct nfp_fl_ct_zone_entry - Zone entry containing conntrack flow information
39  * @zone:	The zone number, used as lookup key in hashtable
40  * @hash_node:	Used by the hashtable
41  * @priv:	Pointer to nfp_flower_priv data
42  * @nft:	Pointer to nf_flowtable for this zone
43  *
44  * @pre_ct_list:	The pre_ct_list of nfp_fl_ct_flow_entry entries
45  * @pre_ct_count:	Keep count of the number of pre_ct entries
46  *
47  * @post_ct_list:	The post_ct_list of nfp_fl_ct_flow_entry entries
48  * @post_ct_count:	Keep count of the number of post_ct entries
49  *
50  * @tc_merge_tb:	The table of merged tc flows
51  * @tc_merge_count:	Keep count of the number of merged tc entries
52  *
53  * @nft_flows_list:	The list of nft relatednfp_fl_ct_flow_entry entries
54  * @nft_flows_count:	Keep count of the number of nft_flow entries
55  *
56  * @nft_merge_tb:	The table of merged tc+nft flows
57  * @nft_merge_count:	Keep count of the number of merged tc+nft entries
58  */
59 struct nfp_fl_ct_zone_entry {
60 	u16 zone;
61 	struct rhash_head hash_node;
62 
63 	struct nfp_flower_priv *priv;
64 	struct nf_flowtable *nft;
65 
66 	struct list_head pre_ct_list;
67 	unsigned int pre_ct_count;
68 
69 	struct list_head post_ct_list;
70 	unsigned int post_ct_count;
71 
72 	struct rhashtable tc_merge_tb;
73 	unsigned int tc_merge_count;
74 
75 	struct list_head nft_flows_list;
76 	unsigned int nft_flows_count;
77 
78 	struct rhashtable nft_merge_tb;
79 	unsigned int nft_merge_count;
80 };
81 
82 enum ct_entry_type {
83 	CT_TYPE_PRE_CT,
84 	CT_TYPE_NFT,
85 	CT_TYPE_POST_CT,
86 	_CT_TYPE_MAX,
87 };
88 
89 enum nfp_nfp_layer_name {
90 	FLOW_PAY_META_TCI =    0,
91 	FLOW_PAY_INPORT,
92 	FLOW_PAY_EXT_META,
93 	FLOW_PAY_MAC_MPLS,
94 	FLOW_PAY_L4,
95 	FLOW_PAY_IPV4,
96 	FLOW_PAY_IPV6,
97 	FLOW_PAY_CT,
98 	FLOW_PAY_GRE,
99 	FLOW_PAY_QINQ,
100 	FLOW_PAY_UDP_TUN,
101 	FLOW_PAY_GENEVE_OPT,
102 
103 	_FLOW_PAY_LAYERS_MAX
104 };
105 
106 /* NFP flow entry flags. */
107 #define NFP_FL_ACTION_DO_NAT		BIT(0)
108 #define NFP_FL_ACTION_DO_MANGLE		BIT(1)
109 
110 /**
111  * struct nfp_fl_ct_flow_entry - Flow entry containing conntrack flow information
112  * @cookie:	Flow cookie, same as original TC flow, used as key
113  * @list_node:	Used by the list
114  * @chain_index:	Chain index of the original flow
115  * @netdev:	netdev structure.
116  * @type:	Type of pre-entry from enum ct_entry_type
117  * @zt:		Reference to the zone table this belongs to
118  * @children:	List of tc_merge flows this flow forms part of
119  * @rule:	Reference to the original TC flow rule
120  * @stats:	Used to cache stats for updating
121  * @tun_offset: Used to indicate tunnel action offset in action list
122  * @flags:	Used to indicate flow flag like NAT which used by merge.
123  */
124 struct nfp_fl_ct_flow_entry {
125 	unsigned long cookie;
126 	struct list_head list_node;
127 	u32 chain_index;
128 	enum ct_entry_type type;
129 	struct net_device *netdev;
130 	struct nfp_fl_ct_zone_entry *zt;
131 	struct list_head children;
132 	struct flow_rule *rule;
133 	struct flow_stats stats;
134 	u8 tun_offset;		// Set to NFP_FL_CT_NO_TUN if no tun
135 	u8 flags;
136 };
137 
138 /**
139  * struct nfp_fl_ct_tc_merge - Merge of two flows from tc
140  * @cookie:		Flow cookie, combination of pre and post ct cookies
141  * @hash_node:		Used by the hashtable
142  * @pre_ct_list:	This entry is part of a pre_ct_list
143  * @post_ct_list:	This entry is part of a post_ct_list
144  * @zt:			Reference to the zone table this belongs to
145  * @pre_ct_parent:	The pre_ct_parent
146  * @post_ct_parent:	The post_ct_parent
147  * @children:		List of nft merged entries
148  */
149 struct nfp_fl_ct_tc_merge {
150 	unsigned long cookie[2];
151 	struct rhash_head hash_node;
152 	struct list_head pre_ct_list;
153 	struct list_head post_ct_list;
154 	struct nfp_fl_ct_zone_entry *zt;
155 	struct nfp_fl_ct_flow_entry *pre_ct_parent;
156 	struct nfp_fl_ct_flow_entry *post_ct_parent;
157 	struct list_head children;
158 };
159 
160 /**
161  * struct nfp_fl_nft_tc_merge - Merge of tc_merge flows with nft flow
162  * @netdev:		Ingress netdev name
163  * @cookie:		Flow cookie, combination of tc_merge and nft cookies
164  * @hash_node:		Used by the hashtable
165  * @zt:	Reference to the zone table this belongs to
166  * @nft_flow_list:	This entry is part of a nft_flows_list
167  * @tc_merge_list:	This entry is part of a ct_merge_list
168  * @tc_m_parent:	The tc_merge parent
169  * @nft_parent:	The nft_entry parent
170  * @tc_flower_cookie:	The cookie of the flow offloaded to the nfp
171  * @flow_pay:	Reference to the offloaded flow struct
172  */
173 struct nfp_fl_nft_tc_merge {
174 	struct net_device *netdev;
175 	unsigned long cookie[3];
176 	struct rhash_head hash_node;
177 	struct nfp_fl_ct_zone_entry *zt;
178 	struct list_head nft_flow_list;
179 	struct list_head tc_merge_list;
180 	struct nfp_fl_ct_tc_merge *tc_m_parent;
181 	struct nfp_fl_ct_flow_entry *nft_parent;
182 	unsigned long tc_flower_cookie;
183 	struct nfp_fl_payload *flow_pay;
184 };
185 
186 /**
187  * struct nfp_fl_ct_map_entry - Map between flow cookie and specific ct_flow
188  * @cookie:	Flow cookie, same as original TC flow, used as key
189  * @hash_node:	Used by the hashtable
190  * @ct_entry:	Pointer to corresponding ct_entry
191  */
192 struct nfp_fl_ct_map_entry {
193 	unsigned long cookie;
194 	struct rhash_head hash_node;
195 	struct nfp_fl_ct_flow_entry *ct_entry;
196 };
197 
198 bool is_pre_ct_flow(struct flow_cls_offload *flow);
199 bool is_post_ct_flow(struct flow_cls_offload *flow);
200 
201 /**
202  * nfp_fl_ct_handle_pre_ct() - Handles -trk conntrack rules
203  * @priv:	Pointer to app priv
204  * @netdev:	netdev structure.
205  * @flow:	TC flower classifier offload structure.
206  * @extack:	Extack pointer for errors
207  *
208  * Adds a new entry to the relevant zone table and tries to
209  * merge with other +trk+est entries and offload if possible.
210  *
211  * Return: negative value on error, 0 if configured successfully.
212  */
213 int nfp_fl_ct_handle_pre_ct(struct nfp_flower_priv *priv,
214 			    struct net_device *netdev,
215 			    struct flow_cls_offload *flow,
216 			    struct netlink_ext_ack *extack);
217 /**
218  * nfp_fl_ct_handle_post_ct() - Handles +trk+est conntrack rules
219  * @priv:	Pointer to app priv
220  * @netdev:	netdev structure.
221  * @flow:	TC flower classifier offload structure.
222  * @extack:	Extack pointer for errors
223  *
224  * Adds a new entry to the relevant zone table and tries to
225  * merge with other -trk entries and offload if possible.
226  *
227  * Return: negative value on error, 0 if configured successfully.
228  */
229 int nfp_fl_ct_handle_post_ct(struct nfp_flower_priv *priv,
230 			     struct net_device *netdev,
231 			     struct flow_cls_offload *flow,
232 			     struct netlink_ext_ack *extack);
233 
234 /**
235  * nfp_fl_ct_clean_flow_entry() - Free a nfp_fl_ct_flow_entry
236  * @entry:	Flow entry to cleanup
237  */
238 void nfp_fl_ct_clean_flow_entry(struct nfp_fl_ct_flow_entry *entry);
239 
240 /**
241  * nfp_fl_ct_del_flow() - Handle flow_del callbacks for conntrack
242  * @ct_map_ent:	ct map entry for the flow that needs deleting
243  */
244 int nfp_fl_ct_del_flow(struct nfp_fl_ct_map_entry *ct_map_ent);
245 
246 /**
247  * nfp_fl_ct_handle_nft_flow() - Handle flower flow callbacks for nft table
248  * @type:	Type provided by callback
249  * @type_data:	Callback data
250  * @cb_priv:	Pointer to data provided when registering the callback, in this
251  *		case it's the zone table.
252  */
253 int nfp_fl_ct_handle_nft_flow(enum tc_setup_type type, void *type_data,
254 			      void *cb_priv);
255 
256 /**
257  * nfp_fl_ct_stats() - Handle flower stats callbacks for ct flows
258  * @flow:	TC flower classifier offload structure.
259  * @ct_map_ent:	ct map entry for the flow that needs deleting
260  */
261 int nfp_fl_ct_stats(struct flow_cls_offload *flow,
262 		    struct nfp_fl_ct_map_entry *ct_map_ent);
263 #endif
264