1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
4 #ifndef __NFP_FLOWER_H__
5 #define __NFP_FLOWER_H__ 1
6
7 #include "cmsg.h"
8 #include "../nfp_net.h"
9
10 #include <linux/circ_buf.h>
11 #include <linux/hashtable.h>
12 #include <linux/rhashtable.h>
13 #include <linux/time64.h>
14 #include <linux/types.h>
15 #include <net/pkt_cls.h>
16 #include <net/tcp.h>
17 #include <linux/workqueue.h>
18 #include <linux/idr.h>
19
20 struct nfp_fl_pre_lag;
21 struct net_device;
22 struct nfp_app;
23
24 #define NFP_FL_STAT_ID_MU_NUM GENMASK(31, 22)
25 #define NFP_FL_STAT_ID_STAT GENMASK(21, 0)
26
27 #define NFP_FL_STATS_ELEM_RS FIELD_SIZEOF(struct nfp_fl_stats_id, \
28 init_unalloc)
29 #define NFP_FLOWER_MASK_ENTRY_RS 256
30 #define NFP_FLOWER_MASK_ELEMENT_RS 1
31 #define NFP_FLOWER_MASK_HASH_BITS 10
32
33 #define NFP_FL_META_FLAG_MANAGE_MASK BIT(7)
34
35 #define NFP_FL_MASK_REUSE_TIME_NS 40000
36 #define NFP_FL_MASK_ID_LOCATION 1
37
38 /* Extra features bitmap. */
39 #define NFP_FL_FEATS_GENEVE BIT(0)
40 #define NFP_FL_NBI_MTU_SETTING BIT(1)
41 #define NFP_FL_FEATS_GENEVE_OPT BIT(2)
42 #define NFP_FL_FEATS_VLAN_PCP BIT(3)
43 #define NFP_FL_FEATS_VF_RLIM BIT(4)
44 #define NFP_FL_FEATS_FLOW_MOD BIT(5)
45 #define NFP_FL_FEATS_PRE_TUN_RULES BIT(6)
46 #define NFP_FL_FEATS_FLOW_MERGE BIT(30)
47 #define NFP_FL_FEATS_LAG BIT(31)
48
49 struct nfp_fl_mask_id {
50 struct circ_buf mask_id_free_list;
51 ktime_t *last_used;
52 u8 init_unallocated;
53 };
54
55 struct nfp_fl_stats_id {
56 struct circ_buf free_list;
57 u32 init_unalloc;
58 u8 repeated_em_count;
59 };
60
61 /**
62 * struct nfp_fl_tunnel_offloads - priv data for tunnel offloads
63 * @offloaded_macs: Hashtable of the offloaded MAC addresses
64 * @ipv4_off_list: List of IPv4 addresses to offload
65 * @neigh_off_list: List of neighbour offloads
66 * @ipv4_off_lock: Lock for the IPv4 address list
67 * @neigh_off_lock: Lock for the neighbour address list
68 * @mac_off_ids: IDA to manage id assignment for offloaded MACs
69 * @neigh_nb: Notifier to monitor neighbour state
70 */
71 struct nfp_fl_tunnel_offloads {
72 struct rhashtable offloaded_macs;
73 struct list_head ipv4_off_list;
74 struct list_head neigh_off_list;
75 struct mutex ipv4_off_lock;
76 spinlock_t neigh_off_lock;
77 struct ida mac_off_ids;
78 struct notifier_block neigh_nb;
79 };
80
81 /**
82 * struct nfp_mtu_conf - manage MTU setting
83 * @portnum: NFP port number of repr with requested MTU change
84 * @requested_val: MTU value requested for repr
85 * @ack: Received ack that MTU has been correctly set
86 * @wait_q: Wait queue for MTU acknowledgements
87 * @lock: Lock for setting/reading MTU variables
88 */
89 struct nfp_mtu_conf {
90 u32 portnum;
91 unsigned int requested_val;
92 bool ack;
93 wait_queue_head_t wait_q;
94 spinlock_t lock;
95 };
96
97 /**
98 * struct nfp_fl_lag - Flower APP priv data for link aggregation
99 * @work: Work queue for writing configs to the HW
100 * @lock: Lock to protect lag_group_list
101 * @group_list: List of all master/slave groups offloaded
102 * @ida_handle: IDA to handle group ids
103 * @pkt_num: Incremented for each config packet sent
104 * @batch_ver: Incremented for each batch of config packets
105 * @global_inst: Instance allocator for groups
106 * @rst_cfg: Marker to reset HW LAG config
107 * @retrans_skbs: Cmsgs that could not be processed by HW and require
108 * retransmission
109 */
110 struct nfp_fl_lag {
111 struct delayed_work work;
112 struct mutex lock;
113 struct list_head group_list;
114 struct ida ida_handle;
115 unsigned int pkt_num;
116 unsigned int batch_ver;
117 u8 global_inst;
118 bool rst_cfg;
119 struct sk_buff_head retrans_skbs;
120 };
121
122 /**
123 * struct nfp_fl_internal_ports - Flower APP priv data for additional ports
124 * @port_ids: Assignment of ids to any additional ports
125 * @lock: Lock for extra ports list
126 */
127 struct nfp_fl_internal_ports {
128 struct idr port_ids;
129 spinlock_t lock;
130 };
131
132 /**
133 * struct nfp_flower_priv - Flower APP per-vNIC priv data
134 * @app: Back pointer to app
135 * @nn: Pointer to vNIC
136 * @mask_id_seed: Seed used for mask hash table
137 * @flower_version: HW version of flower
138 * @flower_ext_feats: Bitmap of extra features the HW supports
139 * @stats_ids: List of free stats ids
140 * @mask_ids: List of free mask ids
141 * @mask_table: Hash table used to store masks
142 * @stats_ring_size: Maximum number of allowed stats ids
143 * @flow_table: Hash table used to store flower rules
144 * @stats: Stored stats updates for flower rules
145 * @stats_lock: Lock for flower rule stats updates
146 * @stats_ctx_table: Hash table to map stats contexts to its flow rule
147 * @cmsg_work: Workqueue for control messages processing
148 * @cmsg_skbs_high: List of higher priority skbs for control message
149 * processing
150 * @cmsg_skbs_low: List of lower priority skbs for control message
151 * processing
152 * @tun: Tunnel offload data
153 * @reify_replies: atomically stores the number of replies received
154 * from firmware for repr reify
155 * @reify_wait_queue: wait queue for repr reify response counting
156 * @mtu_conf: Configuration of repr MTU value
157 * @nfp_lag: Link aggregation data block
158 * @indr_block_cb_priv: List of priv data passed to indirect block cbs
159 * @non_repr_priv: List of offloaded non-repr ports and their priv data
160 * @active_mem_unit: Current active memory unit for flower rules
161 * @total_mem_units: Total number of available memory units for flower rules
162 * @internal_ports: Internal port ids used in offloaded rules
163 * @qos_stats_work: Workqueue for qos stats processing
164 * @qos_rate_limiters: Current active qos rate limiters
165 * @qos_stats_lock: Lock on qos stats updates
166 * @pre_tun_rule_cnt: Number of pre-tunnel rules offloaded
167 */
168 struct nfp_flower_priv {
169 struct nfp_app *app;
170 struct nfp_net *nn;
171 u32 mask_id_seed;
172 u64 flower_version;
173 u64 flower_ext_feats;
174 struct nfp_fl_stats_id stats_ids;
175 struct nfp_fl_mask_id mask_ids;
176 DECLARE_HASHTABLE(mask_table, NFP_FLOWER_MASK_HASH_BITS);
177 u32 stats_ring_size;
178 struct rhashtable flow_table;
179 struct nfp_fl_stats *stats;
180 spinlock_t stats_lock; /* lock stats */
181 struct rhashtable stats_ctx_table;
182 struct work_struct cmsg_work;
183 struct sk_buff_head cmsg_skbs_high;
184 struct sk_buff_head cmsg_skbs_low;
185 struct nfp_fl_tunnel_offloads tun;
186 atomic_t reify_replies;
187 wait_queue_head_t reify_wait_queue;
188 struct nfp_mtu_conf mtu_conf;
189 struct nfp_fl_lag nfp_lag;
190 struct list_head indr_block_cb_priv;
191 struct list_head non_repr_priv;
192 unsigned int active_mem_unit;
193 unsigned int total_mem_units;
194 struct nfp_fl_internal_ports internal_ports;
195 struct delayed_work qos_stats_work;
196 unsigned int qos_rate_limiters;
197 spinlock_t qos_stats_lock; /* Protect the qos stats */
198 int pre_tun_rule_cnt;
199 };
200
201 /**
202 * struct nfp_fl_qos - Flower APP priv data for quality of service
203 * @netdev_port_id: NFP port number of repr with qos info
204 * @curr_stats: Currently stored stats updates for qos info
205 * @prev_stats: Previously stored updates for qos info
206 * @last_update: Stored time when last stats were updated
207 */
208 struct nfp_fl_qos {
209 u32 netdev_port_id;
210 struct nfp_stat_pair curr_stats;
211 struct nfp_stat_pair prev_stats;
212 u64 last_update;
213 };
214
215 /**
216 * struct nfp_flower_repr_priv - Flower APP per-repr priv data
217 * @nfp_repr: Back pointer to nfp_repr
218 * @lag_port_flags: Extended port flags to record lag state of repr
219 * @mac_offloaded: Flag indicating a MAC address is offloaded for repr
220 * @offloaded_mac_addr: MAC address that has been offloaded for repr
221 * @block_shared: Flag indicating if offload applies to shared blocks
222 * @mac_list: List entry of reprs that share the same offloaded MAC
223 * @qos_table: Stored info on filters implementing qos
224 * @on_bridge: Indicates if the repr is attached to a bridge
225 */
226 struct nfp_flower_repr_priv {
227 struct nfp_repr *nfp_repr;
228 unsigned long lag_port_flags;
229 bool mac_offloaded;
230 u8 offloaded_mac_addr[ETH_ALEN];
231 bool block_shared;
232 struct list_head mac_list;
233 struct nfp_fl_qos qos_table;
234 bool on_bridge;
235 };
236
237 /**
238 * struct nfp_flower_non_repr_priv - Priv data for non-repr offloaded ports
239 * @list: List entry of offloaded reprs
240 * @netdev: Pointer to non-repr net_device
241 * @ref_count: Number of references held for this priv data
242 * @mac_offloaded: Flag indicating a MAC address is offloaded for device
243 * @offloaded_mac_addr: MAC address that has been offloaded for dev
244 */
245 struct nfp_flower_non_repr_priv {
246 struct list_head list;
247 struct net_device *netdev;
248 int ref_count;
249 bool mac_offloaded;
250 u8 offloaded_mac_addr[ETH_ALEN];
251 };
252
253 struct nfp_fl_key_ls {
254 u32 key_layer_two;
255 u8 key_layer;
256 int key_size;
257 };
258
259 struct nfp_fl_rule_metadata {
260 u8 key_len;
261 u8 mask_len;
262 u8 act_len;
263 u8 flags;
264 __be32 host_ctx_id;
265 __be64 host_cookie __packed;
266 __be64 flow_version __packed;
267 __be32 shortcut;
268 };
269
270 struct nfp_fl_stats {
271 u64 pkts;
272 u64 bytes;
273 u64 used;
274 };
275
276 struct nfp_fl_payload {
277 struct nfp_fl_rule_metadata meta;
278 unsigned long tc_flower_cookie;
279 struct rhash_head fl_node;
280 struct rcu_head rcu;
281 __be32 nfp_tun_ipv4_addr;
282 struct net_device *ingress_dev;
283 char *unmasked_data;
284 char *mask_data;
285 char *action_data;
286 struct list_head linked_flows;
287 bool in_hw;
288 struct {
289 struct net_device *dev;
290 __be16 vlan_tci;
291 __be16 port_idx;
292 } pre_tun_rule;
293 };
294
295 struct nfp_fl_payload_link {
296 /* A link contains a pointer to a merge flow and an associated sub_flow.
297 * Each merge flow will feature in 2 links to its underlying sub_flows.
298 * A sub_flow will have at least 1 link to a merge flow or more if it
299 * has been used to create multiple merge flows.
300 *
301 * For a merge flow, 'linked_flows' in its nfp_fl_payload struct lists
302 * all links to sub_flows (sub_flow.flow) via merge.list.
303 * For a sub_flow, 'linked_flows' gives all links to merge flows it has
304 * formed (merge_flow.flow) via sub_flow.list.
305 */
306 struct {
307 struct list_head list;
308 struct nfp_fl_payload *flow;
309 } merge_flow, sub_flow;
310 };
311
312 extern const struct rhashtable_params nfp_flower_table_params;
313
314 struct nfp_fl_stats_frame {
315 __be32 stats_con_id;
316 __be32 pkt_count;
317 __be64 byte_count;
318 __be64 stats_cookie;
319 };
320
321 static inline bool
nfp_flower_internal_port_can_offload(struct nfp_app * app,struct net_device * netdev)322 nfp_flower_internal_port_can_offload(struct nfp_app *app,
323 struct net_device *netdev)
324 {
325 struct nfp_flower_priv *app_priv = app->priv;
326
327 if (!(app_priv->flower_ext_feats & NFP_FL_FEATS_FLOW_MERGE))
328 return false;
329 if (!netdev->rtnl_link_ops)
330 return false;
331 if (!strcmp(netdev->rtnl_link_ops->kind, "openvswitch"))
332 return true;
333
334 return false;
335 }
336
337 /* The address of the merged flow acts as its cookie.
338 * Cookies supplied to us by TC flower are also addresses to allocated
339 * memory and thus this scheme should not generate any collisions.
340 */
nfp_flower_is_merge_flow(struct nfp_fl_payload * flow_pay)341 static inline bool nfp_flower_is_merge_flow(struct nfp_fl_payload *flow_pay)
342 {
343 return flow_pay->tc_flower_cookie == (unsigned long)flow_pay;
344 }
345
nfp_flower_is_supported_bridge(struct net_device * netdev)346 static inline bool nfp_flower_is_supported_bridge(struct net_device *netdev)
347 {
348 return netif_is_ovs_master(netdev);
349 }
350
351 int nfp_flower_metadata_init(struct nfp_app *app, u64 host_ctx_count,
352 unsigned int host_ctx_split);
353 void nfp_flower_metadata_cleanup(struct nfp_app *app);
354
355 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
356 enum tc_setup_type type, void *type_data);
357 int nfp_flower_merge_offloaded_flows(struct nfp_app *app,
358 struct nfp_fl_payload *sub_flow1,
359 struct nfp_fl_payload *sub_flow2);
360 int nfp_flower_compile_flow_match(struct nfp_app *app,
361 struct flow_cls_offload *flow,
362 struct nfp_fl_key_ls *key_ls,
363 struct net_device *netdev,
364 struct nfp_fl_payload *nfp_flow,
365 enum nfp_flower_tun_type tun_type,
366 struct netlink_ext_ack *extack);
367 int nfp_flower_compile_action(struct nfp_app *app,
368 struct flow_cls_offload *flow,
369 struct net_device *netdev,
370 struct nfp_fl_payload *nfp_flow,
371 struct netlink_ext_ack *extack);
372 int nfp_compile_flow_metadata(struct nfp_app *app,
373 struct flow_cls_offload *flow,
374 struct nfp_fl_payload *nfp_flow,
375 struct net_device *netdev,
376 struct netlink_ext_ack *extack);
377 void __nfp_modify_flow_metadata(struct nfp_flower_priv *priv,
378 struct nfp_fl_payload *nfp_flow);
379 int nfp_modify_flow_metadata(struct nfp_app *app,
380 struct nfp_fl_payload *nfp_flow);
381
382 struct nfp_fl_payload *
383 nfp_flower_search_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie,
384 struct net_device *netdev);
385 struct nfp_fl_payload *
386 nfp_flower_get_fl_payload_from_ctx(struct nfp_app *app, u32 ctx_id);
387 struct nfp_fl_payload *
388 nfp_flower_remove_fl_table(struct nfp_app *app, unsigned long tc_flower_cookie);
389
390 void nfp_flower_rx_flow_stats(struct nfp_app *app, struct sk_buff *skb);
391
392 int nfp_tunnel_config_start(struct nfp_app *app);
393 void nfp_tunnel_config_stop(struct nfp_app *app);
394 int nfp_tunnel_mac_event_handler(struct nfp_app *app,
395 struct net_device *netdev,
396 unsigned long event, void *ptr);
397 void nfp_tunnel_del_ipv4_off(struct nfp_app *app, __be32 ipv4);
398 void nfp_tunnel_add_ipv4_off(struct nfp_app *app, __be32 ipv4);
399 void nfp_tunnel_request_route(struct nfp_app *app, struct sk_buff *skb);
400 void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb);
401 void nfp_flower_lag_init(struct nfp_fl_lag *lag);
402 void nfp_flower_lag_cleanup(struct nfp_fl_lag *lag);
403 int nfp_flower_lag_reset(struct nfp_fl_lag *lag);
404 int nfp_flower_lag_netdev_event(struct nfp_flower_priv *priv,
405 struct net_device *netdev,
406 unsigned long event, void *ptr);
407 bool nfp_flower_lag_unprocessed_msg(struct nfp_app *app, struct sk_buff *skb);
408 int nfp_flower_lag_populate_pre_action(struct nfp_app *app,
409 struct net_device *master,
410 struct nfp_fl_pre_lag *pre_act,
411 struct netlink_ext_ack *extack);
412 int nfp_flower_lag_get_output_id(struct nfp_app *app,
413 struct net_device *master);
414 void nfp_flower_qos_init(struct nfp_app *app);
415 void nfp_flower_qos_cleanup(struct nfp_app *app);
416 int nfp_flower_setup_qos_offload(struct nfp_app *app, struct net_device *netdev,
417 struct tc_cls_matchall_offload *flow);
418 void nfp_flower_stats_rlim_reply(struct nfp_app *app, struct sk_buff *skb);
419 int nfp_flower_reg_indir_block_handler(struct nfp_app *app,
420 struct net_device *netdev,
421 unsigned long event);
422
423 void
424 __nfp_flower_non_repr_priv_get(struct nfp_flower_non_repr_priv *non_repr_priv);
425 struct nfp_flower_non_repr_priv *
426 nfp_flower_non_repr_priv_get(struct nfp_app *app, struct net_device *netdev);
427 void
428 __nfp_flower_non_repr_priv_put(struct nfp_flower_non_repr_priv *non_repr_priv);
429 void
430 nfp_flower_non_repr_priv_put(struct nfp_app *app, struct net_device *netdev);
431 u32 nfp_flower_get_port_id_from_netdev(struct nfp_app *app,
432 struct net_device *netdev);
433 int nfp_flower_xmit_pre_tun_flow(struct nfp_app *app,
434 struct nfp_fl_payload *flow);
435 int nfp_flower_xmit_pre_tun_del_flow(struct nfp_app *app,
436 struct nfp_fl_payload *flow);
437 #endif
438