From: Ben Pfaff Date: Tue, 3 Mar 2009 20:37:51 +0000 (-0800) Subject: netdev: New function netdev_nodev_set_etheraddr(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53afb7a0098bc1d5b3dbda123275759ac4a375ec;p=openvswitch netdev: New function netdev_nodev_set_etheraddr(). --- diff --git a/lib/netdev.c b/lib/netdev.c index 37d1f4c2..5bca129b 100644 --- a/lib/netdev.c +++ b/lib/netdev.c @@ -145,6 +145,8 @@ static int do_get_ifindex(const char *netdev_name); static int get_ifindex(const struct netdev *, int *ifindexp); static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN], int *hwaddr_familyp); +static int set_etheraddr(const char *netdev_name, int hwaddr_family, + const uint8_t[ETH_ADDR_LEN]); /* Obtains the IPv6 address for 'name' into 'in6'. */ static void @@ -672,19 +674,18 @@ netdev_send_wait(struct netdev *netdev) int netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN]) { - struct ifreq ifr; - - memset(&ifr, 0, sizeof ifr); - strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name); - ifr.ifr_hwaddr.sa_family = netdev->hwaddr_family; - memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN); - if (ioctl(netdev->netdev_fd, SIOCSIFHWADDR, &ifr) < 0) { - VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s", - netdev->name, strerror(errno)); - return errno; + int error = set_etheraddr(netdev->name, netdev->hwaddr_family, mac); + if (!error) { + memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN); } - memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN); - return 0; + return error; +} + +int +netdev_nodev_set_etheraddr(const char *name, const uint8_t mac[ETH_ADDR_LEN]) +{ + init_netdev(); + return set_etheraddr(name, ARPHRD_ETHER, mac); } /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or @@ -1272,6 +1273,23 @@ get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN], } } memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN); + return 0; +} +static int +set_etheraddr(const char *netdev_name, int hwaddr_family, + const uint8_t mac[ETH_ADDR_LEN]) +{ + struct ifreq ifr; + + memset(&ifr, 0, sizeof ifr); + strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name); + ifr.ifr_hwaddr.sa_family = hwaddr_family; + memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN); + if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) { + VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s", + netdev_name, strerror(errno)); + return errno; + } return 0; } diff --git a/lib/netdev.h b/lib/netdev.h index 960396ec..6981803a 100644 --- a/lib/netdev.h +++ b/lib/netdev.h @@ -121,6 +121,7 @@ int netdev_get_stats(const struct netdev *, struct netdev_stats *); void netdev_enumerate(struct svec *); int netdev_nodev_get_flags(const char *netdev_name, enum netdev_flags *); +int netdev_nodev_set_etheraddr(const char *name, const uint8_t mac[6]); int netdev_nodev_get_etheraddr(const char *netdev_name, uint8_t mac[6]); #endif /* netdev.h */