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