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
efx_ipv6_addr_all_ones(struct in6_addr * addr)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
efx_tc_match_is_encap(const struct efx_tc_match_fields * mask)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 */
144 enum efx_tc_em_pseudo_type {
145 EFX_TC_EM_DIRECT,
146 EFX_TC_EM_PSEUDO_MASK,
147 };
148
149 struct efx_tc_encap_match {
150 __be32 src_ip, dst_ip;
151 struct in6_addr src_ip6, dst_ip6;
152 __be16 udp_dport;
153 __be16 udp_sport, udp_sport_mask;
154 u8 ip_tos, ip_tos_mask;
155 struct rhash_head linkage;
156 enum efx_encap_type tun_type;
157 u8 child_ip_tos_mask;
158 __be16 child_udp_sport_mask;
159 refcount_t ref;
160 enum efx_tc_em_pseudo_type type;
161 u32 fw_id; /* index of this entry in firmware encap match table */
162 struct efx_tc_encap_match *pseudo; /* Referenced pseudo EM if needed */
163 };
164
165 struct efx_tc_recirc_id {
166 u32 chain_index;
167 struct net_device *net_dev;
168 struct rhash_head linkage;
169 refcount_t ref;
170 u8 fw_id; /* index allocated for use in the MAE */
171 };
172
173 struct efx_tc_match {
174 struct efx_tc_match_fields value;
175 struct efx_tc_match_fields mask;
176 struct efx_tc_encap_match *encap;
177 struct efx_tc_recirc_id *rid;
178 };
179
180 struct efx_tc_action_set_list {
181 struct list_head list;
182 u32 fw_id;
183 };
184
185 struct efx_tc_lhs_action {
186 struct efx_tc_recirc_id *rid;
187 struct efx_tc_ct_zone *zone;
188 struct efx_tc_counter_index *count;
189 };
190
191 struct efx_tc_flow_rule {
192 unsigned long cookie;
193 struct rhash_head linkage;
194 struct efx_tc_match match;
195 struct efx_tc_action_set_list acts;
196 struct efx_tc_action_set_list *fallback; /* what to use when unready? */
197 u32 fw_id;
198 };
199
200 struct efx_tc_lhs_rule {
201 unsigned long cookie;
202 struct efx_tc_match match;
203 struct efx_tc_lhs_action lhs_act;
204 struct rhash_head linkage;
205 u32 fw_id;
206 };
207
208 enum efx_tc_rule_prios {
209 EFX_TC_PRIO_TC, /* Rule inserted by TC */
210 EFX_TC_PRIO_DFLT, /* Default switch rule; one of efx_tc_default_rules */
211 EFX_TC_PRIO__NUM
212 };
213
214 struct efx_tc_table_field_fmt {
215 u16 field_id;
216 u16 lbn;
217 u16 width;
218 u8 masking;
219 u8 scheme;
220 };
221
222 struct efx_tc_table_desc {
223 u16 type;
224 u16 key_width;
225 u16 resp_width;
226 u16 n_keys;
227 u16 n_resps;
228 u16 n_prios;
229 u8 flags;
230 u8 scheme;
231 struct efx_tc_table_field_fmt *keys;
232 struct efx_tc_table_field_fmt *resps;
233 };
234
235 struct efx_tc_table_ct { /* TABLE_ID_CONNTRACK_TABLE */
236 struct efx_tc_table_desc desc;
237 bool hooked;
238 struct { /* indices of named fields within @desc.keys */
239 u8 eth_proto_idx;
240 u8 ip_proto_idx;
241 u8 src_ip_idx; /* either v4 or v6 */
242 u8 dst_ip_idx;
243 u8 l4_sport_idx;
244 u8 l4_dport_idx;
245 u8 zone_idx; /* for TABLE_FIELD_ID_DOMAIN */
246 } keys;
247 struct { /* indices of named fields within @desc.resps */
248 u8 dnat_idx;
249 u8 nat_ip_idx;
250 u8 l4_natport_idx;
251 u8 mark_idx;
252 u8 counter_id_idx;
253 } resps;
254 };
255
256 /**
257 * struct efx_tc_state - control plane data for TC offload
258 *
259 * @caps: MAE capabilities reported by MCDI
260 * @block_list: List of &struct efx_tc_block_binding
261 * @mutex: Used to serialise operations on TC hashtables
262 * @counter_ht: Hashtable of TC counters (FW IDs and counter values)
263 * @counter_id_ht: Hashtable mapping TC counter cookies to counters
264 * @encap_ht: Hashtable of TC encap actions
265 * @mac_ht: Hashtable of MAC address entries (for pedits)
266 * @encap_match_ht: Hashtable of TC encap matches
267 * @match_action_ht: Hashtable of TC match-action rules
268 * @lhs_rule_ht: Hashtable of TC left-hand (act ct & goto chain) rules
269 * @ct_zone_ht: Hashtable of TC conntrack flowtable bindings
270 * @ct_ht: Hashtable of TC conntrack flow entries
271 * @neigh_ht: Hashtable of neighbour watches (&struct efx_neigh_binder)
272 * @recirc_ht: Hashtable of recirculation ID mappings (&struct efx_tc_recirc_id)
273 * @recirc_ida: Recirculation ID allocator
274 * @meta_ct: MAE table layout for conntrack table
275 * @reps_mport_id: MAE port allocated for representor RX
276 * @reps_filter_uc: VNIC filter for representor unicast RX (promisc)
277 * @reps_filter_mc: VNIC filter for representor multicast RX (allmulti)
278 * @reps_mport_vport_id: vport_id for representor RX filters
279 * @flush_counters: counters have been stopped, waiting for drain
280 * @flush_gen: final generation count per type array as reported by
281 * MC_CMD_MAE_COUNTERS_STREAM_STOP
282 * @seen_gen: most recent generation count per type as seen by efx_tc_rx()
283 * @flush_wq: wait queue used by efx_mae_stop_counters() to wait for
284 * MAE counters RXQ to finish draining
285 * @dflt: Match-action rules for default switching; at priority
286 * %EFX_TC_PRIO_DFLT. Named by *ingress* port
287 * @dflt.pf: rule for traffic ingressing from PF (egresses to wire)
288 * @dflt.wire: rule for traffic ingressing from wire (egresses to PF)
289 * @facts: Fallback action-set-lists for unready rules. Named by *egress* port
290 * @facts.pf: action-set-list for unready rules on PF netdev, hence applying to
291 * traffic from wire, and egressing to PF
292 * @facts.reps: action-set-list for unready rules on representors, hence
293 * applying to traffic from representees, and egressing to the reps mport
294 * @up: have TC datastructures been set up?
295 */
296 struct efx_tc_state {
297 struct mae_caps *caps;
298 struct list_head block_list;
299 struct mutex mutex;
300 struct rhashtable counter_ht;
301 struct rhashtable counter_id_ht;
302 struct rhashtable encap_ht;
303 struct rhashtable mac_ht;
304 struct rhashtable encap_match_ht;
305 struct rhashtable match_action_ht;
306 struct rhashtable lhs_rule_ht;
307 struct rhashtable ct_zone_ht;
308 struct rhashtable ct_ht;
309 struct rhashtable neigh_ht;
310 struct rhashtable recirc_ht;
311 struct ida recirc_ida;
312 struct efx_tc_table_ct meta_ct;
313 u32 reps_mport_id, reps_mport_vport_id;
314 s32 reps_filter_uc, reps_filter_mc;
315 bool flush_counters;
316 u32 flush_gen[EFX_TC_COUNTER_TYPE_MAX];
317 u32 seen_gen[EFX_TC_COUNTER_TYPE_MAX];
318 wait_queue_head_t flush_wq;
319 struct {
320 struct efx_tc_flow_rule pf;
321 struct efx_tc_flow_rule wire;
322 } dflt;
323 struct {
324 struct efx_tc_action_set_list pf;
325 struct efx_tc_action_set_list reps;
326 } facts;
327 bool up;
328 };
329
330 struct efx_rep;
331
332 enum efx_encap_type efx_tc_indr_netdev_type(struct net_device *net_dev);
333 struct efx_rep *efx_tc_flower_lookup_efv(struct efx_nic *efx,
334 struct net_device *dev);
335 s64 efx_tc_flower_external_mport(struct efx_nic *efx, struct efx_rep *efv);
336 int efx_tc_configure_default_rule_rep(struct efx_rep *efv);
337 void efx_tc_deconfigure_default_rule(struct efx_nic *efx,
338 struct efx_tc_flow_rule *rule);
339 int efx_tc_flower(struct efx_nic *efx, struct net_device *net_dev,
340 struct flow_cls_offload *tc, struct efx_rep *efv);
341
342 int efx_tc_insert_rep_filters(struct efx_nic *efx);
343 void efx_tc_remove_rep_filters(struct efx_nic *efx);
344
345 int efx_init_tc(struct efx_nic *efx);
346 void efx_fini_tc(struct efx_nic *efx);
347
348 int efx_init_struct_tc(struct efx_nic *efx);
349 void efx_fini_struct_tc(struct efx_nic *efx);
350
351 #endif /* EFX_TC_H */
352