along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <netinet/ether.h>
+
#include "link-config.h"
+#include "ethtool-util.h"
+
+#include "libudev-private.h"
+#include "sd-rtnl.h"
#include "util.h"
#include "log.h"
#include "strv.h"
struct link_config_ctx {
LIST_HEAD(link_config, links);
+ int ethtool_fd;
+
+ sd_rtnl *rtnl;
+
char **link_dirs;
usec_t *link_dirs_ts_usec;
};
int link_config_ctx_new(link_config_ctx **ret) {
link_config_ctx *ctx;
+ int r;
if (!ret)
return -EINVAL;
if (!ctx)
return -ENOMEM;
+ r = ethtool_connect(&ctx->ethtool_fd);
+ if (r < 0) {
+ link_config_ctx_free(ctx);
+ return r;
+ }
+
+ r = sd_rtnl_open(0, &ctx->rtnl);
+ if (r < 0) {
+ link_config_ctx_free(ctx);
+ return r;
+ }
+
LIST_HEAD_INIT(ctx->links);
ctx->link_dirs = strv_new("/etc/net/links",
if (!ctx)
return;
+ if (ctx->ethtool_fd >= 0)
+ close_nointr_nofail(ctx->ethtool_fd);
+
+ sd_rtnl_unref(ctx->rtnl);
+
strv_free(ctx->link_dirs);
free(ctx->link_dirs_ts_usec);
link_configs_free(ctx);
return -ENOENT;
}
+static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, const char *mac, unsigned int mtu) {
+ _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
+ bool need_update;
+ int r;
+
+ assert(rtnl);
+ assert(ifindex > 0);
+
+ r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
+ if (r < 0)
+ return r;
+
+ if (name) {
+ r = sd_rtnl_message_append(message, IFLA_IFNAME, name);
+ if (r < 0)
+ return r;
+
+ need_update = true;
+ }
+
+ if (mac) {
+ r = sd_rtnl_message_append(message, IFLA_ADDRESS, ether_aton(mac));
+ if (r < 0)
+ return r;
+
+ need_update = true;
+ }
+
+ if (mtu > 0) {
+ r = sd_rtnl_message_append(message, IFLA_MTU, &mtu);
+ if (r < 0)
+ return r;
+
+ need_update = true;
+ }
+
+ if (need_update) {
+ r = sd_rtnl_send_with_reply_and_block(rtnl, message, 250 * USEC_PER_MSEC, NULL);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device) {
const char *name;
- int r;
+ int r, ifindex;
name = udev_device_get_sysname(device);
if (!name)
log_info("Configuring %s", name);
if (config->description) {
- r = udev_device_set_sysattr_value(device, "ifalias", config->description);
+ r = udev_device_set_sysattr_value(device, "ifalias",
+ config->description);
if (r < 0)
- log_warning("Could not set description of %s to '%s': %s", name, config->description, strerror(-r));
+ log_warning("Could not set description of %s to '%s': %s",
+ name, config->description, strerror(-r));
else
- log_info("Set link description of %s to '%s'", name, config->description);
+ log_info("Set link description of %s to '%s'", name,
+ config->description);
+ }
+
+ if (config->speed || config->duplex) {
+ r = ethtool_set_speed(ctx->ethtool_fd, name,
+ config->speed, config->duplex);
+ if (r < 0)
+ log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
+ name, config->speed, config->duplex, strerror(-r));
+ else
+ log_info("Set speed or duplex of %s to %u Mbytes (%s)", name,
+ config->speed, config->duplex);
+ }
+
+ if (config->wol) {
+ r = ethtool_set_wol(ctx->ethtool_fd, name, config->wol);
+ if (r < 0)
+ log_warning("Could not set WakeOnLan of %s to %s: %s",
+ name, config->wol, strerror(-r));
+ else
+ log_info("Set WakeOnLan of %s to %s", name, config->wol);
+ }
+
+ ifindex = udev_device_get_ifindex(device);
+ if (ifindex <= 0) {
+ log_warning("Could not find ifindex");
+ return -ENODEV;
+ }
+
+ r = rtnl_set_properties(ctx->rtnl, ifindex, config->name, config->mac, config->mtu);
+ if (r < 0) {
+ log_warning("Could not set Name, MACAddress or MTU on %s", name);
+ return r;
}
return 0;