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