xref: /openbmc/linux/drivers/net/ethernet/sfc/tc.h (revision 1909387fcfcfc9197a0adcaa9702967e5c18f812)
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 static inline bool efx_ipv6_addr_all_ones(struct in6_addr *addr)
22 {
23 	return !memchr_inv(addr, 0xff, sizeof(*addr));
24 }
25 
26 struct efx_tc_encap_action; /* see tc_encap_actions.h */
27 
28 struct efx_tc_action_set {
29 	u16 vlan_push:2;
30 	u16 vlan_pop:2;
31 	u16 decap:1;
32 	u16 deliver:1;
33 	__be16 vlan_tci[2]; /* TCIs for vlan_push */
34 	__be16 vlan_proto[2]; /* Ethertypes for vlan_push */
35 	struct efx_tc_counter_index *count;
36 	struct efx_tc_encap_action *encap_md; /* entry in tc_encap_ht table */
37 	struct list_head encap_user; /* entry on encap_md->users list */
38 	struct efx_tc_action_set_list *user; /* Only populated if encap_md */
39 	struct list_head count_user; /* entry on counter->users list, if encap */
40 	u32 dest_mport;
41 	u32 fw_id; /* index of this entry in firmware actions table */
42 	struct list_head list;
43 };
44 
45 struct efx_tc_match_fields {
46 	/* L1 */
47 	u32 ingress_port;
48 	u8 recirc_id;
49 	/* L2 (inner when encap) */
50 	__be16 eth_proto;
51 	__be16 vlan_tci[2], vlan_proto[2];
52 	u8 eth_saddr[ETH_ALEN], eth_daddr[ETH_ALEN];
53 	/* L3 (when IP) */
54 	u8 ip_proto, ip_tos, ip_ttl;
55 	__be32 src_ip, dst_ip;
56 #ifdef CONFIG_IPV6
57 	struct in6_addr src_ip6, dst_ip6;
58 #endif
59 	bool ip_frag, ip_firstfrag;
60 	/* L4 */
61 	__be16 l4_sport, l4_dport; /* Ports (UDP, TCP) */
62 	__be16 tcp_flags;
63 	/* Encap.  The following are *outer* fields.  Note that there are no
64 	 * outer eth (L2) fields; this is because TC doesn't have them.
65 	 */
66 	__be32 enc_src_ip, enc_dst_ip;
67 	struct in6_addr enc_src_ip6, enc_dst_ip6;
68 	u8 enc_ip_tos, enc_ip_ttl;
69 	__be16 enc_sport, enc_dport;
70 	__be32 enc_keyid; /* e.g. VNI, VSID */
71 };
72 
73 static inline bool efx_tc_match_is_encap(const struct efx_tc_match_fields *mask)
74 {
75 	return mask->enc_src_ip || mask->enc_dst_ip ||
76 	       !ipv6_addr_any(&mask->enc_src_ip6) ||
77 	       !ipv6_addr_any(&mask->enc_dst_ip6) || mask->enc_ip_tos ||
78 	       mask->enc_ip_ttl || mask->enc_sport || mask->enc_dport;
79 }
80 
81 /**
82  * enum efx_tc_em_pseudo_type - &struct efx_tc_encap_match pseudo type
83  *
84  * These are used to classify "pseudo" encap matches, which don't refer
85  * to an entry in hardware but rather indicate that a section of the
86  * match space is in use by another Outer Rule.
87  *
88  * @EFX_TC_EM_DIRECT: real HW entry in Outer Rule table; not a pseudo.
89  *	Hardware index in &struct efx_tc_encap_match.fw_id is valid.
90  * @EFX_TC_EM_PSEUDO_MASK: registered by an encap match which includes a
91  *	match on an optional field (currently ip_tos and/or udp_sport),
92  *	to prevent an overlapping encap match _without_ optional fields.
93  *	The pseudo encap match may be referenced again by an encap match
94  *	with different values for these fields, but all masks must match the
95  *	first (stored in our child_* fields).
96  */
97 enum efx_tc_em_pseudo_type {
98 	EFX_TC_EM_DIRECT,
99 	EFX_TC_EM_PSEUDO_MASK,
100 };
101 
102 struct efx_tc_encap_match {
103 	__be32 src_ip, dst_ip;
104 	struct in6_addr src_ip6, dst_ip6;
105 	__be16 udp_dport;
106 	__be16 udp_sport, udp_sport_mask;
107 	u8 ip_tos, ip_tos_mask;
108 	struct rhash_head linkage;
109 	enum efx_encap_type tun_type;
110 	u8 child_ip_tos_mask;
111 	__be16 child_udp_sport_mask;
112 	refcount_t ref;
113 	enum efx_tc_em_pseudo_type type;
114 	u32 fw_id; /* index of this entry in firmware encap match table */
115 	struct efx_tc_encap_match *pseudo; /* Referenced pseudo EM if needed */
116 };
117 
118 struct efx_tc_match {
119 	struct efx_tc_match_fields value;
120 	struct efx_tc_match_fields mask;
121 	struct efx_tc_encap_match *encap;
122 };
123 
124 struct efx_tc_action_set_list {
125 	struct list_head list;
126 	u32 fw_id;
127 };
128 
129 struct efx_tc_flow_rule {
130 	unsigned long cookie;
131 	struct rhash_head linkage;
132 	struct efx_tc_match match;
133 	struct efx_tc_action_set_list acts;
134 	struct efx_tc_action_set_list *fallback; /* what to use when unready? */
135 	u32 fw_id;
136 };
137 
138 enum efx_tc_rule_prios {
139 	EFX_TC_PRIO_TC, /* Rule inserted by TC */
140 	EFX_TC_PRIO_DFLT, /* Default switch rule; one of efx_tc_default_rules */
141 	EFX_TC_PRIO__NUM
142 };
143 
144 struct efx_tc_table_field_fmt {
145 	u16 field_id;
146 	u16 lbn;
147 	u16 width;
148 	u8 masking;
149 	u8 scheme;
150 };
151 
152 struct efx_tc_table_desc {
153 	u16 type;
154 	u16 key_width;
155 	u16 resp_width;
156 	u16 n_keys;
157 	u16 n_resps;
158 	u16 n_prios;
159 	u8 flags;
160 	u8 scheme;
161 	struct efx_tc_table_field_fmt *keys;
162 	struct efx_tc_table_field_fmt *resps;
163 };
164 
165 struct efx_tc_table_ct { /* TABLE_ID_CONNTRACK_TABLE */
166 	struct efx_tc_table_desc desc;
167 	bool hooked;
168 	struct { /* indices of named fields within @desc.keys */
169 		u8 eth_proto_idx;
170 		u8 ip_proto_idx;
171 		u8 src_ip_idx; /* either v4 or v6 */
172 		u8 dst_ip_idx;
173 		u8 l4_sport_idx;
174 		u8 l4_dport_idx;
175 		u8 zone_idx; /* for TABLE_FIELD_ID_DOMAIN */
176 	} keys;
177 	struct { /* indices of named fields within @desc.resps */
178 		u8 dnat_idx;
179 		u8 nat_ip_idx;
180 		u8 l4_natport_idx;
181 		u8 mark_idx;
182 		u8 counter_id_idx;
183 	} resps;
184 };
185 
186 /**
187  * struct efx_tc_state - control plane data for TC offload
188  *
189  * @caps: MAE capabilities reported by MCDI
190  * @block_list: List of &struct efx_tc_block_binding
191  * @mutex: Used to serialise operations on TC hashtables
192  * @counter_ht: Hashtable of TC counters (FW IDs and counter values)
193  * @counter_id_ht: Hashtable mapping TC counter cookies to counters
194  * @encap_ht: Hashtable of TC encap actions
195  * @encap_match_ht: Hashtable of TC encap matches
196  * @match_action_ht: Hashtable of TC match-action rules
197  * @ct_zone_ht: Hashtable of TC conntrack flowtable bindings
198  * @ct_ht: Hashtable of TC conntrack flow entries
199  * @neigh_ht: Hashtable of neighbour watches (&struct efx_neigh_binder)
200  * @meta_ct: MAE table layout for conntrack table
201  * @reps_mport_id: MAE port allocated for representor RX
202  * @reps_filter_uc: VNIC filter for representor unicast RX (promisc)
203  * @reps_filter_mc: VNIC filter for representor multicast RX (allmulti)
204  * @reps_mport_vport_id: vport_id for representor RX filters
205  * @flush_counters: counters have been stopped, waiting for drain
206  * @flush_gen: final generation count per type array as reported by
207  *             MC_CMD_MAE_COUNTERS_STREAM_STOP
208  * @seen_gen: most recent generation count per type as seen by efx_tc_rx()
209  * @flush_wq: wait queue used by efx_mae_stop_counters() to wait for
210  *	MAE counters RXQ to finish draining
211  * @dflt: Match-action rules for default switching; at priority
212  *	%EFX_TC_PRIO_DFLT.  Named by *ingress* port
213  * @dflt.pf: rule for traffic ingressing from PF (egresses to wire)
214  * @dflt.wire: rule for traffic ingressing from wire (egresses to PF)
215  * @facts: Fallback action-set-lists for unready rules.  Named by *egress* port
216  * @facts.pf: action-set-list for unready rules on PF netdev, hence applying to
217  *	traffic from wire, and egressing to PF
218  * @facts.reps: action-set-list for unready rules on representors, hence
219  *	applying to traffic from representees, and egressing to the reps mport
220  * @up: have TC datastructures been set up?
221  */
222 struct efx_tc_state {
223 	struct mae_caps *caps;
224 	struct list_head block_list;
225 	struct mutex mutex;
226 	struct rhashtable counter_ht;
227 	struct rhashtable counter_id_ht;
228 	struct rhashtable encap_ht;
229 	struct rhashtable encap_match_ht;
230 	struct rhashtable match_action_ht;
231 	struct rhashtable ct_zone_ht;
232 	struct rhashtable ct_ht;
233 	struct rhashtable neigh_ht;
234 	struct efx_tc_table_ct meta_ct;
235 	u32 reps_mport_id, reps_mport_vport_id;
236 	s32 reps_filter_uc, reps_filter_mc;
237 	bool flush_counters;
238 	u32 flush_gen[EFX_TC_COUNTER_TYPE_MAX];
239 	u32 seen_gen[EFX_TC_COUNTER_TYPE_MAX];
240 	wait_queue_head_t flush_wq;
241 	struct {
242 		struct efx_tc_flow_rule pf;
243 		struct efx_tc_flow_rule wire;
244 	} dflt;
245 	struct {
246 		struct efx_tc_action_set_list pf;
247 		struct efx_tc_action_set_list reps;
248 	} facts;
249 	bool up;
250 };
251 
252 struct efx_rep;
253 
254 enum efx_encap_type efx_tc_indr_netdev_type(struct net_device *net_dev);
255 struct efx_rep *efx_tc_flower_lookup_efv(struct efx_nic *efx,
256 					 struct net_device *dev);
257 s64 efx_tc_flower_external_mport(struct efx_nic *efx, struct efx_rep *efv);
258 int efx_tc_configure_default_rule_rep(struct efx_rep *efv);
259 void efx_tc_deconfigure_default_rule(struct efx_nic *efx,
260 				     struct efx_tc_flow_rule *rule);
261 int efx_tc_flower(struct efx_nic *efx, struct net_device *net_dev,
262 		  struct flow_cls_offload *tc, struct efx_rep *efv);
263 
264 int efx_tc_insert_rep_filters(struct efx_nic *efx);
265 void efx_tc_remove_rep_filters(struct efx_nic *efx);
266 
267 int efx_init_tc(struct efx_nic *efx);
268 void efx_fini_tc(struct efx_nic *efx);
269 
270 int efx_init_struct_tc(struct efx_nic *efx);
271 void efx_fini_struct_tc(struct efx_nic *efx);
272 
273 #endif /* EFX_TC_H */
274