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 "main.h"
8 
9 #define NFP_FL_CT_NO_TUN	0xff
10 
11 extern const struct rhashtable_params nfp_zone_table_params;
12 extern const struct rhashtable_params nfp_ct_map_params;
13 extern const struct rhashtable_params nfp_tc_ct_merge_params;
14 
15 /**
16  * struct nfp_fl_ct_zone_entry - Zone entry containing conntrack flow information
17  * @zone:	The zone number, used as lookup key in hashtable
18  * @hash_node:	Used by the hashtable
19  * @priv:	Pointer to nfp_flower_priv data
20  * @nft:	Pointer to nf_flowtable for this zone
21  *
22  * @pre_ct_list:	The pre_ct_list of nfp_fl_ct_flow_entry entries
23  * @pre_ct_count:	Keep count of the number of pre_ct entries
24  *
25  * @post_ct_list:	The post_ct_list of nfp_fl_ct_flow_entry entries
26  * @post_ct_count:	Keep count of the number of post_ct entries
27  *
28  * @tc_merge_tb:	The table of merged tc flows
29  * @tc_merge_count:	Keep count of the number of merged tc entries
30  */
31 struct nfp_fl_ct_zone_entry {
32 	u16 zone;
33 	struct rhash_head hash_node;
34 
35 	struct nfp_flower_priv *priv;
36 	struct nf_flowtable *nft;
37 
38 	struct list_head pre_ct_list;
39 	unsigned int pre_ct_count;
40 
41 	struct list_head post_ct_list;
42 	unsigned int post_ct_count;
43 
44 	struct rhashtable tc_merge_tb;
45 	unsigned int tc_merge_count;
46 };
47 
48 enum ct_entry_type {
49 	CT_TYPE_PRE_CT,
50 	CT_TYPE_NFT,
51 	CT_TYPE_POST_CT,
52 };
53 
54 /**
55  * struct nfp_fl_ct_flow_entry - Flow entry containing conntrack flow information
56  * @cookie:	Flow cookie, same as original TC flow, used as key
57  * @list_node:	Used by the list
58  * @chain_index:	Chain index of the original flow
59  * @netdev:	netdev structure.
60  * @type:	Type of pre-entry from enum ct_entry_type
61  * @zt:		Reference to the zone table this belongs to
62  * @children:	List of tc_merge flows this flow forms part of
63  * @rule:	Reference to the original TC flow rule
64  * @stats:	Used to cache stats for updating
65  * @tun_offset: Used to indicate tunnel action offset in action list
66  */
67 struct nfp_fl_ct_flow_entry {
68 	unsigned long cookie;
69 	struct list_head list_node;
70 	u32 chain_index;
71 	enum ct_entry_type type;
72 	struct net_device *netdev;
73 	struct nfp_fl_ct_zone_entry *zt;
74 	struct list_head children;
75 	struct flow_rule *rule;
76 	struct flow_stats stats;
77 	u8 tun_offset;		// Set to NFP_FL_CT_NO_TUN if no tun
78 };
79 
80 /**
81  * struct nfp_fl_ct_tc_merge - Merge of two flows from tc
82  * @cookie:		Flow cookie, combination of pre and post ct cookies
83  * @hash_node:		Used by the hashtable
84  * @pre_ct_list:	This entry is part of a pre_ct_list
85  * @post_ct_list:	This entry is part of a post_ct_list
86  * @zt:			Reference to the zone table this belongs to
87  * @pre_ct_parent:	The pre_ct_parent
88  * @post_ct_parent:	The post_ct_parent
89  * @children:		List of nft merged entries
90  */
91 struct nfp_fl_ct_tc_merge {
92 	unsigned long cookie[2];
93 	struct rhash_head hash_node;
94 	struct list_head pre_ct_list;
95 	struct list_head post_ct_list;
96 	struct nfp_fl_ct_zone_entry *zt;
97 	struct nfp_fl_ct_flow_entry *pre_ct_parent;
98 	struct nfp_fl_ct_flow_entry *post_ct_parent;
99 	struct list_head children;
100 };
101 
102 /**
103  * struct nfp_fl_ct_map_entry - Map between flow cookie and specific ct_flow
104  * @cookie:	Flow cookie, same as original TC flow, used as key
105  * @hash_node:	Used by the hashtable
106  * @ct_entry:	Pointer to corresponding ct_entry
107  */
108 struct nfp_fl_ct_map_entry {
109 	unsigned long cookie;
110 	struct rhash_head hash_node;
111 	struct nfp_fl_ct_flow_entry *ct_entry;
112 };
113 
114 bool is_pre_ct_flow(struct flow_cls_offload *flow);
115 bool is_post_ct_flow(struct flow_cls_offload *flow);
116 
117 /**
118  * nfp_fl_ct_handle_pre_ct() - Handles -trk conntrack rules
119  * @priv:	Pointer to app priv
120  * @netdev:	netdev structure.
121  * @flow:	TC flower classifier offload structure.
122  * @extack:	Extack pointer for errors
123  *
124  * Adds a new entry to the relevant zone table and tries to
125  * merge with other +trk+est entries and offload if possible.
126  *
127  * Return: negative value on error, 0 if configured successfully.
128  */
129 int nfp_fl_ct_handle_pre_ct(struct nfp_flower_priv *priv,
130 			    struct net_device *netdev,
131 			    struct flow_cls_offload *flow,
132 			    struct netlink_ext_ack *extack);
133 /**
134  * nfp_fl_ct_handle_post_ct() - Handles +trk+est conntrack rules
135  * @priv:	Pointer to app priv
136  * @netdev:	netdev structure.
137  * @flow:	TC flower classifier offload structure.
138  * @extack:	Extack pointer for errors
139  *
140  * Adds a new entry to the relevant zone table and tries to
141  * merge with other -trk entries and offload if possible.
142  *
143  * Return: negative value on error, 0 if configured successfully.
144  */
145 int nfp_fl_ct_handle_post_ct(struct nfp_flower_priv *priv,
146 			     struct net_device *netdev,
147 			     struct flow_cls_offload *flow,
148 			     struct netlink_ext_ack *extack);
149 
150 /**
151  * nfp_fl_ct_clean_flow_entry() - Free a nfp_fl_ct_flow_entry
152  * @entry:	Flow entry to cleanup
153  */
154 void nfp_fl_ct_clean_flow_entry(struct nfp_fl_ct_flow_entry *entry);
155 #endif
156