2 * include/net/devlink.h - Network physical device Netlink interface
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 #ifndef _NET_DEVLINK_H_
12 #define _NET_DEVLINK_H_
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/gfp.h>
17 #include <linux/list.h>
18 #include <linux/netdevice.h>
19 #include <net/net_namespace.h>
20 #include <uapi/linux/devlink.h>
25 struct list_head list
;
26 struct list_head port_list
;
27 struct list_head sb_list
;
28 struct list_head dpipe_table_list
;
29 struct list_head resource_list
;
30 struct devlink_dpipe_headers
*dpipe_headers
;
31 const struct devlink_ops
*ops
;
35 char priv
[0] __aligned(NETDEV_ALIGN
);
39 struct list_head list
;
40 struct devlink
*devlink
;
43 enum devlink_port_type type
;
44 enum devlink_port_type desired_type
;
50 struct devlink_sb_pool_info
{
51 enum devlink_sb_pool_type pool_type
;
53 enum devlink_sb_threshold_type threshold_type
;
57 * struct devlink_dpipe_field - dpipe field object
59 * @id: index inside the headers field array
61 * @mapping_type: mapping type
63 struct devlink_dpipe_field
{
66 unsigned int bitwidth
;
67 enum devlink_dpipe_field_mapping_type mapping_type
;
71 * struct devlink_dpipe_header - dpipe header object
73 * @id: index, global/local detrmined by global bit
75 * @fields_count: number of fields
76 * @global: indicates if header is shared like most protocol header
79 struct devlink_dpipe_header
{
82 struct devlink_dpipe_field
*fields
;
83 unsigned int fields_count
;
88 * struct devlink_dpipe_match - represents match operation
89 * @type: type of match
90 * @header_index: header index (packets can have several headers of same
91 * type like in case of tunnels)
93 * @fieled_id: field index
95 struct devlink_dpipe_match
{
96 enum devlink_dpipe_match_type type
;
97 unsigned int header_index
;
98 struct devlink_dpipe_header
*header
;
99 unsigned int field_id
;
103 * struct devlink_dpipe_action - represents action operation
104 * @type: type of action
105 * @header_index: header index (packets can have several headers of same
106 * type like in case of tunnels)
108 * @fieled_id: field index
110 struct devlink_dpipe_action
{
111 enum devlink_dpipe_action_type type
;
112 unsigned int header_index
;
113 struct devlink_dpipe_header
*header
;
114 unsigned int field_id
;
118 * struct devlink_dpipe_value - represents value of match/action
121 * @mapping_value: in case the field has some mapping this value
122 * specified the mapping value
123 * @mapping_valid: specify if mapping value is valid
124 * @value_size: value size
128 struct devlink_dpipe_value
{
130 struct devlink_dpipe_action
*action
;
131 struct devlink_dpipe_match
*match
;
133 unsigned int mapping_value
;
135 unsigned int value_size
;
141 * struct devlink_dpipe_entry - table entry object
142 * @index: index of the entry in the table
143 * @match_values: match values
144 * @matche_values_count: count of matches tuples
145 * @action_values: actions values
146 * @action_values_count: count of actions values
147 * @counter: value of counter
148 * @counter_valid: Specify if value is valid from hardware
150 struct devlink_dpipe_entry
{
152 struct devlink_dpipe_value
*match_values
;
153 unsigned int match_values_count
;
154 struct devlink_dpipe_value
*action_values
;
155 unsigned int action_values_count
;
161 * struct devlink_dpipe_dump_ctx - context provided to driver in order
164 * @cmd: devlink command
166 * @nest: top attribute
169 struct devlink_dpipe_dump_ctx
{
170 struct genl_info
*info
;
171 enum devlink_command cmd
;
177 struct devlink_dpipe_table_ops
;
180 * struct devlink_dpipe_table - table object
183 * @counters_enabled: indicates if counters are active
184 * @counter_control_extern: indicates if counter control is in dpipe or
186 * @resource_valid: Indicate that the resource id is valid
187 * @resource_id: relative resource this table is related to
188 * @resource_units: number of resource's unit consumed per table's entry
189 * @table_ops: table operations
192 struct devlink_dpipe_table
{
194 struct list_head list
;
196 bool counters_enabled
;
197 bool counter_control_extern
;
201 struct devlink_dpipe_table_ops
*table_ops
;
206 * struct devlink_dpipe_table_ops - dpipe_table ops
207 * @actions_dump - dumps all tables actions
208 * @matches_dump - dumps all tables matches
209 * @entries_dump - dumps all active entries in the table
210 * @counters_set_update - when changing the counter status hardware sync
211 * maybe needed to allocate/free counter related
213 * @size_get - get size
215 struct devlink_dpipe_table_ops
{
216 int (*actions_dump
)(void *priv
, struct sk_buff
*skb
);
217 int (*matches_dump
)(void *priv
, struct sk_buff
*skb
);
218 int (*entries_dump
)(void *priv
, bool counters_enabled
,
219 struct devlink_dpipe_dump_ctx
*dump_ctx
);
220 int (*counters_set_update
)(void *priv
, bool enable
);
221 u64 (*size_get
)(void *priv
);
225 * struct devlink_dpipe_headers - dpipe headers
226 * @headers - header array can be shared (global bit) or driver specific
227 * @headers_count - count of headers
229 struct devlink_dpipe_headers
{
230 struct devlink_dpipe_header
**headers
;
231 unsigned int headers_count
;
235 * struct devlink_resource_ops - resource ops
236 * @occ_get: get the occupied size
237 * @size_validate: validate the size of the resource before update, reload
238 * is needed for changes to take place
240 struct devlink_resource_ops
{
241 u64 (*occ_get
)(struct devlink
*devlink
);
242 int (*size_validate
)(struct devlink
*devlink
, u64 size
,
243 struct netlink_ext_ack
*extack
);
247 * struct devlink_resource_size_params - resource's size parameters
248 * @size_min: minimum size which can be set
249 * @size_max: maximum size which can be set
250 * @size_granularity: size granularity
251 * @size_unit: resource's basic unit
253 struct devlink_resource_size_params
{
256 u64 size_granularity
;
257 enum devlink_resource_unit unit
;
261 devlink_resource_size_params_init(struct devlink_resource_size_params
*size_params
,
262 u64 size_min
, u64 size_max
,
263 u64 size_granularity
,
264 enum devlink_resource_unit unit
)
266 size_params
->size_min
= size_min
;
267 size_params
->size_max
= size_max
;
268 size_params
->size_granularity
= size_granularity
;
269 size_params
->unit
= unit
;
273 * struct devlink_resource - devlink resource
274 * @name: name of the resource
275 * @id: id, per devlink instance
276 * @size: size of the resource
277 * @size_new: updated size of the resource, reload is needed
278 * @size_valid: valid in case the total size of the resource is valid
279 * including its children
280 * @parent: parent resource
281 * @size_params: size parameters
283 * @resource_list: list of child resources
284 * @resource_ops: resource ops
286 struct devlink_resource
{
292 struct devlink_resource
*parent
;
293 struct devlink_resource_size_params size_params
;
294 struct list_head list
;
295 struct list_head resource_list
;
296 const struct devlink_resource_ops
*resource_ops
;
299 #define DEVLINK_RESOURCE_ID_PARENT_TOP 0
302 int (*reload
)(struct devlink
*devlink
);
303 int (*port_type_set
)(struct devlink_port
*devlink_port
,
304 enum devlink_port_type port_type
);
305 int (*port_split
)(struct devlink
*devlink
, unsigned int port_index
,
307 int (*port_unsplit
)(struct devlink
*devlink
, unsigned int port_index
);
308 int (*sb_pool_get
)(struct devlink
*devlink
, unsigned int sb_index
,
310 struct devlink_sb_pool_info
*pool_info
);
311 int (*sb_pool_set
)(struct devlink
*devlink
, unsigned int sb_index
,
312 u16 pool_index
, u32 size
,
313 enum devlink_sb_threshold_type threshold_type
);
314 int (*sb_port_pool_get
)(struct devlink_port
*devlink_port
,
315 unsigned int sb_index
, u16 pool_index
,
317 int (*sb_port_pool_set
)(struct devlink_port
*devlink_port
,
318 unsigned int sb_index
, u16 pool_index
,
320 int (*sb_tc_pool_bind_get
)(struct devlink_port
*devlink_port
,
321 unsigned int sb_index
,
323 enum devlink_sb_pool_type pool_type
,
324 u16
*p_pool_index
, u32
*p_threshold
);
325 int (*sb_tc_pool_bind_set
)(struct devlink_port
*devlink_port
,
326 unsigned int sb_index
,
328 enum devlink_sb_pool_type pool_type
,
329 u16 pool_index
, u32 threshold
);
330 int (*sb_occ_snapshot
)(struct devlink
*devlink
,
331 unsigned int sb_index
);
332 int (*sb_occ_max_clear
)(struct devlink
*devlink
,
333 unsigned int sb_index
);
334 int (*sb_occ_port_pool_get
)(struct devlink_port
*devlink_port
,
335 unsigned int sb_index
, u16 pool_index
,
336 u32
*p_cur
, u32
*p_max
);
337 int (*sb_occ_tc_port_bind_get
)(struct devlink_port
*devlink_port
,
338 unsigned int sb_index
,
340 enum devlink_sb_pool_type pool_type
,
341 u32
*p_cur
, u32
*p_max
);
343 int (*eswitch_mode_get
)(struct devlink
*devlink
, u16
*p_mode
);
344 int (*eswitch_mode_set
)(struct devlink
*devlink
, u16 mode
);
345 int (*eswitch_inline_mode_get
)(struct devlink
*devlink
, u8
*p_inline_mode
);
346 int (*eswitch_inline_mode_set
)(struct devlink
*devlink
, u8 inline_mode
);
347 int (*eswitch_encap_mode_get
)(struct devlink
*devlink
, u8
*p_encap_mode
);
348 int (*eswitch_encap_mode_set
)(struct devlink
*devlink
, u8 encap_mode
);
351 static inline void *devlink_priv(struct devlink
*devlink
)
354 return &devlink
->priv
;
357 static inline struct devlink
*priv_to_devlink(void *priv
)
360 return container_of(priv
, struct devlink
, priv
);
365 #if IS_ENABLED(CONFIG_NET_DEVLINK)
367 struct devlink
*devlink_alloc(const struct devlink_ops
*ops
, size_t priv_size
);
368 int devlink_register(struct devlink
*devlink
, struct device
*dev
);
369 void devlink_unregister(struct devlink
*devlink
);
370 void devlink_free(struct devlink
*devlink
);
371 int devlink_port_register(struct devlink
*devlink
,
372 struct devlink_port
*devlink_port
,
373 unsigned int port_index
);
374 void devlink_port_unregister(struct devlink_port
*devlink_port
);
375 void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
376 struct net_device
*netdev
);
377 void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
378 struct ib_device
*ibdev
);
379 void devlink_port_type_clear(struct devlink_port
*devlink_port
);
380 void devlink_port_split_set(struct devlink_port
*devlink_port
,
382 int devlink_sb_register(struct devlink
*devlink
, unsigned int sb_index
,
383 u32 size
, u16 ingress_pools_count
,
384 u16 egress_pools_count
, u16 ingress_tc_count
,
385 u16 egress_tc_count
);
386 void devlink_sb_unregister(struct devlink
*devlink
, unsigned int sb_index
);
387 int devlink_dpipe_table_register(struct devlink
*devlink
,
388 const char *table_name
,
389 struct devlink_dpipe_table_ops
*table_ops
,
390 void *priv
, bool counter_control_extern
);
391 void devlink_dpipe_table_unregister(struct devlink
*devlink
,
392 const char *table_name
);
393 int devlink_dpipe_headers_register(struct devlink
*devlink
,
394 struct devlink_dpipe_headers
*dpipe_headers
);
395 void devlink_dpipe_headers_unregister(struct devlink
*devlink
);
396 bool devlink_dpipe_table_counter_enabled(struct devlink
*devlink
,
397 const char *table_name
);
398 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx
*dump_ctx
);
399 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx
*dump_ctx
,
400 struct devlink_dpipe_entry
*entry
);
401 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx
*dump_ctx
);
402 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry
*entry
);
403 int devlink_dpipe_action_put(struct sk_buff
*skb
,
404 struct devlink_dpipe_action
*action
);
405 int devlink_dpipe_match_put(struct sk_buff
*skb
,
406 struct devlink_dpipe_match
*match
);
407 extern struct devlink_dpipe_header devlink_dpipe_header_ethernet
;
408 extern struct devlink_dpipe_header devlink_dpipe_header_ipv4
;
409 extern struct devlink_dpipe_header devlink_dpipe_header_ipv6
;
411 int devlink_resource_register(struct devlink
*devlink
,
412 const char *resource_name
,
416 u64 parent_resource_id
,
417 const struct devlink_resource_size_params
*size_params
,
418 const struct devlink_resource_ops
*resource_ops
);
419 void devlink_resources_unregister(struct devlink
*devlink
,
420 struct devlink_resource
*resource
);
421 int devlink_resource_size_get(struct devlink
*devlink
,
423 u64
*p_resource_size
);
424 int devlink_dpipe_table_resource_set(struct devlink
*devlink
,
425 const char *table_name
, u64 resource_id
,
430 static inline struct devlink
*devlink_alloc(const struct devlink_ops
*ops
,
433 return kzalloc(sizeof(struct devlink
) + priv_size
, GFP_KERNEL
);
436 static inline int devlink_register(struct devlink
*devlink
, struct device
*dev
)
441 static inline void devlink_unregister(struct devlink
*devlink
)
445 static inline void devlink_free(struct devlink
*devlink
)
450 static inline int devlink_port_register(struct devlink
*devlink
,
451 struct devlink_port
*devlink_port
,
452 unsigned int port_index
)
457 static inline void devlink_port_unregister(struct devlink_port
*devlink_port
)
461 static inline void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
462 struct net_device
*netdev
)
466 static inline void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
467 struct ib_device
*ibdev
)
471 static inline void devlink_port_type_clear(struct devlink_port
*devlink_port
)
475 static inline void devlink_port_split_set(struct devlink_port
*devlink_port
,
480 static inline int devlink_sb_register(struct devlink
*devlink
,
481 unsigned int sb_index
, u32 size
,
482 u16 ingress_pools_count
,
483 u16 egress_pools_count
,
484 u16 ingress_tc_count
,
490 static inline void devlink_sb_unregister(struct devlink
*devlink
,
491 unsigned int sb_index
)
496 devlink_dpipe_table_register(struct devlink
*devlink
,
497 const char *table_name
,
498 struct devlink_dpipe_table_ops
*table_ops
,
499 void *priv
, bool counter_control_extern
)
504 static inline void devlink_dpipe_table_unregister(struct devlink
*devlink
,
505 const char *table_name
)
509 static inline int devlink_dpipe_headers_register(struct devlink
*devlink
,
510 struct devlink_dpipe_headers
*
516 static inline void devlink_dpipe_headers_unregister(struct devlink
*devlink
)
520 static inline bool devlink_dpipe_table_counter_enabled(struct devlink
*devlink
,
521 const char *table_name
)
527 devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx
*dump_ctx
)
533 devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx
*dump_ctx
,
534 struct devlink_dpipe_entry
*entry
)
540 devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx
*dump_ctx
)
546 devlink_dpipe_entry_clear(struct devlink_dpipe_entry
*entry
)
551 devlink_dpipe_action_put(struct sk_buff
*skb
,
552 struct devlink_dpipe_action
*action
)
558 devlink_dpipe_match_put(struct sk_buff
*skb
,
559 struct devlink_dpipe_match
*match
)
565 devlink_resource_register(struct devlink
*devlink
,
566 const char *resource_name
,
570 u64 parent_resource_id
,
571 const struct devlink_resource_size_params
*size_params
,
572 const struct devlink_resource_ops
*resource_ops
)
578 devlink_resources_unregister(struct devlink
*devlink
,
579 struct devlink_resource
*resource
)
584 devlink_resource_size_get(struct devlink
*devlink
, u64 resource_id
,
585 u64
*p_resource_size
)
591 devlink_dpipe_table_resource_set(struct devlink
*devlink
,
592 const char *table_name
, u64 resource_id
,
600 #endif /* _NET_DEVLINK_H_ */