xref: /aosp_15_r20/external/libnl/src/lib/link.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2008-2010 Thomas Graf <[email protected]>
4  */
5 
6 /**
7  * @ingroup cli
8  * @defgroup cli_link Links
9  *
10  * @{
11  */
12 
13 #include "nl-default.h"
14 
15 #include <linux/if.h>
16 
17 #include <netlink/cli/utils.h>
18 #include <netlink/cli/link.h>
19 
nl_cli_link_alloc(void)20 struct rtnl_link *nl_cli_link_alloc(void)
21 {
22 	struct rtnl_link *link;
23 
24 	link = rtnl_link_alloc();
25 	if (!link)
26 		nl_cli_fatal(ENOMEM, "Unable to allocate link object");
27 
28 	return link;
29 }
30 
nl_cli_link_alloc_cache_family_flags(struct nl_sock * sock,int family,unsigned int flags)31 struct nl_cache *nl_cli_link_alloc_cache_family_flags(struct nl_sock *sock,
32 						      int family,
33 						      unsigned int flags)
34 {
35 	struct nl_cache *cache;
36 	int err;
37 
38 	if ((err = rtnl_link_alloc_cache_flags(sock, family, &cache, flags)) < 0)
39 		nl_cli_fatal(err, "Unable to allocate link cache: %s",
40 			     nl_geterror(err));
41 
42 	nl_cache_mngt_provide(cache);
43 
44 	return cache;
45 }
46 
nl_cli_link_alloc_cache_family(struct nl_sock * sock,int family)47 struct nl_cache *nl_cli_link_alloc_cache_family(struct nl_sock *sock, int family)
48 {
49 	return nl_cli_link_alloc_cache_family_flags(sock, family, 0);
50 }
51 
nl_cli_link_alloc_cache(struct nl_sock * sock)52 struct nl_cache *nl_cli_link_alloc_cache(struct nl_sock *sock)
53 {
54 	return nl_cli_link_alloc_cache_family(sock, AF_UNSPEC);
55 }
56 
nl_cli_link_alloc_cache_flags(struct nl_sock * sock,unsigned int flags)57 struct nl_cache *nl_cli_link_alloc_cache_flags(struct nl_sock *sock,
58 						unsigned int flags)
59 {
60 	return nl_cli_link_alloc_cache_family_flags(sock, AF_UNSPEC, flags);
61 }
62 
nl_cli_link_parse_family(struct rtnl_link * link,char * arg)63 void nl_cli_link_parse_family(struct rtnl_link *link, char *arg)
64 {
65 	int family;
66 
67 	if ((family = nl_str2af(arg)) < 0)
68 		nl_cli_fatal(EINVAL,
69 			     "Unable to translate address family \"%s\"", arg);
70 
71 	rtnl_link_set_family(link, family);
72 }
73 
nl_cli_link_parse_name(struct rtnl_link * link,char * arg)74 void nl_cli_link_parse_name(struct rtnl_link *link, char *arg)
75 {
76 	rtnl_link_set_name(link, arg);
77 }
78 
nl_cli_link_parse_mtu(struct rtnl_link * link,char * arg)79 void nl_cli_link_parse_mtu(struct rtnl_link *link, char *arg)
80 {
81 	uint32_t mtu = nl_cli_parse_u32(arg);
82 	rtnl_link_set_mtu(link, mtu);
83 }
84 
nl_cli_link_parse_ifindex(struct rtnl_link * link,char * arg)85 void nl_cli_link_parse_ifindex(struct rtnl_link *link, char *arg)
86 {
87 	uint32_t index = nl_cli_parse_u32(arg);
88 	rtnl_link_set_ifindex(link, index);
89 }
90 
nl_cli_link_parse_txqlen(struct rtnl_link * link,char * arg)91 void nl_cli_link_parse_txqlen(struct rtnl_link *link, char *arg)
92 {
93 	uint32_t qlen = nl_cli_parse_u32(arg);
94 	rtnl_link_set_txqlen(link, qlen);
95 }
96 
nl_cli_link_parse_weight(struct rtnl_link * link,char * arg)97 void nl_cli_link_parse_weight(struct rtnl_link *link, char *arg)
98 {
99 }
100 
nl_cli_link_parse_ifalias(struct rtnl_link * link,char * arg)101 void nl_cli_link_parse_ifalias(struct rtnl_link *link, char *arg)
102 {
103 	if (strlen(arg) > IFALIASZ)
104 		nl_cli_fatal(ERANGE,
105 			"Link ifalias too big, must not exceed %u in length.",
106 			IFALIASZ);
107 
108 	rtnl_link_set_ifalias(link, arg);
109 }
110 
111 /** @} */
112