xref: /linux/drivers/net/ethernet/sfc/tc.h (revision 04317b129e4eb5c6f4a58bb899b2019c1545320b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2019 Solarflare Communications Inc.
5  * Copyright 2020-2022 Xilinx Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published
9  * by the Free Software Foundation, incorporated herein by reference.
10  */
11 
12 #ifndef EFX_TC_H
13 #define EFX_TC_H
14 #include <net/flow_offload.h>
15 #include <linux/rhashtable.h>
16 #include "net_driver.h"
17 #include "tc_counters.h"
18 
19 #define IS_ALL_ONES(v)	(!(typeof (v))~(v))
20 
21 /**
22  * struct efx_tc_mac_pedit_action - mac pedit action fields
23  *
24  * @h_addr:	mac address field of ethernet header
25  * @linkage:	rhashtable reference
26  * @ref:	reference count
27  * @fw_id:	index of this entry in firmware MAC address table
28  *
29  * MAC address edits are indirected through a table in the hardware
30  */
31 struct efx_tc_mac_pedit_action {
32 	u8 h_addr[ETH_ALEN];
33 	struct rhash_head linkage;
34 	refcount_t ref;
35 	u32 fw_id; /* index of this entry in firmware MAC address table */
36 };
37 
38 static inline bool efx_ipv6_addr_all_ones(struct in6_addr *addr)
39 {
40 	return !memchr_inv(addr, 0xff, sizeof(*addr));
41 }
42 
43 struct efx_tc_encap_action; /* see tc_encap_actions.h */
44 
45 /**
46  * struct efx_tc_action_set - collection of tc action fields
47  *
48  * @vlan_push: the number of vlan headers to push
49  * @vlan_pop: the number of vlan headers to pop
50  * @decap: used to indicate a tunnel header decapsulation should take place
51  * @do_ttl_dec: used to indicate IP TTL / Hop Limit should be decremented
52  * @deliver: used to indicate a deliver action should take place
53  * @vlan_tci: tci fields for vlan push actions
54  * @vlan_proto: ethernet types for vlan push actions
55  * @count: counter mapping
56  * @encap_md: encap entry in tc_encap_ht table
57  * @encap_user: linked list of encap users (encap_md->users)
58  * @user: owning action-set-list. Only populated if @encap_md is; used by efx_tc_update_encap() fallback handling
59  * @count_user: linked list of counter users (counter->users)
60  * @dest_mport: destination mport
61  * @src_mac: source mac entry in tc_mac_ht table
62  * @dst_mac: destination mac entry in tc_mac_ht table
63  * @fw_id: index of this entry in firmware actions table
64  * @list: linked list of tc actions
65  *
66  */
67 struct efx_tc_action_set {
68 	u16 vlan_push:2;
69 	u16 vlan_pop:2;
70 	u16 decap:1;
71 	u16 do_ttl_dec:1;
72 	u16 deliver:1;
73 	__be16 vlan_tci[2];
74 	__be16 vlan_proto[2];
75 	struct efx_tc_counter_index *count;
76 	struct efx_tc_encap_action *encap_md;
77 	struct list_head encap_user;
78 	struct efx_tc_action_set_list *user;
79 	struct list_head count_user;
80 	u32 dest_mport;
81 	struct efx_tc_mac_pedit_action *src_mac;
82 	struct efx_tc_mac_pedit_action *dst_mac;
83 	u32 fw_id;
84 	struct list_head list;
85 };
86 
87 struct efx_tc_match_fields {
88 	/* L1 */
89 	u32 ingress_port;
90 	u8 recirc_id; /* mapped from (u32) TC chain_index to smaller space */
91 	/* L2 (inner when encap) */
92 	__be16 eth_proto;
93 	__be16 vlan_tci[2], vlan_proto[2];
94 	u8 eth_saddr[ETH_ALEN], eth_daddr[ETH_ALEN];
95 	/* L3 (when IP) */
96 	u8 ip_proto, ip_tos, ip_ttl;
97 	__be32 src_ip, dst_ip;
98 #ifdef CONFIG_IPV6
99 	struct in6_addr src_ip6, dst_ip6;
100 #endif
101 	bool ip_frag, ip_firstfrag;
102 	/* L4 */
103 	__be16 l4_sport, l4_dport; /* Ports (UDP, TCP) */
104 	__be16 tcp_flags;
105 	bool tcp_syn_fin_rst; /* true if ANY of SYN/FIN/RST are set */
106 	/* Encap.  The following are *outer* fields.  Note that there are no
107 	 * outer eth (L2) fields; this is because TC doesn't have them.
108 	 */
109 	__be32 enc_src_ip, enc_dst_ip;
110 	struct in6_addr enc_src_ip6, enc_dst_ip6;
111 	u8 enc_ip_tos, enc_ip_ttl;
112 	__be16 enc_sport, enc_dport;
113 	__be32 enc_keyid; /* e.g. VNI, VSID */
114 	/* Conntrack. */
115 	u16 ct_state_trk:1, ct_state_est:1;
116 	u32 ct_mark;
117 	u16 ct_zone;
118 };
119 
120 static inline bool efx_tc_match_is_encap(const struct efx_tc_match_fields *mask)
121 {
122 	return mask->enc_src_ip || mask->enc_dst_ip ||
123 	       !ipv6_addr_any(&mask->enc_src_ip6) ||
124 	       !ipv6_addr_any(&mask->enc_dst_ip6) || mask->enc_ip_tos ||
125 	       mask->enc_ip_ttl || mask->enc_sport || mask->enc_dport;
126 }
127 
128 /**
129  * enum efx_tc_em_pseudo_type - &struct efx_tc_encap_match pseudo type
130  *
131  * These are used to classify "pseudo" encap matches, which don't refer
132  * to an entry in hardware but rather indicate that a section of the
133  * match space is in use by another Outer Rule.
134  *
135  * @EFX_TC_EM_DIRECT: real HW entry in Outer Rule table; not a pseudo.
136  *	Hardware index in &struct efx_tc_encap_match.fw_id is valid.
137  * @EFX_TC_EM_PSEUDO_MASK: registered by an encap match which includes a
138  *	match on an optional field (currently ip_tos and/or udp_sport),
139  *	to prevent an overlapping encap match _without_ optional fields.
140  *	The pseudo encap match may be referenced again by an encap match
141  *	with different values for these fields, but all masks must match the
142  *	first (stored in our child_* fields).
143  * @EFX_TC_EM_PSEUDO_OR: registered by an fLHS rule that fits in the OR
144  *	table.  The &struct efx_tc_lhs_rule already holds the HW OR entry.
145  *	Only one reference to this encap match may exist.
146  */
147 enum efx_tc_em_pseudo_type {
148 	EFX_TC_EM_DIRECT,
149 	EFX_TC_EM_PSEUDO_MASK,
150 	EFX_TC_EM_PSEUDO_OR,
151 };
152 
153 struct efx_tc_encap_match {
154 	__be32 src_ip, dst_ip;
155 	struct in6_addr src_ip6, dst_ip6;
156 	__be16 udp_dport;
157 	__be16 udp_sport, udp_sport_mask;
158 	u8 ip_tos, ip_tos_mask;
159 	struct rhash_head linkage;
160 	enum efx_encap_type tun_type;
161 	u8 child_ip_tos_mask;
162 	__be16 child_udp_sport_mask;
163 	refcount_t ref;
164 	enum efx_tc_em_pseudo_type type;
165 	u32 fw_id; /* index of this entry in firmware encap match table */
166 	struct efx_tc_encap_match *pseudo; /* Referenced pseudo EM if needed */
167 };
168 
169 struct efx_tc_recirc_id {
170 	u32 chain_index;
171 	struct net_device *net_dev;
172 	struct rhash_head linkage;
173 	refcount_t ref;
174 	u8 fw_id; /* index allocated for use in the MAE */
175 };
176 
177 struct efx_tc_match {
178 	struct efx_tc_match_fields value;
179 	struct efx_tc_match_fields mask;
180 	struct efx_tc_encap_match *encap;
181 	struct efx_tc_recirc_id *rid;
182 };
183 
184 struct efx_tc_action_set_list {
185 	struct list_head list;
186 	u32 fw_id;
187 };
188 
189 struct efx_tc_lhs_action {
190 	enum efx_encap_type tun_type;
191 	struct efx_tc_recirc_id *rid;
192 	struct efx_tc_ct_zone *zone;
193 	struct efx_tc_counter_index *count;
194 };
195 
196 struct efx_tc_flow_rule {
197 	unsigned long cookie;
198 	struct rhash_head linkage;
199 	struct efx_tc_match match;
200 	struct efx_tc_action_set_list acts;
201 	struct efx_tc_action_set_list *fallback; /* what to use when unready? */
202 	u32 fw_id;
203 };
204 
205 struct efx_tc_lhs_rule {
206 	unsigned long cookie;
207 	struct efx_tc_match match;
208 	struct efx_tc_lhs_action lhs_act;
209 	struct rhash_head linkage;
210 	u32 fw_id;
211 	bool is_ar; /* Action Rule (for OR-AR-CT-AR sequence) */
212 };
213 
214 enum efx_tc_rule_prios {
215 	EFX_TC_PRIO_TC, /* Rule inserted by TC */
216 	EFX_TC_PRIO_DFLT, /* Default switch rule; one of efx_tc_default_rules */
217 	EFX_TC_PRIO__NUM
218 };
219 
220 struct efx_tc_table_field_fmt {
221 	u16 field_id;
222 	u16 lbn;
223 	u16 width;
224 	u8 masking;
225 	u8 scheme;
226 };
227 
228 struct efx_tc_table_desc {
229 	u16 type;
230 	u16 key_width;
231 	u16 resp_width;
232 	u16 n_keys;
233 	u16 n_resps;
234 	u16 n_prios;
235 	u8 flags;
236 	u8 scheme;
237 	struct efx_tc_table_field_fmt *keys;
238 	struct efx_tc_table_field_fmt *resps;
239 };
240 
241 struct efx_tc_table_ct { /* TABLE_ID_CONNTRACK_TABLE */
242 	struct efx_tc_table_desc desc;
243 	bool hooked;
244 	struct { /* indices of named fields within @desc.keys */
245 		u8 eth_proto_idx;
246 		u8 ip_proto_idx;
247 		u8 src_ip_idx; /* either v4 or v6 */
248 		u8 dst_ip_idx;
249 		u8 l4_sport_idx;
250 		u8 l4_dport_idx;
251 		u8 zone_idx; /* for TABLE_FIELD_ID_DOMAIN */
252 	} keys;
253 	struct { /* indices of named fields within @desc.resps */
254 		u8 dnat_idx;
255 		u8 nat_ip_idx;
256 		u8 l4_natport_idx;
257 		u8 mark_idx;
258 		u8 counter_id_idx;
259 	} resps;
260 };
261 
262 /**
263  * struct efx_tc_state - control plane data for TC offload
264  *
265  * @caps: MAE capabilities reported by MCDI
266  * @block_list: List of &struct efx_tc_block_binding
267  * @mutex: Used to serialise operations on TC hashtables
268  * @counter_ht: Hashtable of TC counters (FW IDs and counter values)
269  * @counter_id_ht: Hashtable mapping TC counter cookies to counters
270  * @encap_ht: Hashtable of TC encap actions
271  * @mac_ht: Hashtable of MAC address entries (for pedits)
272  * @encap_match_ht: Hashtable of TC encap matches
273  * @match_action_ht: Hashtable of TC match-action rules
274  * @lhs_rule_ht: Hashtable of TC left-hand (act ct & goto chain) rules
275  * @ct_zone_ht: Hashtable of TC conntrack flowtable bindings
276  * @ct_ht: Hashtable of TC conntrack flow entries
277  * @neigh_ht: Hashtable of neighbour watches (&struct efx_neigh_binder)
278  * @recirc_ht: Hashtable of recirculation ID mappings (&struct efx_tc_recirc_id)
279  * @recirc_ida: Recirculation ID allocator
280  * @meta_ct: MAE table layout for conntrack table
281  * @reps_mport_id: MAE port allocated for representor RX
282  * @reps_filter_uc: VNIC filter for representor unicast RX (promisc)
283  * @reps_filter_mc: VNIC filter for representor multicast RX (allmulti)
284  * @reps_mport_vport_id: vport_id for representor RX filters
285  * @flush_counters: counters have been stopped, waiting for drain
286  * @flush_gen: final generation count per type array as reported by
287  *             MC_CMD_MAE_COUNTERS_STREAM_STOP
288  * @seen_gen: most recent generation count per type as seen by efx_tc_rx()
289  * @flush_wq: wait queue used by efx_mae_stop_counters() to wait for
290  *	MAE counters RXQ to finish draining
291  * @dflt: Match-action rules for default switching; at priority
292  *	%EFX_TC_PRIO_DFLT.  Named by *ingress* port
293  * @dflt.pf: rule for traffic ingressing from PF (egresses to wire)
294  * @dflt.wire: rule for traffic ingressing from wire (egresses to PF)
295  * @facts: Fallback action-set-lists for unready rules.  Named by *egress* port
296  * @facts.pf: action-set-list for unready rules on PF netdev, hence applying to
297  *	traffic from wire, and egressing to PF
298  * @facts.reps: action-set-list for unready rules on representors, hence
299  *	applying to traffic from representees, and egressing to the reps mport
300  * @up: have TC datastructures been set up?
301  */
302 struct efx_tc_state {
303 	struct mae_caps *caps;
304 	struct list_head block_list;
305 	struct mutex mutex;
306 	struct rhashtable counter_ht;
307 	struct rhashtable counter_id_ht;
308 	struct rhashtable encap_ht;
309 	struct rhashtable mac_ht;
310 	struct rhashtable encap_match_ht;
311 	struct rhashtable match_action_ht;
312 	struct rhashtable lhs_rule_ht;
313 	struct rhashtable ct_zone_ht;
314 	struct rhashtable ct_ht;
315 	struct rhashtable neigh_ht;
316 	struct rhashtable recirc_ht;
317 	struct ida recirc_ida;
318 	struct efx_tc_table_ct meta_ct;
319 	u32 reps_mport_id, reps_mport_vport_id;
320 	s32 reps_filter_uc, reps_filter_mc;
321 	bool flush_counters;
322 	u32 flush_gen[EFX_TC_COUNTER_TYPE_MAX];
323 	u32 seen_gen[EFX_TC_COUNTER_TYPE_MAX];
324 	wait_queue_head_t flush_wq;
325 	struct {
326 		struct efx_tc_flow_rule pf;
327 		struct efx_tc_flow_rule wire;
328 	} dflt;
329 	struct {
330 		struct efx_tc_action_set_list pf;
331 		struct efx_tc_action_set_list reps;
332 	} facts;
333 	bool up;
334 };
335 
336 struct efx_rep;
337 
338 enum efx_encap_type efx_tc_indr_netdev_type(struct net_device *net_dev);
339 struct efx_rep *efx_tc_flower_lookup_efv(struct efx_nic *efx,
340 					 struct net_device *dev);
341 s64 efx_tc_flower_external_mport(struct efx_nic *efx, struct efx_rep *efv);
342 int efx_tc_configure_default_rule_rep(struct efx_rep *efv);
343 void efx_tc_deconfigure_default_rule(struct efx_nic *efx,
344 				     struct efx_tc_flow_rule *rule);
345 int efx_tc_flower(struct efx_nic *efx, struct net_device *net_dev,
346 		  struct flow_cls_offload *tc, struct efx_rep *efv);
347 
348 int efx_tc_insert_rep_filters(struct efx_nic *efx);
349 void efx_tc_remove_rep_filters(struct efx_nic *efx);
350 
351 int efx_init_tc(struct efx_nic *efx);
352 void efx_fini_tc(struct efx_nic *efx);
353 
354 int efx_init_struct_tc(struct efx_nic *efx);
355 void efx_fini_struct_tc(struct efx_nic *efx);
356 
357 #endif /* EFX_TC_H */
358