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.
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.
14 #include <net/flow_offload.h>
15 #include <linux/rhashtable.h>
16 #include "net_driver.h"
17 #include "tc_counters.h"
19 #define IS_ALL_ONES(v) (!(typeof (v))~(v))
22 * struct efx_tc_mac_pedit_action - mac pedit action fields
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
29 * MAC address edits are indirected through a table in the hardware
31 struct efx_tc_mac_pedit_action
{
33 struct rhash_head linkage
;
35 u32 fw_id
; /* index of this entry in firmware MAC address table */
38 static inline bool efx_ipv6_addr_all_ones(struct in6_addr
*addr
)
40 return !memchr_inv(addr
, 0xff, sizeof(*addr
));
43 struct efx_tc_encap_action
; /* see tc_encap_actions.h */
46 * struct efx_tc_action_set - collection of tc action fields
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_nat: perform NAT/NPT with values returned by conntrack match
52 * @do_ttl_dec: used to indicate IP TTL / Hop Limit should be decremented
53 * @deliver: used to indicate a deliver action should take place
54 * @vlan_tci: tci fields for vlan push actions
55 * @vlan_proto: ethernet types for vlan push actions
56 * @count: counter mapping
57 * @encap_md: encap entry in tc_encap_ht table
58 * @encap_user: linked list of encap users (encap_md->users)
59 * @user: owning action-set-list. Only populated if @encap_md is; used by efx_tc_update_encap() fallback handling
60 * @count_user: linked list of counter users (counter->users)
61 * @dest_mport: destination mport
62 * @src_mac: source mac entry in tc_mac_ht table
63 * @dst_mac: destination mac entry in tc_mac_ht table
64 * @fw_id: index of this entry in firmware actions table
65 * @list: linked list of tc actions
68 struct efx_tc_action_set
{
77 struct efx_tc_counter_index
*count
;
78 struct efx_tc_encap_action
*encap_md
;
79 struct list_head encap_user
;
80 struct efx_tc_action_set_list
*user
;
81 struct list_head count_user
;
83 struct efx_tc_mac_pedit_action
*src_mac
;
84 struct efx_tc_mac_pedit_action
*dst_mac
;
86 struct list_head list
;
89 struct efx_tc_match_fields
{
92 u8 recirc_id
; /* mapped from (u32) TC chain_index to smaller space */
93 /* L2 (inner when encap) */
95 __be16 vlan_tci
[2], vlan_proto
[2];
96 u8 eth_saddr
[ETH_ALEN
], eth_daddr
[ETH_ALEN
];
98 u8 ip_proto
, ip_tos
, ip_ttl
;
99 __be32 src_ip
, dst_ip
;
101 struct in6_addr src_ip6
, dst_ip6
;
103 bool ip_frag
, ip_firstfrag
;
105 __be16 l4_sport
, l4_dport
; /* Ports (UDP, TCP) */
107 bool tcp_syn_fin_rst
; /* true if ANY of SYN/FIN/RST are set */
108 /* Encap. The following are *outer* fields. Note that there are no
109 * outer eth (L2) fields; this is because TC doesn't have them.
111 __be32 enc_src_ip
, enc_dst_ip
;
112 struct in6_addr enc_src_ip6
, enc_dst_ip6
;
113 u8 enc_ip_tos
, enc_ip_ttl
;
114 __be16 enc_sport
, enc_dport
;
115 __be32 enc_keyid
; /* e.g. VNI, VSID */
117 u16 ct_state_trk
:1, ct_state_est
:1;
122 static inline bool efx_tc_match_is_encap(const struct efx_tc_match_fields
*mask
)
124 return mask
->enc_src_ip
|| mask
->enc_dst_ip
||
125 !ipv6_addr_any(&mask
->enc_src_ip6
) ||
126 !ipv6_addr_any(&mask
->enc_dst_ip6
) || mask
->enc_ip_tos
||
127 mask
->enc_ip_ttl
|| mask
->enc_sport
|| mask
->enc_dport
;
131 * enum efx_tc_em_pseudo_type - &struct efx_tc_encap_match pseudo type
133 * These are used to classify "pseudo" encap matches, which don't refer
134 * to an entry in hardware but rather indicate that a section of the
135 * match space is in use by another Outer Rule.
137 * @EFX_TC_EM_DIRECT: real HW entry in Outer Rule table; not a pseudo.
138 * Hardware index in &struct efx_tc_encap_match.fw_id is valid.
139 * @EFX_TC_EM_PSEUDO_MASK: registered by an encap match which includes a
140 * match on an optional field (currently ip_tos and/or udp_sport),
141 * to prevent an overlapping encap match _without_ optional fields.
142 * The pseudo encap match may be referenced again by an encap match
143 * with different values for these fields, but all masks must match the
144 * first (stored in our child_* fields).
145 * @EFX_TC_EM_PSEUDO_OR: registered by an fLHS rule that fits in the OR
146 * table. The &struct efx_tc_lhs_rule already holds the HW OR entry.
147 * Only one reference to this encap match may exist.
149 enum efx_tc_em_pseudo_type
{
151 EFX_TC_EM_PSEUDO_MASK
,
155 struct efx_tc_encap_match
{
156 __be32 src_ip
, dst_ip
;
157 struct in6_addr src_ip6
, dst_ip6
;
159 __be16 udp_sport
, udp_sport_mask
;
160 u8 ip_tos
, ip_tos_mask
;
161 struct rhash_head linkage
;
162 enum efx_encap_type tun_type
;
163 u8 child_ip_tos_mask
;
164 __be16 child_udp_sport_mask
;
166 enum efx_tc_em_pseudo_type type
;
167 u32 fw_id
; /* index of this entry in firmware encap match table */
168 struct efx_tc_encap_match
*pseudo
; /* Referenced pseudo EM if needed */
171 struct efx_tc_recirc_id
{
173 struct net_device
*net_dev
;
174 struct rhash_head linkage
;
176 u8 fw_id
; /* index allocated for use in the MAE */
179 struct efx_tc_match
{
180 struct efx_tc_match_fields value
;
181 struct efx_tc_match_fields mask
;
182 struct efx_tc_encap_match
*encap
;
183 struct efx_tc_recirc_id
*rid
;
186 struct efx_tc_action_set_list
{
187 struct list_head list
;
191 struct efx_tc_lhs_action
{
192 enum efx_encap_type tun_type
;
193 struct efx_tc_recirc_id
*rid
;
194 struct efx_tc_ct_zone
*zone
;
195 struct efx_tc_counter_index
*count
;
198 struct efx_tc_flow_rule
{
199 unsigned long cookie
;
200 struct rhash_head linkage
;
201 struct efx_tc_match match
;
202 struct efx_tc_action_set_list acts
;
203 struct efx_tc_action_set_list
*fallback
; /* what to use when unready? */
207 struct efx_tc_lhs_rule
{
208 unsigned long cookie
;
209 struct efx_tc_match match
;
210 struct efx_tc_lhs_action lhs_act
;
211 struct rhash_head linkage
;
213 bool is_ar
; /* Action Rule (for OR-AR-CT-AR sequence) */
216 enum efx_tc_rule_prios
{
217 EFX_TC_PRIO_TC
, /* Rule inserted by TC */
218 EFX_TC_PRIO_DFLT
, /* Default switch rule; one of efx_tc_default_rules */
222 struct efx_tc_table_field_fmt
{
230 struct efx_tc_table_desc
{
239 struct efx_tc_table_field_fmt
*keys
;
240 struct efx_tc_table_field_fmt
*resps
;
243 struct efx_tc_table_ct
{ /* TABLE_ID_CONNTRACK_TABLE */
244 struct efx_tc_table_desc desc
;
246 struct { /* indices of named fields within @desc.keys */
249 u8 src_ip_idx
; /* either v4 or v6 */
253 u8 zone_idx
; /* for TABLE_FIELD_ID_DOMAIN */
255 struct { /* indices of named fields within @desc.resps */
265 * struct efx_tc_state - control plane data for TC offload
267 * @caps: MAE capabilities reported by MCDI
268 * @block_list: List of &struct efx_tc_block_binding
269 * @mutex: Used to serialise operations on TC hashtables
270 * @counter_ht: Hashtable of TC counters (FW IDs and counter values)
271 * @counter_id_ht: Hashtable mapping TC counter cookies to counters
272 * @encap_ht: Hashtable of TC encap actions
273 * @mac_ht: Hashtable of MAC address entries (for pedits)
274 * @encap_match_ht: Hashtable of TC encap matches
275 * @match_action_ht: Hashtable of TC match-action rules
276 * @lhs_rule_ht: Hashtable of TC left-hand (act ct & goto chain) rules
277 * @ct_zone_ht: Hashtable of TC conntrack flowtable bindings
278 * @ct_ht: Hashtable of TC conntrack flow entries
279 * @neigh_ht: Hashtable of neighbour watches (&struct efx_neigh_binder)
280 * @recirc_ht: Hashtable of recirculation ID mappings (&struct efx_tc_recirc_id)
281 * @recirc_ida: Recirculation ID allocator
282 * @meta_ct: MAE table layout for conntrack table
283 * @reps_mport_id: MAE port allocated for representor RX
284 * @reps_filter_uc: VNIC filter for representor unicast RX (promisc)
285 * @reps_filter_mc: VNIC filter for representor multicast RX (allmulti)
286 * @reps_mport_vport_id: vport_id for representor RX filters
287 * @flush_counters: counters have been stopped, waiting for drain
288 * @flush_gen: final generation count per type array as reported by
289 * MC_CMD_MAE_COUNTERS_STREAM_STOP
290 * @seen_gen: most recent generation count per type as seen by efx_tc_rx()
291 * @flush_wq: wait queue used by efx_mae_stop_counters() to wait for
292 * MAE counters RXQ to finish draining
293 * @dflt: Match-action rules for default switching; at priority
294 * %EFX_TC_PRIO_DFLT. Named by *ingress* port
295 * @dflt.pf: rule for traffic ingressing from PF (egresses to wire)
296 * @dflt.wire: rule for traffic ingressing from wire (egresses to PF)
297 * @facts: Fallback action-set-lists for unready rules. Named by *egress* port
298 * @facts.pf: action-set-list for unready rules on PF netdev, hence applying to
299 * traffic from wire, and egressing to PF
300 * @facts.reps: action-set-list for unready rules on representors, hence
301 * applying to traffic from representees, and egressing to the reps mport
302 * @up: have TC datastructures been set up?
304 struct efx_tc_state
{
305 struct mae_caps
*caps
;
306 struct list_head block_list
;
308 struct rhashtable counter_ht
;
309 struct rhashtable counter_id_ht
;
310 struct rhashtable encap_ht
;
311 struct rhashtable mac_ht
;
312 struct rhashtable encap_match_ht
;
313 struct rhashtable match_action_ht
;
314 struct rhashtable lhs_rule_ht
;
315 struct rhashtable ct_zone_ht
;
316 struct rhashtable ct_ht
;
317 struct rhashtable neigh_ht
;
318 struct rhashtable recirc_ht
;
319 struct ida recirc_ida
;
320 struct efx_tc_table_ct meta_ct
;
321 u32 reps_mport_id
, reps_mport_vport_id
;
322 s32 reps_filter_uc
, reps_filter_mc
;
324 u32 flush_gen
[EFX_TC_COUNTER_TYPE_MAX
];
325 u32 seen_gen
[EFX_TC_COUNTER_TYPE_MAX
];
326 wait_queue_head_t flush_wq
;
328 struct efx_tc_flow_rule pf
;
329 struct efx_tc_flow_rule wire
;
332 struct efx_tc_action_set_list pf
;
333 struct efx_tc_action_set_list reps
;
340 enum efx_encap_type
efx_tc_indr_netdev_type(struct net_device
*net_dev
);
341 struct efx_rep
*efx_tc_flower_lookup_efv(struct efx_nic
*efx
,
342 struct net_device
*dev
);
343 s64
efx_tc_flower_external_mport(struct efx_nic
*efx
, struct efx_rep
*efv
);
344 int efx_tc_configure_default_rule_rep(struct efx_rep
*efv
);
345 void efx_tc_deconfigure_default_rule(struct efx_nic
*efx
,
346 struct efx_tc_flow_rule
*rule
);
347 int efx_tc_flower(struct efx_nic
*efx
, struct net_device
*net_dev
,
348 struct flow_cls_offload
*tc
, struct efx_rep
*efv
);
350 int efx_tc_insert_rep_filters(struct efx_nic
*efx
);
351 void efx_tc_remove_rep_filters(struct efx_nic
*efx
);
353 int efx_init_tc(struct efx_nic
*efx
);
354 void efx_fini_tc(struct efx_nic
*efx
);
356 int efx_init_struct_tc(struct efx_nic
*efx
);
357 void efx_fini_struct_tc(struct efx_nic
*efx
);
359 #endif /* EFX_TC_H */