mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-08 12:41:29 +00:00
portability: fix virNetDevSetMAC and virNetDevExists on BSD
- provide virNetDevSetMAC() implementation based on SIOCSIFLLADDR ioctl. - adjust virNetDevExists() to check for ENXIO error because FreeBSD throws it when device doesn't exist Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
parent
bfe7721d50
commit
95934171fb
@ -2371,6 +2371,14 @@ AC_CHECK_MEMBERS([struct ifreq.ifr_newname,
|
|||||||
[#include <sys/socket.h>
|
[#include <sys/socket.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
])
|
])
|
||||||
|
# Check for BSD approach for setting MAC addr
|
||||||
|
AC_CHECK_DECLS([link_addr],
|
||||||
|
[], [],
|
||||||
|
[#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <net/if_dl.h>
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
# Only COPYING.LIB is under version control, yet COPYING
|
# Only COPYING.LIB is under version control, yet COPYING
|
||||||
# is included as part of the distribution tarball.
|
# is included as part of the distribution tarball.
|
||||||
|
@ -47,6 +47,11 @@
|
|||||||
# undef HAVE_STRUCT_IFREQ
|
# undef HAVE_STRUCT_IFREQ
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if HAVE_DECL_LINK_ADDR
|
||||||
|
# include <sys/sockio.h>
|
||||||
|
# include <net/if_dl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_NONE
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||||
|
|
||||||
#if defined(HAVE_STRUCT_IFREQ)
|
#if defined(HAVE_STRUCT_IFREQ)
|
||||||
@ -110,7 +115,7 @@ int virNetDevExists(const char *ifname)
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
|
if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
|
||||||
if (errno == ENODEV)
|
if (errno == ENODEV || errno == ENXIO)
|
||||||
ret = 0;
|
ret = 0;
|
||||||
else
|
else
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
@ -179,6 +184,40 @@ cleanup:
|
|||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
#elif defined(SIOCSIFLLADDR) && defined(HAVE_STRUCT_IFREQ) && \
|
||||||
|
HAVE_DECL_LINK_ADDR
|
||||||
|
int virNetDevSetMAC(const char *ifname,
|
||||||
|
const virMacAddrPtr macaddr)
|
||||||
|
{
|
||||||
|
struct ifreq ifr;
|
||||||
|
struct sockaddr_dl sdl;
|
||||||
|
char mac[VIR_MAC_STRING_BUFLEN + 1] = ":";
|
||||||
|
int s;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if ((s = virNetDevSetupControl(ifname, &ifr)) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
virMacAddrFormat(macaddr, mac + 1);
|
||||||
|
sdl.sdl_len = sizeof(sdl);
|
||||||
|
link_addr(mac, &sdl);
|
||||||
|
|
||||||
|
memcpy(ifr.ifr_addr.sa_data, sdl.sdl_data, VIR_MAC_BUFLEN);
|
||||||
|
ifr.ifr_addr.sa_len = VIR_MAC_BUFLEN;
|
||||||
|
|
||||||
|
if (ioctl(s, SIOCSIFLLADDR, &ifr) < 0) {
|
||||||
|
virReportSystemError(errno,
|
||||||
|
_("Cannot set interface MAC on '%s'"),
|
||||||
|
ifname);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
cleanup:
|
||||||
|
VIR_FORCE_CLOSE(s);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
int virNetDevSetMAC(const char *ifname,
|
int virNetDevSetMAC(const char *ifname,
|
||||||
const virMacAddrPtr macaddr ATTRIBUTE_UNUSED)
|
const virMacAddrPtr macaddr ATTRIBUTE_UNUSED)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user