tc.h 11 KB

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