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 };
87 
88 /**
89  * struct nfp_fl_ct_flow_entry - Flow entry containing conntrack flow information
90  * @cookie:	Flow cookie, same as original TC flow, used as key
91  * @list_node:	Used by the list
92  * @chain_index:	Chain index of the original flow
93  * @netdev:	netdev structure.
94  * @type:	Type of pre-entry from enum ct_entry_type
95  * @zt:		Reference to the zone table this belongs to
96  * @children:	List of tc_merge flows this flow forms part of
97  * @rule:	Reference to the original TC flow rule
98  * @stats:	Used to cache stats for updating
99  * @tun_offset: Used to indicate tunnel action offset in action list
100  */
101 struct nfp_fl_ct_flow_entry {
102 	unsigned long cookie;
103 	struct list_head list_node;
104 	u32 chain_index;
105 	enum ct_entry_type type;
106 	struct net_device *netdev;
107 	struct nfp_fl_ct_zone_entry *zt;
108 	struct list_head children;
109 	struct flow_rule *rule;
110 	struct flow_stats stats;
111 	u8 tun_offset;		// Set to NFP_FL_CT_NO_TUN if no tun
112 };
113 
114 /**
115  * struct nfp_fl_ct_tc_merge - Merge of two flows from tc
116  * @cookie:		Flow cookie, combination of pre and post ct cookies
117  * @hash_node:		Used by the hashtable
118  * @pre_ct_list:	This entry is part of a pre_ct_list
119  * @post_ct_list:	This entry is part of a post_ct_list
120  * @zt:			Reference to the zone table this belongs to
121  * @pre_ct_parent:	The pre_ct_parent
122  * @post_ct_parent:	The post_ct_parent
123  * @children:		List of nft merged entries
124  */
125 struct nfp_fl_ct_tc_merge {
126 	unsigned long cookie[2];
127 	struct rhash_head hash_node;
128 	struct list_head pre_ct_list;
129 	struct list_head post_ct_list;
130 	struct nfp_fl_ct_zone_entry *zt;
131 	struct nfp_fl_ct_flow_entry *pre_ct_parent;
132 	struct nfp_fl_ct_flow_entry *post_ct_parent;
133 	struct list_head children;
134 };
135 
136 /**
137  * struct nfp_fl_nft_tc_merge - Merge of tc_merge flows with nft flow
138  * @netdev:		Ingress netdev name
139  * @cookie:		Flow cookie, combination of tc_merge and nft cookies
140  * @hash_node:		Used by the hashtable
141  * @zt:	Reference to the zone table this belongs to
142  * @nft_flow_list:	This entry is part of a nft_flows_list
143  * @tc_merge_list:	This entry is part of a ct_merge_list
144  * @tc_m_parent:	The tc_merge parent
145  * @nft_parent:	The nft_entry parent
146  * @tc_flower_cookie:	The cookie of the flow offloaded to the nfp
147  * @flow_pay:	Reference to the offloaded flow struct
148  */
149 struct nfp_fl_nft_tc_merge {
150 	struct net_device *netdev;
151 	unsigned long cookie[3];
152 	struct rhash_head hash_node;
153 	struct nfp_fl_ct_zone_entry *zt;
154 	struct list_head nft_flow_list;
155 	struct list_head tc_merge_list;
156 	struct nfp_fl_ct_tc_merge *tc_m_parent;
157 	struct nfp_fl_ct_flow_entry *nft_parent;
158 	unsigned long tc_flower_cookie;
159 	struct nfp_fl_payload *flow_pay;
160 };
161 
162 /**
163  * struct nfp_fl_ct_map_entry - Map between flow cookie and specific ct_flow
164  * @cookie:	Flow cookie, same as original TC flow, used as key
165  * @hash_node:	Used by the hashtable
166  * @ct_entry:	Pointer to corresponding ct_entry
167  */
168 struct nfp_fl_ct_map_entry {
169 	unsigned long cookie;
170 	struct rhash_head hash_node;
171 	struct nfp_fl_ct_flow_entry *ct_entry;
172 };
173 
174 bool is_pre_ct_flow(struct flow_cls_offload *flow);
175 bool is_post_ct_flow(struct flow_cls_offload *flow);
176 
177 /**
178  * nfp_fl_ct_handle_pre_ct() - Handles -trk conntrack rules
179  * @priv:	Pointer to app priv
180  * @netdev:	netdev structure.
181  * @flow:	TC flower classifier offload structure.
182  * @extack:	Extack pointer for errors
183  *
184  * Adds a new entry to the relevant zone table and tries to
185  * merge with other +trk+est entries and offload if possible.
186  *
187  * Return: negative value on error, 0 if configured successfully.
188  */
189 int nfp_fl_ct_handle_pre_ct(struct nfp_flower_priv *priv,
190 			    struct net_device *netdev,
191 			    struct flow_cls_offload *flow,
192 			    struct netlink_ext_ack *extack);
193 /**
194  * nfp_fl_ct_handle_post_ct() - Handles +trk+est conntrack rules
195  * @priv:	Pointer to app priv
196  * @netdev:	netdev structure.
197  * @flow:	TC flower classifier offload structure.
198  * @extack:	Extack pointer for errors
199  *
200  * Adds a new entry to the relevant zone table and tries to
201  * merge with other -trk entries and offload if possible.
202  *
203  * Return: negative value on error, 0 if configured successfully.
204  */
205 int nfp_fl_ct_handle_post_ct(struct nfp_flower_priv *priv,
206 			     struct net_device *netdev,
207 			     struct flow_cls_offload *flow,
208 			     struct netlink_ext_ack *extack);
209 
210 /**
211  * nfp_fl_ct_clean_flow_entry() - Free a nfp_fl_ct_flow_entry
212  * @entry:	Flow entry to cleanup
213  */
214 void nfp_fl_ct_clean_flow_entry(struct nfp_fl_ct_flow_entry *entry);
215 
216 /**
217  * nfp_fl_ct_del_flow() - Handle flow_del callbacks for conntrack
218  * @ct_map_ent:	ct map entry for the flow that needs deleting
219  */
220 int nfp_fl_ct_del_flow(struct nfp_fl_ct_map_entry *ct_map_ent);
221 
222 /**
223  * nfp_fl_ct_handle_nft_flow() - Handle flower flow callbacks for nft table
224  * @type:	Type provided by callback
225  * @type_data:	Callback data
226  * @cb_priv:	Pointer to data provided when registering the callback, in this
227  *		case it's the zone table.
228  */
229 int nfp_fl_ct_handle_nft_flow(enum tc_setup_type type, void *type_data,
230 			      void *cb_priv);
231 #endif
232