VALID_CARRIER = 1 << 5
};
-/* An AF_INET socket (used for ioctl operations). */
-static int af_inet_sock = -1;
-
#if defined(__NetBSD__)
/* AF_LINK socket used for netdev_bsd_get_stats and set_etheraddr */
static int af_link_sock = -1;
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
-static int netdev_bsd_do_ioctl(const char *, struct ifreq *, unsigned long cmd,
- const char *cmd_name);
static void destroy_tap(int fd, const char *name);
static int get_flags(const struct netdev *, int *flagsp);
static int set_flags(const char *, int flags);
static int
netdev_bsd_init(void)
{
+#if defined(__NetBSD__)
static int status = -1;
if (status >= 0) { /* already initialized */
return status;
}
- af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
- status = af_inet_sock >= 0 ? 0 : errno;
- if (status) {
- VLOG_ERR("failed to create inet socket: %s", ovs_strerror(status));
- return status;
- }
-
-#if defined(__NetBSD__)
af_link_sock = socket(AF_LINK, SOCK_DGRAM, 0);
status = af_link_sock >= 0 ? 0 : errno;
if (status) {
VLOG_ERR("failed to create link socket: %s", ovs_strerror(status));
- close(af_inet_sock);
- af_inet_sock = -1;
}
-#endif /* defined(__NetBSD__) */
return status;
+#else
+ return 0;
+#endif /* defined(__NetBSD__) */
}
/*
/* Change the name of the tap device */
#if defined(SIOCSIFNAME)
ifr.ifr_data = (void *)name;
- if (ioctl(af_inet_sock, SIOCSIFNAME, &ifr) == -1) {
- error = errno;
+ error = af_inet_ioctl(SIOCSIFNAME, &ifr);
+ if (error) {
destroy_tap(netdev->tap_fd, ifr.ifr_name);
goto error_unref_notifier;
}
/* Turn device UP */
ifr_set_flags(&ifr, IFF_UP);
strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
- if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
- error = errno;
+ error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
+ if (error) {
destroy_tap(netdev->tap_fd, kernel_name);
goto error_unref_notifier;
}
struct ifreq ifr;
int error;
- error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
+ error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
SIOCGIFMTU, "SIOCGIFMTU");
if (error) {
return error;
if (!(netdev->cache_valid & VALID_CARRIER)) {
struct ifmediareq ifmr;
+ int error;
memset(&ifmr, 0, sizeof(ifmr));
strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
sizeof ifmr.ifm_name);
- if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
+ error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
+ if (error) {
VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
- netdev_get_name(netdev_), ovs_strerror(errno));
- return errno;
+ netdev_get_name(netdev_), ovs_strerror(error));
+ return error;
}
netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
/* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
* number of supported modes, and a second with a buffer to retrieve
* them. */
- if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
+ error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
+ if (error) {
VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
- netdev_get_name(netdev), ovs_strerror(errno));
- return errno;
+ netdev_get_name(netdev), ovs_strerror(error));
+ return error;
}
media_list = xcalloc(ifmr.ifm_count, sizeof(int));
goto cleanup;
}
- if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
+ error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
+ if (error) {
VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
- netdev_get_name(netdev), ovs_strerror(errno));
- error = errno;
+ netdev_get_name(netdev), ovs_strerror(error));
goto cleanup;
}
int error;
ifr.ifr_addr.sa_family = AF_INET;
- error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
+ error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
SIOCGIFADDR, "SIOCGIFADDR");
if (error) {
return error;
sin = (struct sockaddr_in *) &ifr.ifr_addr;
netdev->in4 = sin->sin_addr;
- error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev_), &ifr,
+ error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
SIOCGIFNETMASK, "SIOCGIFNETMASK");
if (error) {
return error;
{
struct ifreq ifr;
make_in4_sockaddr(&ifr.ifr_addr, addr);
- return netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
+ return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
ioctl_name);
}
close(fd);
strcpy(ifr.ifr_name, name);
/* XXX What to do if this call fails? */
- ioctl(af_inet_sock, SIOCIFDESTROY, &ifr);
+ af_inet_ioctl(SIOCIFDESTROY, &ifr);
}
static int
struct ifreq ifr;
int error;
- error = netdev_bsd_do_ioctl(netdev_get_kernel_name(netdev), &ifr,
+ error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
SIOCGIFFLAGS, "SIOCGIFFLAGS");
*flags = ifr_get_flags(&ifr);
ifr_set_flags(&ifr, flags);
- return netdev_bsd_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
+ return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
}
static int
{
#if defined(__FreeBSD__)
struct ifreq ifr;
+ int error;
memset(&ifr, 0, sizeof ifr);
strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
ifr.ifr_addr.sa_family = hwaddr_family;
ifr.ifr_addr.sa_len = hwaddr_len;
memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
- if (ioctl(af_inet_sock, SIOCSIFLLADDR, &ifr) < 0) {
+ error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
+ if (error) {
VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
- netdev_name, ovs_strerror(errno));
- return errno;
+ netdev_name, ovs_strerror(error));
+ return error;
}
return 0;
#elif defined(__NetBSD__)
#endif
}
-static int
-netdev_bsd_do_ioctl(const char *name, struct ifreq *ifr, unsigned long cmd,
- const char *cmd_name)
-{
- strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
- if (ioctl(af_inet_sock, cmd, ifr) == -1) {
- VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
- ovs_strerror(errno));
- return errno;
- }
- return 0;
-}
-
static int
ifr_get_flags(const struct ifreq *ifr)
{
static const struct netdev_rx_class netdev_rx_linux_class;
-/* Sockets used for ioctl operations. */
-static int af_inet_sock = -1; /* AF_INET, SOCK_DGRAM. */
-
/* This is set pretty low because we probably won't learn anything from the
* additional log messages. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
-static int netdev_linux_init(void);
+static void netdev_linux_run(void);
static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *,
int cmd, const char *cmd_name);
-static int netdev_linux_do_ioctl(const char *name, struct ifreq *, int cmd,
- const char *cmd_name);
static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *,
int cmd, const char *cmd_name);
static int get_flags(const struct netdev *, unsigned int *flags);
static bool
is_netdev_linux_class(const struct netdev_class *netdev_class)
{
- return netdev_class->init == netdev_linux_init;
+ return netdev_class->run == netdev_linux_run;
}
static bool
return CONTAINER_OF(rx, struct netdev_rx_linux, up);
}
\f
-static int
-netdev_linux_init(void)
-{
- static int status = -1;
- if (status < 0) {
- /* Create AF_INET socket. */
- af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
- status = af_inet_sock >= 0 ? 0 : errno;
- if (status) {
- VLOG_ERR("failed to create inet socket: %s", ovs_strerror(status));
- }
- }
- return status;
-}
-
static void
netdev_linux_run(void)
{
struct sockaddr_ll sll;
int ifindex;
/* Result of tcpdump -dd inbound */
- static struct sock_filter filt[] = {
+ static const struct sock_filter filt[] = {
{ 0x28, 0, 0, 0xfffff004 }, /* ldh [0] */
{ 0x15, 0, 1, 0x00000004 }, /* jeq #4 jt 2 jf 3 */
{ 0x6, 0, 0, 0x00000000 }, /* ret #0 */
{ 0x6, 0, 0, 0x0000ffff } /* ret #65535 */
};
- static struct sock_fprog fprog = { ARRAY_SIZE(filt), filt };
+ static const struct sock_fprog fprog = {
+ ARRAY_SIZE(filt), (struct sock_filter *) filt
+ };
/* Create file descriptor. */
fd = socket(PF_PACKET, SOCK_RAW, 0);
sizeof fprog);
if (error) {
error = errno;
- VLOG_ERR("%s: failed attach filter (%s)",
+ VLOG_ERR("%s: failed to attach filter (%s)",
netdev_get_name(netdev_), ovs_strerror(error));
goto error;
}
struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
if (rx->is_tap) {
struct ifreq ifr;
- int error = netdev_linux_do_ioctl(netdev_rx_get_name(rx_), &ifr,
- SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
+ int error = af_inet_ifreq_ioctl(netdev_rx_get_name(rx_), &ifr,
+ SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
if (error) {
return error;
}
struct ifreq ifr;
int error;
- error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr,
- SIOCGIFMTU, "SIOCGIFMTU");
+ error = af_inet_ifreq_ioctl(netdev_get_name(netdev_), &ifr,
+ SIOCGIFMTU, "SIOCGIFMTU");
netdev->netdev_mtu_error = error;
netdev->mtu = ifr.ifr_mtu;
netdev->cache_valid &= ~VALID_MTU;
}
ifr.ifr_mtu = mtu;
- error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr,
- SIOCSIFMTU, "SIOCSIFMTU");
+ error = af_inet_ifreq_ioctl(netdev_get_name(netdev_), &ifr,
+ SIOCSIFMTU, "SIOCSIFMTU");
if (!error || error == ENODEV) {
netdev->netdev_mtu_error = error;
netdev->mtu = ifr.ifr_mtu;
memset(&ifr, 0, sizeof ifr);
memcpy(&ifr.ifr_data, data, sizeof *data);
- error = netdev_linux_do_ioctl(name, &ifr, cmd, cmd_name);
+ error = af_inet_ifreq_ioctl(name, &ifr, cmd, cmd_name);
memcpy(data, &ifr.ifr_data, sizeof *data);
return error;
int ioctl_nr, const char *ioctl_name, struct in_addr addr)
{
struct ifreq ifr;
- ovs_strzcpy(ifr.ifr_name, netdev_get_name(netdev), sizeof ifr.ifr_name);
- make_in4_sockaddr(&ifr.ifr_addr, addr);
- return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr,
- ioctl_name);
+ make_in4_sockaddr(&ifr.ifr_addr, addr);
+ return af_inet_ifreq_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr,
+ ioctl_name);
}
/* Adds 'router' as a default IP gateway. */
make_in4_sockaddr(&rt.rt_gateway, router);
make_in4_sockaddr(&rt.rt_genmask, any);
rt.rt_flags = RTF_UP | RTF_GATEWAY;
- error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
+ error = af_inet_ioctl(SIOCADDRT, &rt);
if (error) {
VLOG_WARN("ioctl(SIOCADDRT): %s", ovs_strerror(error));
}
r.arp_flags = 0;
ovs_strzcpy(r.arp_dev, netdev_get_name(netdev), sizeof r.arp_dev);
COVERAGE_INC(netdev_arp_lookup);
- retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
+ retval = af_inet_ioctl(SIOCGARP, &r);
if (!retval) {
memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
} else if (retval != ENXIO) {
{ \
NAME, \
\
- netdev_linux_init, \
+ NULL, \
netdev_linux_run, \
netdev_linux_wait, \
\
\f
/* Linux-only functions declared in netdev-linux.h */
-/* Returns a fd for an AF_INET socket or a negative errno value. */
-int
-netdev_linux_get_af_inet_sock(void)
-{
- int error = netdev_linux_init();
- return error ? -error : af_inet_sock;
-}
-
/* Modifies the 'flag' bit in ethtool's flags field for 'netdev'. If
* 'enable' is true, the bit is set. Otherwise, it is cleared. */
int
int error;
*flags = 0;
- error = netdev_linux_do_ioctl(dev->name, &ifr, SIOCGIFFLAGS,
- "SIOCGIFFLAGS");
+ error = af_inet_ifreq_ioctl(dev->name, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
if (!error) {
*flags = ifr.ifr_flags;
}
struct ifreq ifr;
ifr.ifr_flags = flags;
- return netdev_linux_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
+ return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
}
static int
do_get_ifindex(const char *netdev_name)
{
struct ifreq ifr;
+ int error;
ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
COVERAGE_INC(netdev_get_ifindex);
- if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
+
+ error = af_inet_ioctl(SIOCGIFINDEX, &ifr);
+ if (error) {
VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
- netdev_name, ovs_strerror(errno));
- return -errno;
+ netdev_name, ovs_strerror(error));
+ return -error;
}
return ifr.ifr_ifindex;
}
{
struct ifreq ifr;
int hwaddr_family;
+ int error;
memset(&ifr, 0, sizeof ifr);
ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
COVERAGE_INC(netdev_get_hwaddr);
- if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
+ error = af_inet_ioctl(SIOCGIFHWADDR, &ifr);
+ if (error) {
/* ENODEV probably means that a vif disappeared asynchronously and
* hasn't been removed from the database yet, so reduce the log level
* to INFO for that case. */
- VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR,
+ VLOG(error == ENODEV ? VLL_INFO : VLL_ERR,
"ioctl(SIOCGIFHWADDR) on %s device failed: %s",
- netdev_name, ovs_strerror(errno));
- return errno;
+ netdev_name, ovs_strerror(error));
+ return error;
}
hwaddr_family = ifr.ifr_hwaddr.sa_family;
if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
const uint8_t mac[ETH_ADDR_LEN])
{
struct ifreq ifr;
+ int error;
memset(&ifr, 0, sizeof ifr);
ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
COVERAGE_INC(netdev_set_hwaddr);
- if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
+ error = af_inet_ioctl(SIOCSIFHWADDR, &ifr);
+ if (error) {
VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
- netdev_name, ovs_strerror(errno));
- return errno;
+ netdev_name, ovs_strerror(error));
}
- return 0;
+ return error;
}
static int
int cmd, const char *cmd_name)
{
struct ifreq ifr;
+ int error;
memset(&ifr, 0, sizeof ifr);
ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
ifr.ifr_data = (caddr_t) ecmd;
ecmd->cmd = cmd;
- if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) {
- return 0;
- } else {
- if (errno != EOPNOTSUPP) {
+ error = af_inet_ioctl(SIOCETHTOOL, &ifr);
+ if (error) {
+ if (error != EOPNOTSUPP) {
VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
- "failed: %s", cmd_name, name, ovs_strerror(errno));
+ "failed: %s", cmd_name, name, ovs_strerror(error));
} else {
/* The device doesn't support this operation. That's pretty
* common, so there's no point in logging anything. */
}
- return errno;
- }
-}
-
-static int
-netdev_linux_do_ioctl(const char *name, struct ifreq *ifr, int cmd,
- const char *cmd_name)
-{
- ovs_strzcpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
- if (ioctl(af_inet_sock, cmd, ifr) == -1) {
- VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
- ovs_strerror(errno));
- return errno;
}
- return 0;
+ return error;
}
static int
int error;
ifr.ifr_addr.sa_family = AF_INET;
- error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
+ error = af_inet_ifreq_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
if (!error) {
const struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *,
&ifr.ifr_addr);