blob: 6545b03e97f73fd5d439a092b9374dfea83d9319 [file] [log] [blame]
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01001/*
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>
5 *
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.
10 */
11#ifndef _NET_DEVLINK_H_
12#define _NET_DEVLINK_H_
13
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>
21
22struct devlink_ops;
23
24struct devlink {
25 struct list_head list;
26 struct list_head port_list;
Jiri Pirkobf797472016-04-14 18:19:13 +020027 struct list_head sb_list;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +020028 struct list_head dpipe_table_list;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +010029 struct list_head resource_list;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +020030 struct devlink_dpipe_headers *dpipe_headers;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010031 const struct devlink_ops *ops;
32 struct device *dev;
33 possible_net_t _net;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +010034 struct mutex lock;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010035 char priv[0] __aligned(NETDEV_ALIGN);
36};
37
38struct devlink_port {
39 struct list_head list;
40 struct devlink *devlink;
41 unsigned index;
42 bool registered;
43 enum devlink_port_type type;
44 enum devlink_port_type desired_type;
45 void *type_dev;
46 bool split;
47 u32 split_group;
48};
49
Jiri Pirkobf797472016-04-14 18:19:13 +020050struct devlink_sb_pool_info {
51 enum devlink_sb_pool_type pool_type;
52 u32 size;
53 enum devlink_sb_threshold_type threshold_type;
54};
55
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +020056/**
57 * struct devlink_dpipe_field - dpipe field object
58 * @name: field name
59 * @id: index inside the headers field array
60 * @bitwidth: bitwidth
61 * @mapping_type: mapping type
62 */
63struct devlink_dpipe_field {
64 const char *name;
65 unsigned int id;
66 unsigned int bitwidth;
67 enum devlink_dpipe_field_mapping_type mapping_type;
68};
69
70/**
71 * struct devlink_dpipe_header - dpipe header object
72 * @name: header name
73 * @id: index, global/local detrmined by global bit
74 * @fields: fields
75 * @fields_count: number of fields
76 * @global: indicates if header is shared like most protocol header
77 * or driver specific
78 */
79struct devlink_dpipe_header {
80 const char *name;
81 unsigned int id;
82 struct devlink_dpipe_field *fields;
83 unsigned int fields_count;
84 bool global;
85};
86
87/**
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)
92 * @header: header
93 * @fieled_id: field index
94 */
95struct 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;
100};
101
102/**
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)
107 * @header: header
108 * @fieled_id: field index
109 */
110struct 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;
115};
116
117/**
118 * struct devlink_dpipe_value - represents value of match/action
119 * @action: action
120 * @match: match
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
125 * @value: value
126 * @mask: bit mask
127 */
128struct devlink_dpipe_value {
129 union {
130 struct devlink_dpipe_action *action;
131 struct devlink_dpipe_match *match;
132 };
133 unsigned int mapping_value;
134 bool mapping_valid;
135 unsigned int value_size;
136 void *value;
137 void *mask;
138};
139
140/**
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
149 */
150struct devlink_dpipe_entry {
151 u64 index;
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;
156 u64 counter;
157 bool counter_valid;
158};
159
160/**
161 * struct devlink_dpipe_dump_ctx - context provided to driver in order
162 * to dump
163 * @info: info
164 * @cmd: devlink command
165 * @skb: skb
166 * @nest: top attribute
167 * @hdr: hdr
168 */
169struct devlink_dpipe_dump_ctx {
170 struct genl_info *info;
171 enum devlink_command cmd;
172 struct sk_buff *skb;
173 struct nlattr *nest;
174 void *hdr;
175};
176
177struct devlink_dpipe_table_ops;
178
179/**
180 * struct devlink_dpipe_table - table object
181 * @priv: private
182 * @name: table name
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200183 * @counters_enabled: indicates if counters are active
184 * @counter_control_extern: indicates if counter control is in dpipe or
185 * external tool
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +0100186 * @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
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200189 * @table_ops: table operations
190 * @rcu: rcu
191 */
192struct devlink_dpipe_table {
193 void *priv;
194 struct list_head list;
195 const char *name;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200196 bool counters_enabled;
197 bool counter_control_extern;
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +0100198 bool resource_valid;
199 u64 resource_id;
200 u64 resource_units;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200201 struct devlink_dpipe_table_ops *table_ops;
202 struct rcu_head rcu;
203};
204
205/**
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
212 * resources
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +0200213 * @size_get - get size
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200214 */
215struct 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);
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +0200221 u64 (*size_get)(void *priv);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200222};
223
224/**
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
228 */
229struct devlink_dpipe_headers {
230 struct devlink_dpipe_header **headers;
231 unsigned int headers_count;
232};
233
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +0100234/**
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
239 */
240struct 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);
244};
245
246/**
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
252 */
253struct devlink_resource_size_params {
254 u64 size_min;
255 u64 size_max;
256 u64 size_granularity;
257 enum devlink_resource_unit unit;
258};
259
260/**
261 * struct devlink_resource - devlink resource
262 * @name: name of the resource
263 * @id: id, per devlink instance
264 * @size: size of the resource
265 * @size_new: updated size of the resource, reload is needed
266 * @size_valid: valid in case the total size of the resource is valid
267 * including its children
268 * @parent: parent resource
269 * @size_params: size parameters
270 * @list: parent list
271 * @resource_list: list of child resources
272 * @resource_ops: resource ops
273 */
274struct devlink_resource {
275 const char *name;
276 u64 id;
277 u64 size;
278 u64 size_new;
279 bool size_valid;
280 struct devlink_resource *parent;
281 struct devlink_resource_size_params *size_params;
282 struct list_head list;
283 struct list_head resource_list;
284 const struct devlink_resource_ops *resource_ops;
285};
286
287#define DEVLINK_RESOURCE_ID_PARENT_TOP 0
288
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100289struct devlink_ops {
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +0100290 int (*reload)(struct devlink *devlink);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100291 int (*port_type_set)(struct devlink_port *devlink_port,
292 enum devlink_port_type port_type);
293 int (*port_split)(struct devlink *devlink, unsigned int port_index,
294 unsigned int count);
295 int (*port_unsplit)(struct devlink *devlink, unsigned int port_index);
Jiri Pirkobf797472016-04-14 18:19:13 +0200296 int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
297 u16 pool_index,
298 struct devlink_sb_pool_info *pool_info);
299 int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
300 u16 pool_index, u32 size,
301 enum devlink_sb_threshold_type threshold_type);
302 int (*sb_port_pool_get)(struct devlink_port *devlink_port,
303 unsigned int sb_index, u16 pool_index,
304 u32 *p_threshold);
305 int (*sb_port_pool_set)(struct devlink_port *devlink_port,
306 unsigned int sb_index, u16 pool_index,
307 u32 threshold);
308 int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
309 unsigned int sb_index,
310 u16 tc_index,
311 enum devlink_sb_pool_type pool_type,
312 u16 *p_pool_index, u32 *p_threshold);
313 int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
314 unsigned int sb_index,
315 u16 tc_index,
316 enum devlink_sb_pool_type pool_type,
317 u16 pool_index, u32 threshold);
Jiri Pirkodf38daf2016-04-14 18:19:14 +0200318 int (*sb_occ_snapshot)(struct devlink *devlink,
319 unsigned int sb_index);
320 int (*sb_occ_max_clear)(struct devlink *devlink,
321 unsigned int sb_index);
322 int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
323 unsigned int sb_index, u16 pool_index,
324 u32 *p_cur, u32 *p_max);
325 int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
326 unsigned int sb_index,
327 u16 tc_index,
328 enum devlink_sb_pool_type pool_type,
329 u32 *p_cur, u32 *p_max);
Or Gerlitz08f4b592016-07-01 14:51:01 +0300330
331 int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
332 int (*eswitch_mode_set)(struct devlink *devlink, u16 mode);
Roi Dayan59bfde02016-11-22 23:09:57 +0200333 int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
334 int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode);
Roi Dayanf43e9b02016-09-25 13:52:44 +0300335 int (*eswitch_encap_mode_get)(struct devlink *devlink, u8 *p_encap_mode);
336 int (*eswitch_encap_mode_set)(struct devlink *devlink, u8 encap_mode);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100337};
338
339static inline void *devlink_priv(struct devlink *devlink)
340{
341 BUG_ON(!devlink);
342 return &devlink->priv;
343}
344
345static inline struct devlink *priv_to_devlink(void *priv)
346{
347 BUG_ON(!priv);
348 return container_of(priv, struct devlink, priv);
349}
350
351struct ib_device;
352
353#if IS_ENABLED(CONFIG_NET_DEVLINK)
354
355struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
356int devlink_register(struct devlink *devlink, struct device *dev);
357void devlink_unregister(struct devlink *devlink);
358void devlink_free(struct devlink *devlink);
359int devlink_port_register(struct devlink *devlink,
360 struct devlink_port *devlink_port,
361 unsigned int port_index);
362void devlink_port_unregister(struct devlink_port *devlink_port);
363void devlink_port_type_eth_set(struct devlink_port *devlink_port,
364 struct net_device *netdev);
365void devlink_port_type_ib_set(struct devlink_port *devlink_port,
366 struct ib_device *ibdev);
367void devlink_port_type_clear(struct devlink_port *devlink_port);
368void devlink_port_split_set(struct devlink_port *devlink_port,
369 u32 split_group);
Jiri Pirkobf797472016-04-14 18:19:13 +0200370int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
371 u32 size, u16 ingress_pools_count,
372 u16 egress_pools_count, u16 ingress_tc_count,
373 u16 egress_tc_count);
374void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200375int devlink_dpipe_table_register(struct devlink *devlink,
376 const char *table_name,
377 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +0200378 void *priv, bool counter_control_extern);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200379void devlink_dpipe_table_unregister(struct devlink *devlink,
380 const char *table_name);
381int devlink_dpipe_headers_register(struct devlink *devlink,
382 struct devlink_dpipe_headers *dpipe_headers);
383void devlink_dpipe_headers_unregister(struct devlink *devlink);
384bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
385 const char *table_name);
386int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx);
387int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
388 struct devlink_dpipe_entry *entry);
389int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx);
Arkadi Sharshevsky35807322017-08-24 08:40:03 +0200390void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200391int devlink_dpipe_action_put(struct sk_buff *skb,
392 struct devlink_dpipe_action *action);
393int devlink_dpipe_match_put(struct sk_buff *skb,
394 struct devlink_dpipe_match *match);
Arkadi Sharshevsky11770092017-08-24 08:39:59 +0200395extern struct devlink_dpipe_header devlink_dpipe_header_ethernet;
Arkadi Sharshevsky3fb886e2017-08-24 08:40:00 +0200396extern struct devlink_dpipe_header devlink_dpipe_header_ipv4;
Arkadi Sharshevsky1797f5b2017-08-31 17:59:12 +0200397extern struct devlink_dpipe_header devlink_dpipe_header_ipv6;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100398
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +0100399int devlink_resource_register(struct devlink *devlink,
400 const char *resource_name,
401 bool top_hierarchy,
402 u64 resource_size,
403 u64 resource_id,
404 u64 parent_resource_id,
405 struct devlink_resource_size_params *size_params,
406 const struct devlink_resource_ops *resource_ops);
407void devlink_resources_unregister(struct devlink *devlink,
408 struct devlink_resource *resource);
409int devlink_resource_size_get(struct devlink *devlink,
410 u64 resource_id,
411 u64 *p_resource_size);
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +0100412int devlink_dpipe_table_resource_set(struct devlink *devlink,
413 const char *table_name, u64 resource_id,
414 u64 resource_units);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +0100415
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100416#else
417
418static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
419 size_t priv_size)
420{
421 return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL);
422}
423
424static inline int devlink_register(struct devlink *devlink, struct device *dev)
425{
426 return 0;
427}
428
429static inline void devlink_unregister(struct devlink *devlink)
430{
431}
432
433static inline void devlink_free(struct devlink *devlink)
434{
435 kfree(devlink);
436}
437
438static inline int devlink_port_register(struct devlink *devlink,
439 struct devlink_port *devlink_port,
440 unsigned int port_index)
441{
442 return 0;
443}
444
445static inline void devlink_port_unregister(struct devlink_port *devlink_port)
446{
447}
448
449static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port,
450 struct net_device *netdev)
451{
452}
453
454static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port,
455 struct ib_device *ibdev)
456{
457}
458
459static inline void devlink_port_type_clear(struct devlink_port *devlink_port)
460{
461}
462
463static inline void devlink_port_split_set(struct devlink_port *devlink_port,
464 u32 split_group)
465{
466}
467
Jiri Pirkobf797472016-04-14 18:19:13 +0200468static inline int devlink_sb_register(struct devlink *devlink,
469 unsigned int sb_index, u32 size,
470 u16 ingress_pools_count,
Jiri Pirkode33efd2016-04-15 09:17:08 +0200471 u16 egress_pools_count,
472 u16 ingress_tc_count,
473 u16 egress_tc_count)
Jiri Pirkobf797472016-04-14 18:19:13 +0200474{
475 return 0;
476}
477
478static inline void devlink_sb_unregister(struct devlink *devlink,
479 unsigned int sb_index)
480{
481}
482
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200483static inline int
484devlink_dpipe_table_register(struct devlink *devlink,
485 const char *table_name,
486 struct devlink_dpipe_table_ops *table_ops,
David S. Miller790c6052017-08-24 18:10:46 -0700487 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200488{
489 return 0;
490}
491
492static inline void devlink_dpipe_table_unregister(struct devlink *devlink,
493 const char *table_name)
494{
495}
496
497static inline int devlink_dpipe_headers_register(struct devlink *devlink,
498 struct devlink_dpipe_headers *
499 dpipe_headers)
500{
501 return 0;
502}
503
504static inline void devlink_dpipe_headers_unregister(struct devlink *devlink)
505{
506}
507
508static inline bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
509 const char *table_name)
510{
511 return false;
512}
513
514static inline int
515devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
516{
517 return 0;
518}
519
520static inline int
521devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
522 struct devlink_dpipe_entry *entry)
523{
524 return 0;
525}
526
527static inline int
528devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
529{
530 return 0;
531}
532
Arkadi Sharshevsky35807322017-08-24 08:40:03 +0200533static inline void
534devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
535{
536}
537
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +0200538static inline int
539devlink_dpipe_action_put(struct sk_buff *skb,
540 struct devlink_dpipe_action *action)
541{
542 return 0;
543}
544
545static inline int
546devlink_dpipe_match_put(struct sk_buff *skb,
547 struct devlink_dpipe_match *match)
548{
549 return 0;
550}
551
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +0100552static inline int
553devlink_resource_register(struct devlink *devlink,
554 const char *resource_name,
555 bool top_hierarchy,
556 u64 resource_size,
557 u64 resource_id,
558 u64 parent_resource_id,
559 struct devlink_resource_size_params *size_params,
560 const struct devlink_resource_ops *resource_ops)
561{
562 return 0;
563}
564
565static inline void
566devlink_resources_unregister(struct devlink *devlink,
567 struct devlink_resource *resource)
568{
569}
570
571static inline int
572devlink_resource_size_get(struct devlink *devlink, u64 resource_id,
573 u64 *p_resource_size)
574{
575 return -EOPNOTSUPP;
576}
577
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +0100578static inline int
579devlink_dpipe_table_resource_set(struct devlink *devlink,
580 const char *table_name, u64 resource_id,
581 u64 resource_units)
582{
583 return -EOPNOTSUPP;
584}
585
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100586#endif
587
588#endif /* _NET_DEVLINK_H_ */