link.c

00001 /*
00002  * src/lib/link.c     CLI Link Helpers
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2008-2010 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 /**
00013  * @ingroup cli
00014  * @defgroup cli_link Links
00015  *
00016  * @{
00017  */
00018 
00019 #include <netlink/cli/utils.h>
00020 #include <netlink/cli/link.h>
00021 #include <linux/if.h>
00022 
00023 struct rtnl_link *nl_cli_link_alloc(void)
00024 {
00025         struct rtnl_link *link;
00026 
00027         link = rtnl_link_alloc();
00028         if (!link)
00029                 nl_cli_fatal(ENOMEM, "Unable to allocate link object");
00030 
00031         return link;
00032 }
00033 
00034 struct nl_cache *nl_cli_link_alloc_cache_family(struct nl_sock *sock, int family)
00035 {
00036         struct nl_cache *cache;
00037         int err;
00038 
00039         if ((err = rtnl_link_alloc_cache(sock, family, &cache)) < 0)
00040                 nl_cli_fatal(err, "Unable to allocate link cache: %s",
00041                              nl_geterror(err));
00042 
00043         nl_cache_mngt_provide(cache);
00044 
00045         return cache;
00046 }
00047 
00048 struct nl_cache *nl_cli_link_alloc_cache(struct nl_sock *sock)
00049 {
00050         return nl_cli_link_alloc_cache_family(sock, AF_UNSPEC);
00051 }
00052 
00053 void nl_cli_link_parse_family(struct rtnl_link *link, char *arg)
00054 {
00055         int family;
00056 
00057         if ((family = nl_str2af(arg)) < 0)
00058                 nl_cli_fatal(EINVAL,
00059                              "Unable to translate address family \"%s\"", arg);
00060 
00061         rtnl_link_set_family(link, family);
00062 }
00063 
00064 void nl_cli_link_parse_name(struct rtnl_link *link, char *arg)
00065 {
00066         rtnl_link_set_name(link, arg);
00067 }
00068 
00069 void nl_cli_link_parse_mtu(struct rtnl_link *link, char *arg)
00070 {
00071         uint32_t mtu = nl_cli_parse_u32(arg);
00072         rtnl_link_set_mtu(link, mtu);
00073 }
00074 
00075 void nl_cli_link_parse_ifindex(struct rtnl_link *link, char *arg)
00076 {
00077         uint32_t index = nl_cli_parse_u32(arg);
00078         rtnl_link_set_ifindex(link, index);
00079 }
00080 
00081 void nl_cli_link_parse_txqlen(struct rtnl_link *link, char *arg)
00082 {
00083         uint32_t qlen = nl_cli_parse_u32(arg);
00084         rtnl_link_set_txqlen(link, qlen);
00085 }
00086 
00087 void nl_cli_link_parse_weight(struct rtnl_link *link, char *arg)
00088 {
00089         uint32_t weight = nl_cli_parse_u32(arg);
00090         rtnl_link_set_weight(link, weight);
00091 }
00092 
00093 void nl_cli_link_parse_ifalias(struct rtnl_link *link, char *arg)
00094 {
00095         if (strlen(arg) > IFALIASZ)
00096                 nl_cli_fatal(ERANGE,
00097                         "Link ifalias too big, must not exceed %u in length.",
00098                         IFALIASZ);
00099 
00100         rtnl_link_set_ifalias(link, arg);
00101 }
00102 
00103 /** @} */