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