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