expose devlink instances relationships
Subsystems (3)
v3 cover expose devlink instances relationships
From: Jiri Pirko <[email protected]>
Print out recently added attributes that expose relationships between devlink instances. This patchset extends the outputs by "nested_devlink" attributes.
Examples:
$ devlink dev
pci/0000:08:00.0:
nested_devlink:
auxiliary/mlx5_core.eth.0
auxiliary/mlx5_core.eth.0
pci/0000:08:00.1:
nested_devlink:
auxiliary/mlx5_core.eth.1
auxiliary/mlx5_core.eth.1$ devlink dev -j -p
{
"dev": {
"pci/0000:08:00.0": {
"nested_devlink": {
"auxiliary/mlx5_core.eth.0": {}
}
},
"auxiliary/mlx5_core.eth.0": {},
"pci/0000:08:00.1": {
"nested_devlink": {
"auxiliary/mlx5_core.eth.1": {}
}
},
"auxiliary/mlx5_core.eth.1": {}
}
}$ devlink port add pci/0000:08:00.0 flavour pcisf pfnum 0 sfnum 106
pci/0000:08:00.0/32768: type eth netdev eth2 flavour pcisf controller 0 pfnum 0 sfnum 106 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable
$ devlink port function set pci/0000:08:00.0/32768 state active
$ devlink port show pci/0000:08:00.0/32768
pci/0000:08:00.0/32768: type eth netdev eth2 flavour pcisf controller 0 pfnum 0 sfnum 106 splittable false
function:
hw_addr 00:00:00:00:00:00 state active opstate attached roce enable
nested_devlink:
auxiliary/mlx5_core.sf.2
$ devlink port show pci/0000:08:00.0/32768 -j -p
{
"port": {
"pci/0000:08:00.0/32768": {
"type": "eth",
"netdev": "eth2",
"flavour": "pcisf",
"controller": 0,
"pfnum": 0,
"sfnum": 106,
"splittable": false,
"function": {
"hw_addr": "00:00:00:00:00:00",
"state": "active",
"opstate": "attached",
"roce": "enable",
"nested_devlink": {
"auxiliary/mlx5_core.sf.2": {}
}
}
}
}
}$ devlink dev reload auxiliary/mlx5_core.sf.2 netns ns1
$ devlink port show pci/0000:08:00.0/32768
pci/0000:08:00.0/32768: type eth netdev eth2 flavour pcisf controller 0 pfnum 0 sfnum 106 splittable false
function:
hw_addr 00:00:00:00:00:00 state active opstate attached roce enable
nested_devlink:
auxiliary/mlx5_core.sf.2: netns ns1
$ devlink port show pci/0000:08:00.0/32768 -j -p
{
"port": {
"pci/0000:08:00.0/32768": {
"type": "eth",
"netdev": "eth2",
"flavour": "pcisf",
"controller": 0,
"pfnum": 0,
"sfnum": 106,
"splittable": false,
"function": {
"hw_addr": "00:00:00:00:00:00",
"state": "active",
"opstate": "attached",
"roce": "enable",
"nested_devlink": {
"auxiliary/mlx5_core.sf.2": {
"netns": "ns1"
}
}
}
}
}
}---v2->v3:
- the output format is changed to treat the nested handle in a similar way as the dev_handle/port_handle allowing to contain attrs, like netns. See examples
- couple of details
- see individual patches for more detailsv1->v2:
- patch #2 was added
- patch #3 uses new helper added by patch #2, typo is fixed
Jiri Pirko (6):
ip/ipnetns: move internals of get_netnsid_from_name() into namespace.c
devlink: do conditional new line print in pr_out_port_handle_end()
devlink: extend pr_out_nested_handle() to print object
devlink: introduce support for netns id for nested handle
devlink: print nested handle for port function
devlink: print nested devlink handle for devlink dev
devlink/devlink.c | 124 +++++++++++++++++++++++++++++++++++--------- include/namespace.h | 5 ++ ip/ipnetns.c | 45 +--------------- lib/namespace.c | 83 +++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+), 68 deletions(-)
Signature · 1 line
2.41.0
PATCH v3 1/6 ip/ipnetns: move internals of get_netnsid_from_name() into namespace.c
From: Jiri Pirko <[email protected]>
In order to be able to reuse get_netnsid_from_name() function outside of ip code, move the internals to lib/namespace.c to a new function called netns_id_from_name().
---v2->v3:
- s/netns_netnsid_from_name/netns_id_from_name/v1->v2:
- new patch
--- include/namespace.h | 4 ++++ ip/ipnetns.c | 45 +---------------------------------------- lib/namespace.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 44 deletions(-)
include/namespace.h +4 -0
@@ -8,6 +8,8 @@#include <sys/syscall.h> #include <errno.h> +#include "namespace.h" +#ifndef NETNS_RUN_DIR #define NETNS_RUN_DIR "/var/run/netns"#endif
@@ -58,4 +60,6 @@ struct netns_func { void *arg; }; +int netns_id_from_name (struct rtnl_handle *rtnl,const char *name); +#endif /* __NAMESPACE_H__ */
ip/ipnetns.c +1 -44
53 lines not on this page · raw message
lib/namespace.c +49 -0
61 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [patch iproute2-next v3 1/6] ip/ipnetns: move internals of get_netnsid_from_name() into namespace.c
include/namespace.h line 11
Quoted 9 lines · Jiri Pirko
include/namespace.h +1
-0@@ -8,6 +8,8 @@#include <sys/syscall.h> #include <errno.h> +#include "namespace.h"
??? including this file into itself?
Quoted 11 lines
include/namespace.h +3
-0+#ifndef NETNS_RUN_DIR #define NETNS_RUN_DIR "/var/run/netns"#endif @@ -58,4 +60,6 @@ struct netns_func { void *arg; }; +int netns_id_from_name (struct rtnl_handle *rtnl,const char *name); +#endif /* __NAMESPACE_H__ */
Re: [patch iproute2-next v3 1/6] ip/ipnetns: move internals of get_netnsid_from_name() into namespace.c
Quoted 9 lines · Thu, Oct 26, 2023 at 06:56:20PM CEST, [email protected] wrote:
Quoted 8 lines · Jiri Pirko
include/namespace.h +0
-0@@ -8,6 +8,8 @@#include <sys/syscall.h> #include <errno.h>
Quoted 3 lines
include/namespace.h line 11
Quoted 1 line
include/namespace.h +1
-0+#include "namespace.h" ??? including this file into itself?
Interesting. Must have been done during code move. Will fix.
Quoted 13 lines
Quoted 12 lines
include/namespace.h +3
-0+#ifndef NETNS_RUN_DIR #define NETNS_RUN_DIR "/var/run/netns"#endif @@ -58,4 +60,6 @@ struct netns_func { void *arg; }; +int netns_id_from_name (struct rtnl_handle *rtnl,const char *name); +#endif /* __NAMESPACE_H__ */
PATCH v3 2/6 devlink: do conditional new line print in pr_out_port_handle_end()
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Instead of printing out new line unconditionally, use __pr_out_newline() to print it only when needed avoiding double prints.
---v2->v3:
- new patch
--- devlink/devlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
devlink/devlink.c +1 -1
@@ -2974,7 +2974,7 @@ static void pr_out_port_handle_end(struct dl *dl)if (dl->json_output)close_json_object ();else -pr_out ("\n" ); +__pr_out_newline (); }static void pr_out_region_chunk_start (struct dl *dl,uint64_t addr)
Signature · 1 line
2.41.0
PATCH v3 3/6 devlink: extend pr_out_nested_handle() to print object
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
For existing pr_out_nested_handle() user (line card), the output stays the same. For the new users, introduce __pr_out_nested_handle() to allow to print devlink instance as object allowing to carry attributes in it (like netns).
Note that as __pr_out_handle_start() and pr_out_handle_end() are newly used, the function is moved below the definitions.
---v2->v3:
- new patch
--- devlink/devlink.c | 51 +++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 19 deletions(-)
devlink/devlink.c +32 -19
@@ -2747,25 +2747,6 @@ static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name, !cmp_arr_last_handle (dl, bus_name, dev_name); } -static void pr_out_nested_handle (struct nlattr *nla_nested_dl) -{ -struct nlattr *tb[DEVLINK_ATTR_MAX +1 ] = {}; -char buf[64 ]; -int err; - - err =mnl_attr_parse_nested (nla_nested_dl, attr_cb, tb); -if (err !=MNL_CB_OK ) -return ; - -if (!tb[DEVLINK_ATTR_BUS_NAME ] || - !tb[DEVLINK_ATTR_DEV_NAME ]) -return ; - -sprintf (buf,"%s/%s" ,mnl_attr_get_str (tb[DEVLINK_ATTR_BUS_NAME ]), -mnl_attr_get_str (tb[DEVLINK_ATTR_DEV_NAME ])); -print_string (PRINT_ANY ,"nested_devlink" ," nested_devlink %s" , buf); -} -static void __pr_out_handle_start (struct dl *dl,struct nlattr **tb,bool content,bool array) {
@@ -2861,6 +2842,38 @@ static void pr_out_selftests_handle_end(struct dl *dl)__pr_out_newline (); } +static void __pr_out_nested_handle (struct dl *dl,struct nlattr *nla_nested_dl, +bool is_object) +{ +struct nlattr *tb[DEVLINK_ATTR_MAX +1 ] = {}; +int err; + + err =mnl_attr_parse_nested (nla_nested_dl, attr_cb, tb); +if (err !=MNL_CB_OK ) +return ; + +if (!tb[DEVLINK_ATTR_BUS_NAME ] || + !tb[DEVLINK_ATTR_DEV_NAME ]) +return ; + +if (!is_object) { +char buf[64 ]; + +sprintf (buf,"%s/%s" ,mnl_attr_get_str (tb[DEVLINK_ATTR_BUS_NAME ]), +mnl_attr_get_str (tb[DEVLINK_ATTR_DEV_NAME ])); +print_string (PRINT_ANY ,"nested_devlink" ," nested_devlink %s" , buf); +return ; + } + +__pr_out_handle_start (dl, tb, false, false); +pr_out_handle_end (dl); +} + +static void pr_out_nested_handle (struct nlattr *nla_nested_dl) +{ +__pr_out_nested_handle (NULL , nla_nested_dl, false); +} +static bool cmp_arr_last_port_handle (struct dl *dl,const char *bus_name,const char *dev_name,uint32_t port_index) {
Signature · 1 line
2.41.0
Re: [patch iproute2-next v3 3/6] devlink: extend pr_out_nested_handle() to print object
Quoted 13 lines · Jiri Pirko
devlink/devlink.c +9
-0@@ -2861,6 +2842,38 @@ static void pr_out_selftests_handle_end(struct dl *dl)__pr_out_newline (); } +static void __pr_out_nested_handle (struct dl *dl,struct nlattr *nla_nested_dl, +bool is_object) +{ +struct nlattr *tb[DEVLINK_ATTR_MAX +1 ] = {}; +int err; + + err =mnl_attr_parse_nested (nla_nested_dl, attr_cb, tb); +if (err !=MNL_CB_OK ) +return ;
devlink/devlink.c static void pr_out_selftests_handle_end(struct dl *dl) · line 2863
Quoted 10 lines
devlink/devlink.c +10
-0+ +if (!tb[DEVLINK_ATTR_BUS_NAME ] || + !tb[DEVLINK_ATTR_DEV_NAME ]) +return ; + +if (!is_object) { +char buf[64 ]; + +sprintf (buf,"%s/%s" ,mnl_attr_get_str (tb[DEVLINK_ATTR_BUS_NAME ]), +mnl_attr_get_str (tb[DEVLINK_ATTR_DEV_NAME ]));
buf[64] - 1 for null terminator - 16 for IFNAMSIZ leaves 47. I do not see limits on bus name length, so how can you guarantee it is always < 47 characters?
Make this snprintf, check the return and make sure buf is null terminated.
Quoted 16 lines
devlink/devlink.c +13
-0+print_string (PRINT_ANY ,"nested_devlink" ," nested_devlink %s" , buf); +return ; + } + +__pr_out_handle_start (dl, tb, false, false); +pr_out_handle_end (dl); +} + +static void pr_out_nested_handle (struct nlattr *nla_nested_dl) +{ +__pr_out_nested_handle (NULL , nla_nested_dl, false); +} +static bool cmp_arr_last_port_handle (struct dl *dl,const char *bus_name,const char *dev_name,uint32_t port_index) {
Re: [patch iproute2-next v3 3/6] devlink: extend pr_out_nested_handle() to print object
Quoted 18 lines · Thu, Oct 26, 2023 at 07:03:30PM CEST, [email protected] wrote:
Quoted 17 lines · Jiri Pirko
devlink/devlink.c +13
-0@@ -2861,6 +2842,38 @@ static void pr_out_selftests_handle_end(struct dl *dl)__pr_out_newline (); } +static void __pr_out_nested_handle (struct dl *dl,struct nlattr *nla_nested_dl, +bool is_object) +{ +struct nlattr *tb[DEVLINK_ATTR_MAX +1 ] = {}; +int err; + + err =mnl_attr_parse_nested (nla_nested_dl, attr_cb, tb); +if (err !=MNL_CB_OK ) +return ; + +if (!tb[DEVLINK_ATTR_BUS_NAME ] || + !tb[DEVLINK_ATTR_DEV_NAME ]) +return ;
Quoted 8 lines
devlink/devlink.c static void pr_out_selftests_handle_end(struct dl *dl) · line 2863
Quoted 6 lines
devlink/devlink.c +6
-0+ +if (!is_object) { +char buf[64 ]; + +sprintf (buf,"%s/%s" ,mnl_attr_get_str (tb[DEVLINK_ATTR_BUS_NAME ]), +mnl_attr_get_str (tb[DEVLINK_ATTR_DEV_NAME ]));buf[64] - 1 for null terminator - 16 for IFNAMSIZ leaves 47. I do not
IFNAMSIZ is irrelevant here.
Quoted 4 lines
see limits on bus name length, so how can you guarantee it is always < 47 characters?
Make this snprintf, check the return and make sure buf is null terminated.
I will fix it in separate patch, as this is just copied here.
Quoted 18 lines
Quoted 17 lines
devlink/devlink.c +13
-0+print_string (PRINT_ANY ,"nested_devlink" ," nested_devlink %s" , buf); +return ; + } + +__pr_out_handle_start (dl, tb, false, false); +pr_out_handle_end (dl); +} + +static void pr_out_nested_handle (struct nlattr *nla_nested_dl) +{ +__pr_out_nested_handle (NULL , nla_nested_dl, false); +} +static bool cmp_arr_last_port_handle (struct dl *dl,const char *bus_name,const char *dev_name,uint32_t port_index) {
Re: [patch iproute2-next v3 3/6] devlink: extend pr_out_nested_handle() to print object
In reply to David AhernQuoted 29 lines · David Ahern
Quoted 13 lines · Jiri Pirko
devlink/devlink.c +9
-0@@ -2861,6 +2842,38 @@ static void pr_out_selftests_handle_end(struct dl *dl)__pr_out_newline (); } +static void __pr_out_nested_handle (struct dl *dl,struct nlattr *nla_nested_dl, +bool is_object) +{ +struct nlattr *tb[DEVLINK_ATTR_MAX +1 ] = {}; +int err; + + err =mnl_attr_parse_nested (nla_nested_dl, attr_cb, tb); +if (err !=MNL_CB_OK ) +return ;devlink/devlink.c static void pr_out_selftests_handle_end(struct dl *dl) · line 2863
Quoted 10 lines
devlink/devlink.c +10
-0+ +if (!tb[DEVLINK_ATTR_BUS_NAME ] || + !tb[DEVLINK_ATTR_DEV_NAME ]) +return ; + +if (!is_object) { +char buf[64 ]; + +sprintf (buf,"%s/%s" ,mnl_attr_get_str (tb[DEVLINK_ATTR_BUS_NAME ]), +mnl_attr_get_str (tb[DEVLINK_ATTR_DEV_NAME ]));buf[64] - 1 for null terminator - 16 for IFNAMSIZ leaves 47. I do not see limits on bus name length, so how can you guarantee it is always < 47 characters?
Quoted 1 line
Make this snprintf, check the return and make sure buf is null terminated.
I was wondering whether somehing like this might make sense in the iproute2 library:
#define alloca_sprintf(FMT, ...) ({ \
int xasprintf_n = snprintf(NULL, 0, (FMT), __VA_ARGS__); \
char *xasprintf_buf = alloca(xasprintf_n); \
sprintf(xasprintf_buf, (FMT), __VA_ARGS__); \
xasprintf_buf; \
})
void foo() {
const char *buf = alloca_sprintf("%x %y %z", etc.);
printf(... buf ...);
}I'm not really happy with it -- because of alloca vs. array, and because of the double evaluation. But all those SPRINT_BUF's peppered everywhere make me uneasy every time I read or write them.
Or maybe roll something custom asprintf-like that can reuse and/or realloc a passed-in buffer?
The sprintf story is pretty bad in iproute2 right now, IMHO.
Re: [patch iproute2-next v3 3/6] devlink: extend pr_out_nested_handle() to print object
Quoted 15 lines · Petr Machata
I was wondering whether somehing like this might make sense in the iproute2 library:
#define alloca_sprintf(FMT, ...) ({ \ int xasprintf_n = snprintf(NULL, 0, (FMT), __VA_ARGS__); \ char *xasprintf_buf = alloca(xasprintf_n); \ sprintf(xasprintf_buf, (FMT), __VA_ARGS__); \ xasprintf_buf; \ }) void foo() { const char *buf = alloca_sprintf("%x %y %z", etc.); printf(... buf ...); }
Quoted 3 lines
I'm not really happy with it -- because of alloca vs. array, and because of the double evaluation. But all those SPRINT_BUF's peppered everywhere make me uneasy every time I read or write them.
agreed.
Quoted 5 lines
Or maybe roll something custom asprintf-like that can reuse and/or realloc a passed-in buffer?
The sprintf story is pretty bad in iproute2 right now, IMHO.
It is a bit of a mess. If you have a few cycles, want to send an RFC? Just pick 1 or 2 to convert to show intent with a new design.
Re: [patch iproute2-next v3 3/6] devlink: extend pr_out_nested_handle() to print object
Quoted 23 lines · David Ahern
Quoted 15 lines · Petr Machata
I was wondering whether somehing like this might make sense in the iproute2 library:
#define alloca_sprintf(FMT, ...) ({ \ int xasprintf_n = snprintf(NULL, 0, (FMT), __VA_ARGS__); \ char *xasprintf_buf = alloca(xasprintf_n); \ sprintf(xasprintf_buf, (FMT), __VA_ARGS__); \ xasprintf_buf; \ }) void foo() { const char *buf = alloca_sprintf("%x %y %z", etc.); printf(... buf ...); }Quoted 3 lines
I'm not really happy with it -- because of alloca vs. array, and because of the double evaluation. But all those SPRINT_BUF's peppered everywhere make me uneasy every time I read or write them.
agreed.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Or maybe roll something custom asprintf-like that can reuse and/or realloc a passed-in buffer?
The sprintf story is pretty bad in iproute2 right now, IMHO.
It is a bit of a mess. If you have a few cycles, want to send an RFC? Just pick 1 or 2 to convert to show intent with a new design.
I picked at it a bit over the weekend, but came up with nothing that I find comfortable proposing. The static buffer approach has some major
advantages: nothing ever fails and nothing ever needs cleanups. This keeps the client code tidy and compact. Anything dynamic adds points of failure and cleanups, which in C means more client-side boilerplate. Anyway, I'll pick at it some more and see I find anything.
PATCH v3 4/6 devlink: introduce support for netns id for nested handle
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Nested handle may contain DEVLINK_ATTR_NETNS_ID attribute that indicates the network namespace where the nested devlink instance resides. Process this converting to netns name if possible and print to user.
---v2->v3:
- moved netns_name_by_id() into lib/namespace.c
- s/netns_name_by_id/netns_name_from_id/
- rebased on top of new patch "devlink: extend pr_out_nested_handle() to print object"v1->v2:
- use previously introduced netns_netnsid_from_name() instead of code duplication for the same function.
- s/nesns_name_by_id_func/netns_name_by_id_func/
--- devlink/devlink.c | 23 ++++++++++++++++++++++- include/namespace.h | 1 + lib/namespace.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-)
devlink/devlink.c +22 -1
@@ -24,6 +24,7 @@#include <linux/genetlink.h> #include <linux/devlink.h> #include <linux/netlink.h> +#include <linux/net_namespace.h> #include <libmnl/libmnl.h> #include <netinet/ether.h> #include <sys/select.h>
@@ -722,6 +723,7 @@ static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = { [DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES] = MNL_TYPE_NESTED, [DEVLINK_ATTR_NESTED_DEVLINK] = MNL_TYPE_NESTED, [DEVLINK_ATTR_SELFTESTS] = MNL_TYPE_NESTED, + [DEVLINK_ATTR_NETNS_ID] = MNL_TYPE_U32, }; static const enum mnl_attr_data_type
@@ -2865,7 +2867,26 @@ static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl,return ; } -__pr_out_handle_start (dl, tb, false, false); +__pr_out_handle_start (dl, tb, tb[DEVLINK_ATTR_NETNS_ID ], false); +if (tb[DEVLINK_ATTR_NETNS_ID ]) { +int32_t id =mnl_attr_get_u32 (tb[DEVLINK_ATTR_NETNS_ID ]); + +if (id >=0 ) { +char *name =netns_name_from_id (id); + +if (name) { +print_string (PRINT_ANY ,"netns" , +" netns %s" , name); +free (name); + }else { +print_int (PRINT_ANY ,"netnsid" , +" netnsid %d" , id); + } + }else { +print_string (PRINT_FP ,NULL ," netnsid %s" ,"unknown" ); +print_int (PRINT_JSON ,"netnsid" ,NULL , id); + } + }pr_out_handle_end (dl); }
include/namespace.h +1 -0
@@ -61,5 +61,6 @@ struct netns_func { };int netns_id_from_name (struct rtnl_handle *rtnl,const char *name); +char *netns_name_from_id (int32_t id);#endif /* __NAMESPACE_H__ */
lib/namespace.c +34 -0
@@ -188,3 +188,37 @@ out:free (answer);return ret; } + +struct netns_name_from_id_ctx { +int32_t id; +char *name; +struct rtnl_handle *rth; +}; + +static int netns_name_from_id_func (char *nsname,void *arg) +{ +struct netns_name_from_id_ctx *ctx = arg; +int32_t ret; + + ret =netns_id_from_name (ctx->rth, nsname); +if (ret <0 || ret != ctx->id) +return 0 ; + ctx->name =strdup (nsname); +return 1 ; +} + +char *netns_name_from_id (int32_t id) +{ +struct rtnl_handle rth; +struct netns_name_from_id_ctx ctx = { + .id = id, + .rth = &rth, + }; + +if (rtnl_open (&rth,0 ) <0 ) +return NULL ; +netns_foreach (netns_name_from_id_func, &ctx); +rtnl_close (&rth); + +return ctx.name; +}
Signature · 1 line
2.41.0
Re: [patch iproute2-next v3 4/6] devlink: introduce support for netns id for nested handle
Quoted 44 lines · Jiri Pirko
From: Jiri Pirko <[email protected]>
Nested handle may contain
DEVLINK_ATTR_NETNS_IDattribute that indicates the network namespace where the nested devlink instance resides. Process this converting to netns name if possible and print to user.Signed-off-by Jiri Pirko <[email protected]>---
v2->v3:
- movednetns_name_by_id()intolib/namespace.c
- s/netns_name_by_id/netns_name_from_id/
- rebased on top of new patch "devlink: extendpr_out_nested_handle()to print object"v1->v2:
- use previously introducednetns_netnsid_from_name()instead of code duplication for the same function.
- s/nesns_name_by_id_func/netns_name_by_id_func/--- devlink/devlink.c | 23 ++++++++++++++++++++++- include/namespace.h | 1 + lib/namespace.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-)devlink/devlink.c +2
-0@@ -24,6 +24,7 @@#include <linux/genetlink.h> #include <linux/devlink.h> #include <linux/netlink.h> +#include <linux/net_namespace.h> #include <libmnl/libmnl.h> #include <netinet/ether.h> #include <sys/select.h> @@ -722,6 +723,7 @@ static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = { [DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES] = MNL_TYPE_NESTED, [DEVLINK_ATTR_NESTED_DEVLINK] = MNL_TYPE_NESTED, [DEVLINK_ATTR_SELFTESTS] = MNL_TYPE_NESTED, + [DEVLINK_ATTR_NETNS_ID] = MNL_TYPE_U32, }; static const enum mnl_attr_data_type
devlink/devlink.c static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl, · line 2874
Quoted 9 lines
devlink/devlink.c +5
-1@@ -2865,7 +2867,26 @@ static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl,return ; } -__pr_out_handle_start (dl, tb, false, false); +__pr_out_handle_start (dl, tb, tb[DEVLINK_ATTR_NETNS_ID ], false); +if (tb[DEVLINK_ATTR_NETNS_ID ]) { +int32_t id =mnl_attr_get_u32 (tb[DEVLINK_ATTR_NETNS_ID ]); + +if (id >=0 ) {
why does the kernel side even add DEVLINK_ATTR_NETNS_ID if peernet2id_alloc fails?
devlink/devlink.c static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl, · line 2887
Quoted 13 lines
devlink/devlink.c +13
-0+char *name =netns_name_from_id (id); + +if (name) { +print_string (PRINT_ANY ,"netns" , +" netns %s" , name); +free (name); + }else { +print_int (PRINT_ANY ,"netnsid" , +" netnsid %d" , id); + } + }else { +print_string (PRINT_FP ,NULL ," netnsid %s" ,"unknown" ); +print_int (PRINT_JSON ,"netnsid" ,NULL , id);
ie., how is -1 useful to userspace?
Quoted 5 lines
devlink/devlink.c +2
-0+ } + }pr_out_handle_end (dl); }
Re: [patch iproute2-next v3 4/6] devlink: introduce support for netns id for nested handle
Quoted 48 lines · Thu, Oct 26, 2023 at 07:08:04PM CEST, [email protected] wrote:
Quoted 47 lines · Jiri Pirko
From: Jiri Pirko <[email protected]>
Nested handle may contain
DEVLINK_ATTR_NETNS_IDattribute that indicates the network namespace where the nested devlink instance resides. Process this converting to netns name if possible and print to user.Signed-off-by Jiri Pirko <[email protected]>---
v2->v3:
- movednetns_name_by_id()intolib/namespace.c
- s/netns_name_by_id/netns_name_from_id/
- rebased on top of new patch "devlink: extendpr_out_nested_handle()to print object"v1->v2:
- use previously introducednetns_netnsid_from_name()instead of code duplication for the same function.
- s/nesns_name_by_id_func/netns_name_by_id_func/--- devlink/devlink.c | 23 ++++++++++++++++++++++- include/namespace.h | 1 + lib/namespace.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-)devlink/devlink.c +2
-0@@ -24,6 +24,7 @@#include <linux/genetlink.h> #include <linux/devlink.h> #include <linux/netlink.h> +#include <linux/net_namespace.h> #include <libmnl/libmnl.h> #include <netinet/ether.h> #include <sys/select.h> @@ -722,6 +723,7 @@ static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = { [DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES] = MNL_TYPE_NESTED, [DEVLINK_ATTR_NESTED_DEVLINK] = MNL_TYPE_NESTED, [DEVLINK_ATTR_SELFTESTS] = MNL_TYPE_NESTED, + [DEVLINK_ATTR_NETNS_ID] = MNL_TYPE_U32, }; static const enum mnl_attr_data_type@@ -2865,7 +2867,26 @@ static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl,return ; }
Quoted 9 lines
devlink/devlink.c static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl, · line 2874
Quoted 6 lines
devlink/devlink.c +5
-1-__pr_out_handle_start (dl, tb, false, false); +__pr_out_handle_start (dl, tb, tb[DEVLINK_ATTR_NETNS_ID ], false); +if (tb[DEVLINK_ATTR_NETNS_ID ]) { +int32_t id =mnl_attr_get_u32 (tb[DEVLINK_ATTR_NETNS_ID ]); + +if (id >=0 ) {why does the kernel side even add
DEVLINK_ATTR_NETNS_IDifpeernet2id_allocfails?
Yep. So the user knows that this is in a different namespace. This logic is copied from iplink
Quoted 17 lines
Quoted 5 lines
devlink/devlink.c +3
-0+char *name =netns_name_from_id (id); + +if (name) {devlink/devlink.c static void __pr_out_nested_handle(struct dl *dl, struct nlattr *nla_nested_dl, · line 2887
Quoted 10 lines
devlink/devlink.c +10
-0+print_string (PRINT_ANY ,"netns" , +" netns %s" , name); +free (name); + }else { +print_int (PRINT_ANY ,"netnsid" , +" netnsid %d" , id); + } + }else { +print_string (PRINT_FP ,NULL ," netnsid %s" ,"unknown" ); +print_int (PRINT_JSON ,"netnsid" ,NULL , id);ie., how is -1 useful to userspace?
The userspace knows that this device is in a different netnamespace, but it is unknown as peernet2id_alloc failed. Should be really a rare corner case when system is under memory pressure.
Quoted 9 lines
Quoted 7 lines
devlink/devlink.c +2
-0+ } + }pr_out_handle_end (dl); }
PATCH v3 5/6 devlink: print nested handle for port function
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
If port function contains nested handle attribute, print it.
---v2->v3:
- rebased on top of new patch "devlink: extend pr_out_nested_handle() to print object"
--- devlink/devlink.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
devlink/devlink.c +20 -0
@@ -772,6 +772,7 @@ static const enum mnl_attr_data_type devlink_function_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = { [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR ] = MNL_TYPE_BINARY, [DEVLINK_PORT_FN_ATTR_STATE] = MNL_TYPE_U8, + [DEVLINK_PORT_FN_ATTR_DEVLINK] = MNL_TYPE_NESTED, };static int function_attr_cb (const struct nlattr *attr,void *data)
@@ -2895,6 +2896,22 @@ static void pr_out_nested_handle(struct nlattr *nla_nested_dl)__pr_out_nested_handle (NULL , nla_nested_dl, false); } +static void pr_out_nested_handle_obj (struct dl *dl, +struct nlattr *nla_nested_dl, +bool obj_start,bool obj_end) +{ +if (obj_start) { +pr_out_object_start (dl,"nested_devlink" ); +check_indent_newline (dl); + } +__pr_out_nested_handle (dl, nla_nested_dl, true); +if (obj_end) { +if (!dl->json_output) +__pr_out_indent_dec (); +pr_out_object_end (dl); + } +} +static bool cmp_arr_last_port_handle (struct dl *dl,const char *bus_name,const char *dev_name,uint32_t port_index) {
@@ -4837,6 +4854,9 @@ static void pr_out_port_function(struct dl *dl, struct nlattr **tb_port) port_fn_caps->value &DEVLINK_PORT_FN_CAP_IPSEC_PACKET ? "enable" : "disable"); } +if (tb[DEVLINK_PORT_FN_ATTR_DEVLINK ]) +pr_out_nested_handle_obj (dl, tb[DEVLINK_PORT_FN_ATTR_DEVLINK ], + true, true);if (!dl->json_output)__pr_out_indent_dec ();
Signature · 1 line
2.41.0
PATCH v3 6/6 devlink: print nested devlink handle for devlink dev
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Devlink dev may contain one or more nested devlink instances. Print them using previously introduced pr_out_nested_handle_obj() helper.
---v2->v3:
- rebased on top of new patch "devlink: extend pr_out_nested_handle() to print object" and previous patch to use pr_out_nested_handle_obj()
--- devlink/devlink.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-)
devlink/devlink.c +26 -4
@@ -3858,13 +3858,35 @@ static void pr_out_reload_data(struct dl *dl, struct nlattr **tb)pr_out_object_end (dl); } +static void pr_out_dev_nested (struct dl *dl,const struct nlmsghdr *nlh) +{ +int i =0 , count =0 ; +struct nlattr *attr; + +mnl_attr_for_each (attr, nlh,sizeof (struct genlmsghdr )) { +if (mnl_attr_get_type (attr) ==DEVLINK_ATTR_NESTED_DEVLINK ) + count++; + } +if (!count) +return ; + +mnl_attr_for_each (attr, nlh,sizeof (struct genlmsghdr )) { +if (mnl_attr_get_type (attr) !=DEVLINK_ATTR_NESTED_DEVLINK ) +continue ; +pr_out_nested_handle_obj (dl, attr, i ==0 , i == count -1 ); + i++; + } +} -static void pr_out_dev (struct dl *dl,struct nlattr **tb) +static void pr_out_dev (struct dl *dl,const struct nlmsghdr *nlh, +struct nlattr **tb) {if ((tb[DEVLINK_ATTR_RELOAD_FAILED ] &&mnl_attr_get_u8 (tb[DEVLINK_ATTR_RELOAD_FAILED ])) || - (tb[DEVLINK_ATTR_DEV_STATS ] && dl->stats)) { + (tb[DEVLINK_ATTR_DEV_STATS ] && dl->stats) || + tb[DEVLINK_ATTR_NESTED_DEVLINK ]) {__pr_out_handle_start (dl, tb, true, false);pr_out_reload_data (dl, tb); +pr_out_dev_nested (dl, nlh);pr_out_handle_end (dl); }else {pr_out_handle (dl, tb);
@@ -3881,7 +3903,7 @@ static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data)if (!tb[DEVLINK_ATTR_BUS_NAME ] || !tb[DEVLINK_ATTR_DEV_NAME ])return MNL_CB_ERROR ; -pr_out_dev (dl, tb); +pr_out_dev (dl, nlh, tb);return MNL_CB_OK ; }
@@ -6808,7 +6830,7 @@ static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)return MNL_CB_ERROR ;pr_out_mon_header (genl->cmd); dl->stats = true; -pr_out_dev (dl, tb); +pr_out_dev (dl, nlh, tb);pr_out_mon_footer ();break ;case DEVLINK_CMD_PORT_GET :/* fall through */
Signature · 1 line
2.41.0