libnl  3.5.0
link.c
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 
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 
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 
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 
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 
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 
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 
78 void nl_cli_link_parse_name(struct rtnl_link *link, char *arg)
79 {
80  rtnl_link_set_name(link, arg);
81 }
82 
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 
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 
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 
101 void nl_cli_link_parse_weight(struct rtnl_link *link, char *arg)
102 {
103 }
104 
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 /** @} */
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
Definition: cache_mngt.c:333
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:43
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78