diff --git a/doc/route.txt b/doc/route.txt index cd24eef..c8f1735 100644 --- a/doc/route.txt +++ b/doc/route.txt @@ -645,9 +645,8 @@ This attribute is unused and obsoleted in all recent kernels. struct rtnl_link *link; -link = rtnl_link_alloc(); +link = rtnl_link_bond_alloc(); rtnl_link_set_name(link, "my_bond"); -rtnl_link_set_type(link, "bond"); /* requires admin privileges */ if (rtnl_link_add(sk, link, NLM_F_CREATE) < 0) diff --git a/include/netlink/route/link/bonding.h b/include/netlink/route/link/bonding.h index 78ee6bd..5c34662 100644 --- a/include/netlink/route/link/bonding.h +++ b/include/netlink/route/link/bonding.h @@ -6,7 +6,7 @@ * License as published by the Free Software Foundation version 2.1 * of the License. * - * Copyright (c) 2011 Thomas Graf + * Copyright (c) 2011-2013 Thomas Graf */ #ifndef NETLINK_LINK_BONDING_H_ @@ -19,6 +19,8 @@ extern "C" { #endif +extern struct rtnl_link *rtnl_link_bond_alloc(void); + extern int rtnl_link_bond_add(struct nl_sock *, const char *, struct rtnl_link *); diff --git a/lib/route/link/bonding.c b/lib/route/link/bonding.c index b060ee1..f4c520b 100644 --- a/lib/route/link/bonding.c +++ b/lib/route/link/bonding.c @@ -6,7 +6,7 @@ * License as published by the Free Software Foundation version 2.1 * of the License. * - * Copyright (c) 2011 Thomas Graf + * Copyright (c) 2011-2013 Thomas Graf */ /** @@ -24,6 +24,27 @@ #include #include +/** + * Allocate link object of type bond + * + * @return Allocated link object or NULL. + */ +struct rtnl_link *rtnl_link_bond_alloc(void) +{ + struct rtnl_link *link; + int err; + + if (!(link = rtnl_link_alloc())) + return NULL; + + if ((err = rtnl_link_set_type(link, "bond")) < 0) { + rtnl_link_put(link); + return NULL; + } + + return link; +} + /** * Create a new kernel bonding device * @arg sock netlink socket @@ -54,22 +75,17 @@ int rtnl_link_bond_add(struct nl_sock *sock, const char *name, struct rtnl_link *link; int err; - if (!(link = rtnl_link_alloc())) + if (!(link = rtnl_link_bond_alloc())) return -NLE_NOMEM; - if (!name) { - if (opts) - name = rtnl_link_get_name(opts); - } - - if ((err = rtnl_link_set_type(link, "bond")) < 0) - goto errout; + if (!name && opts) + name = rtnl_link_get_name(opts); if (name) rtnl_link_set_name(link, name); err = rtnl_link_add(sock, link, NLM_F_CREATE); -errout: + rtnl_link_put(link); return err; @@ -94,12 +110,9 @@ int rtnl_link_bond_enslave_ifindex(struct nl_sock *sock, int master, struct rtnl_link *link; int err; - if (!(link = rtnl_link_alloc())) + if (!(link = rtnl_link_bond_alloc())) return -NLE_NOMEM; - if ((err = rtnl_link_set_type(link, "bond")) < 0) - goto errout; - rtnl_link_set_ifindex(link, slave); rtnl_link_set_master(link, master); diff --git a/tests/test-create-bond.c b/tests/test-create-bond.c index e54b0e6..11bc5b0 100644 --- a/tests/test-create-bond.c +++ b/tests/test-create-bond.c @@ -1,5 +1,6 @@ #include #include +#include int main(int argc, char *argv[]) { @@ -13,14 +14,9 @@ int main(int argc, char *argv[]) return err; } - link = rtnl_link_alloc(); + link = rtnl_link_bond_alloc(); rtnl_link_set_name(link, "my_bond"); - if ((err = rtnl_link_set_type(link, "bond")) < 0) { - nl_perror(err, "Unable to set link info type"); - return err; - } - if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) { nl_perror(err, "Unable to add link"); return err;