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