X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetdev.c;h=ba11b6abdb766e0e8cbda66a91cbc3a15a601ee5;hb=a52b6f66fd57dbc592db74f4347a2d2443ef2e75;hp=1e3fd55f18817b7f1d3e22a0848fed702ea706a9;hpb=735683ac615ca00ec62aff107ab1a07df037f543;p=sliver-openvswitch.git diff --git a/lib/netdev.c b/lib/netdev.c index 1e3fd55f1..ba11b6abd 100644 --- a/lib/netdev.c +++ b/lib/netdev.c @@ -31,6 +31,7 @@ * derivatives without specific, written prior permission. */ +#include #include "netdev.h" #include @@ -40,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -48,17 +50,19 @@ #include #include #include +#include #include #include #include #include -#include "list.h" #include "fatal-signal.h" -#include "buffer.h" +#include "list.h" +#include "ofpbuf.h" #include "openflow.h" #include "packets.h" #include "poll-loop.h" +#include "socket-util.h" #define THIS_MODULE VLM_netdev #include "vlog.h" @@ -66,59 +70,56 @@ struct netdev { struct list node; char *name; + int ifindex; int fd; uint8_t etheraddr[ETH_ADDR_LEN]; int speed; int mtu; - uint32_t features; - int save_flags; -}; -static struct list netdev_list = LIST_INITIALIZER(&netdev_list); + /* Bitmaps of OFPPF_* that describe features. All bits disabled if + * unsupported or unavailable. */ + uint32_t curr; /* Current features. */ + uint32_t advertised; /* Features being advertised by the port. */ + uint32_t supported; /* Features supported by the port. */ + uint32_t peer; /* Features advertised by the peer. */ -static void init_netdev(void); -static int restore_flags(struct netdev *netdev); + struct in6_addr in6; + int save_flags; /* Initial device flags. */ + int changed_flags; /* Flags that we changed. */ +}; -/* Check whether device NAME has an IPv4 address assigned to it and, if so, log - * an error. */ -static void -check_ipv4_address(const char *name) -{ - int sock; - struct ifreq ifr; +/* All open network devices. */ +static struct list netdev_list = LIST_INITIALIZER(&netdev_list); - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) { - VLOG_WARN("socket(AF_INET): %s", strerror(errno)); - return; - } +/* An AF_INET socket (used for ioctl operations). */ +static int af_inet_sock = -1; - strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); - ifr.ifr_addr.sa_family = AF_INET; - if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) { - VLOG_ERR("%s device has assigned IP address %s", name, - inet_ntoa(((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr)); - } +/* 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); - close(sock); -} +static void init_netdev(void); +static int restore_flags(struct netdev *netdev); +static int get_flags(const struct netdev *, int *flagsp); +static int set_flags(struct netdev *, int flags); -/* Check whether device NAME has an IPv6 address assigned to it and, if so, log - * an error. */ +/* Obtains the IPv6 address for 'name' into 'in6'. */ static void -check_ipv6_address(const char *name) +get_ipv6_address(const char *name, struct in6_addr *in6) { FILE *file; char line[128]; file = fopen("/proc/net/if_inet6", "r"); if (file == NULL) { + /* This most likely indicates that the host doesn't have IPv6 support, + * so it's not really a failure condition.*/ + *in6 = in6addr_any; return; } while (fgets(line, sizeof line, file)) { - struct in6_addr in6; - uint8_t *s6 = in6.s6_addr; + uint8_t *s6 = in6->s6_addr; char ifname[16 + 1]; #define X8 "%2"SCNx8 @@ -131,12 +132,10 @@ check_ipv6_address(const char *name) ifname) == 17 && !strcmp(name, ifname)) { - char in6_name[INET6_ADDRSTRLEN + 1]; - inet_ntop(AF_INET6, &in6, in6_name, sizeof in6_name); - VLOG_ERR("%s device has assigned IPv6 address %s", - name, in6_name); + return; } } + *in6 = in6addr_any; fclose(file); } @@ -147,8 +146,10 @@ do_ethtool(struct netdev *netdev) struct ifreq ifr; struct ethtool_cmd ecmd; - netdev->speed = 0; - netdev->features = 0; + netdev->curr = 0; + netdev->supported = 0; + netdev->advertised = 0; + netdev->peer = 0; memset(&ifr, 0, sizeof ifr); strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name); @@ -158,48 +159,107 @@ do_ethtool(struct netdev *netdev) ecmd.cmd = ETHTOOL_GSET; if (ioctl(netdev->fd, SIOCETHTOOL, &ifr) == 0) { if (ecmd.supported & SUPPORTED_10baseT_Half) { - netdev->features |= OFPPF_10MB_HD; + netdev->supported |= OFPPF_10MB_HD; } if (ecmd.supported & SUPPORTED_10baseT_Full) { - netdev->features |= OFPPF_10MB_FD; + netdev->supported |= OFPPF_10MB_FD; } if (ecmd.supported & SUPPORTED_100baseT_Half) { - netdev->features |= OFPPF_100MB_HD; + netdev->supported |= OFPPF_100MB_HD; } if (ecmd.supported & SUPPORTED_100baseT_Full) { - netdev->features |= OFPPF_100MB_FD; + netdev->supported |= OFPPF_100MB_FD; } if (ecmd.supported & SUPPORTED_1000baseT_Half) { - netdev->features |= OFPPF_1GB_HD; + netdev->supported |= OFPPF_1GB_HD; } if (ecmd.supported & SUPPORTED_1000baseT_Full) { - netdev->features |= OFPPF_1GB_FD; + netdev->supported |= OFPPF_1GB_FD; } - /* 10Gbps half-duplex doesn't exist... */ if (ecmd.supported & SUPPORTED_10000baseT_Full) { - netdev->features |= OFPPF_10GB_FD; + netdev->supported |= OFPPF_10GB_FD; } + if (ecmd.supported & SUPPORTED_TP) { + netdev->supported |= OFPPF_COPPER; + } + if (ecmd.supported & SUPPORTED_FIBRE) { + netdev->supported |= OFPPF_FIBER; + } + if (ecmd.supported & SUPPORTED_Autoneg) { + netdev->supported |= OFPPF_AUTONEG; + } +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14) + if (ecmd.supported & SUPPORTED_Pause) { + netdev->supported |= OFPPF_PAUSE; + } + if (ecmd.supported & SUPPORTED_Asym_Pause) { + netdev->supported |= OFPPF_PAUSE_ASYM; + } +#endif /* kernel >= 2.6.14 */ - switch (ecmd.speed) { - case SPEED_10: - netdev->speed = 10; - break; - - case SPEED_100: - netdev->speed = 100; - break; + /* Set the advertised features */ + if (ecmd.advertising & ADVERTISED_10baseT_Half) { + netdev->advertised |= OFPPF_10MB_HD; + } + if (ecmd.advertising & ADVERTISED_10baseT_Full) { + netdev->advertised |= OFPPF_10MB_FD; + } + if (ecmd.advertising & ADVERTISED_100baseT_Half) { + netdev->advertised |= OFPPF_100MB_HD; + } + if (ecmd.advertising & ADVERTISED_100baseT_Full) { + netdev->advertised |= OFPPF_100MB_FD; + } + if (ecmd.advertising & ADVERTISED_1000baseT_Half) { + netdev->advertised |= OFPPF_1GB_HD; + } + if (ecmd.advertising & ADVERTISED_1000baseT_Full) { + netdev->advertised |= OFPPF_1GB_FD; + } + if (ecmd.advertising & ADVERTISED_10000baseT_Full) { + netdev->advertised |= OFPPF_10GB_FD; + } + if (ecmd.advertising & ADVERTISED_TP) { + netdev->advertised |= OFPPF_COPPER; + } + if (ecmd.advertising & ADVERTISED_FIBRE) { + netdev->advertised |= OFPPF_FIBER; + } + if (ecmd.advertising & ADVERTISED_Autoneg) { + netdev->advertised |= OFPPF_AUTONEG; + } +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14) + if (ecmd.advertising & ADVERTISED_Pause) { + netdev->advertised |= OFPPF_PAUSE; + } + if (ecmd.advertising & ADVERTISED_Asym_Pause) { + netdev->advertised |= OFPPF_PAUSE_ASYM; + } +#endif /* kernel >= 2.6.14 */ - case SPEED_1000: - netdev->speed = 1000; - break; + /* Set the current features */ + if (ecmd.speed == SPEED_10) { + netdev->curr = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD; + } + else if (ecmd.speed == SPEED_100) { + netdev->curr = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD; + } + else if (ecmd.speed == SPEED_1000) { + netdev->curr = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD; + } + else if (ecmd.speed == SPEED_10000) { + netdev->curr = OFPPF_10GB_FD; + } - case SPEED_2500: - netdev->speed = 2500; - break; + if (ecmd.port == PORT_TP) { + netdev->curr |= OFPPF_COPPER; + } + else if (ecmd.port == PORT_FIBRE) { + netdev->curr |= OFPPF_FIBER; + } - case SPEED_10000: - netdev->speed = 10000; - break; + if (ecmd.autoneg) { + netdev->curr |= OFPPF_AUTONEG; } } else { VLOG_DBG("ioctl(SIOCETHTOOL) failed: %s", strerror(errno)); @@ -208,17 +268,21 @@ do_ethtool(struct netdev *netdev) /* Opens the network device named 'name' (e.g. "eth0") and returns zero if * successful, otherwise a positive errno value. On success, sets '*netdev' - * to the new network device, otherwise to null. */ + * to the new network device, otherwise to null. + * + * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to + * capture frames of that type received on the device. It may also be one of + * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those + * categories. */ int -netdev_open(const char *name, struct netdev **netdev_) +netdev_open(const char *name, int ethertype, struct netdev **netdev_) { int fd; - struct sockaddr sa; + struct sockaddr_ll sll; struct ifreq ifr; unsigned int ifindex; - socklen_t rcvbuf_len; - size_t rcvbuf; uint8_t etheraddr[ETH_ADDR_LEN]; + struct in6_addr in6; int mtu; int error; struct netdev *netdev; @@ -226,47 +290,16 @@ netdev_open(const char *name, struct netdev **netdev_) *netdev_ = NULL; init_netdev(); - /* Create raw socket. - * - * We have to use SOCK_PACKET, despite its deprecation, because only - * SOCK_PACKET lets us set the hardware source address of outgoing - * packets. */ - fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL)); + /* Create raw socket. */ + fd = socket(PF_PACKET, SOCK_RAW, + htons(ethertype == NETDEV_ETH_TYPE_NONE ? 0 + : ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL + : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2 + : ethertype)); if (fd < 0) { return errno; } - /* Bind to specific ethernet device. */ - memset(&sa, 0, sizeof sa); - sa.sa_family = AF_UNSPEC; - strncpy((char *) sa.sa_data, name, sizeof sa.sa_data); - if (bind(fd, &sa, sizeof sa) < 0) { - VLOG_ERR("bind to %s failed: %s", name, strerror(errno)); - goto error; - } - - /* Between the socket() and bind() calls above, the socket receives all - * packets on all system interfaces. We do not want to receive that - * data, but there is no way to avoid it. So we must now drain out the - * receive queue. There is no way to know how long the receive queue is, - * but we know that the total number of bytes queued does not exceed the - * receive buffer size, so we pull packets until none are left or we've - * read that many bytes. */ - rcvbuf_len = sizeof rcvbuf; - if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &rcvbuf_len) < 0) { - VLOG_ERR("getsockopt(SO_RCVBUF) on %s device failed: %s", - name, strerror(errno)); - goto error; - } - while (rcvbuf > 0) { - char buffer; - ssize_t n_bytes = recv(fd, &buffer, 1, MSG_TRUNC | MSG_DONTWAIT); - if (n_bytes <= 0) { - break; - } - rcvbuf -= n_bytes; - } - /* Get ethernet device index. */ strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) { @@ -276,6 +309,26 @@ netdev_open(const char *name, struct netdev **netdev_) } ifindex = ifr.ifr_ifindex; + /* Bind to specific ethernet device. */ + memset(&sll, 0, sizeof sll); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = ifindex; + if (bind(fd, (struct sockaddr *) &sll, sizeof sll) < 0) { + VLOG_ERR("bind to %s failed: %s", name, strerror(errno)); + goto error; + } + + if (ethertype != NETDEV_ETH_TYPE_NONE) { + /* Between the socket() and bind() calls above, the socket receives all + * packets of the requested type on all system interfaces. We do not + * want to receive that data, but there is no way to avoid it. So we + * must now drain out the receive queue. */ + error = drain_rcvbuf(fd); + if (error) { + goto error; + } + } + /* Get MAC address. */ if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s", @@ -297,50 +350,37 @@ netdev_open(const char *name, struct netdev **netdev_) } mtu = ifr.ifr_mtu; + get_ipv6_address(name, &in6); + /* Allocate network device. */ netdev = xmalloc(sizeof *netdev); netdev->name = xstrdup(name); + netdev->ifindex = ifindex; netdev->fd = fd; memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr); netdev->mtu = mtu; + netdev->in6 = in6; /* Get speed, features. */ do_ethtool(netdev); /* Save flags to restore at close or exit. */ - if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) { - VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s", - name, strerror(errno)); - goto error; + error = get_flags(netdev, &netdev->save_flags); + if (error) { + goto preset_error; } - netdev->save_flags = ifr.ifr_flags; + netdev->changed_flags = 0; fatal_signal_block(); list_push_back(&netdev_list, &netdev->node); fatal_signal_unblock(); - /* Bring up interface and set promiscuous mode. */ - ifr.ifr_flags |= IFF_PROMISC | IFF_UP; - if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) { - error = errno; - VLOG_ERR("failed to set promiscuous mode on %s device: %s", - name, strerror(errno)); - netdev_close(netdev); - return error; - } - - /* Complain to administrator if any IP addresses are assigned to the - * interface. We warn about this because packets received for that IP - * address will be processed both by the kernel TCP/IP stack and by us as a - * switch, which produces poor results. */ - check_ipv4_address(name); - check_ipv6_address(name); - /* Success! */ *netdev_ = netdev; return 0; error: error = errno; +preset_error: close(fd); return error; } @@ -372,11 +412,11 @@ netdev_close(struct netdev *netdev) /* Pads 'buffer' out with zero-bytes to the minimum valid length of an * Ethernet packet, if necessary. */ static void -pad_to_minimum_length(struct buffer *buffer) +pad_to_minimum_length(struct ofpbuf *buffer) { if (buffer->size < ETH_TOTAL_MIN) { size_t shortage = ETH_TOTAL_MIN - buffer->size; - memset(buffer_put_uninit(buffer, shortage), 0, shortage); + memset(ofpbuf_put_uninit(buffer, shortage), 0, shortage); } } @@ -393,21 +433,21 @@ pad_to_minimum_length(struct buffer *buffer) * be returned. */ int -netdev_recv(struct netdev *netdev, struct buffer *buffer) +netdev_recv(struct netdev *netdev, struct ofpbuf *buffer) { ssize_t n_bytes; assert(buffer->size == 0); - assert(buffer_tailroom(buffer) >= ETH_TOTAL_MIN); + assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN); do { n_bytes = recv(netdev->fd, - buffer_tail(buffer), buffer_tailroom(buffer), + ofpbuf_tail(buffer), ofpbuf_tailroom(buffer), MSG_DONTWAIT); } while (n_bytes < 0 && errno == EINTR); if (n_bytes < 0) { if (errno != EAGAIN) { - VLOG_WARN("error receiving Ethernet packet on %s: %s", - strerror(errno), netdev->name); + VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s", + strerror(errno), netdev->name); } return errno; } else { @@ -431,37 +471,38 @@ netdev_recv_wait(struct netdev *netdev) poll_fd_wait(netdev->fd, POLLIN); } +/* Discards all packets waiting to be received from 'netdev'. */ +void +netdev_drain(struct netdev *netdev) +{ + drain_rcvbuf(netdev->fd); +} + /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive * errno value. Returns EAGAIN without blocking if the packet cannot be queued * immediately. Returns EMSGSIZE if a partial packet was transmitted or if - * the packet is too big to transmit on the device. + * the packet is too big or too small to transmit on the device. + * + * The caller retains ownership of 'buffer' in all cases. * * The kernel maintains a packet transmission queue, so the caller is not * expected to do additional queuing of packets. */ int -netdev_send(struct netdev *netdev, struct buffer *buffer) +netdev_send(struct netdev *netdev, const struct ofpbuf *buffer) { ssize_t n_bytes; const struct eth_header *eh; - struct sockaddr_pkt spkt; - - /* Ensure packet is long enough. (Although all incoming packets are at - * least ETH_TOTAL_MIN bytes long, we could have trimmed some data off a - * minimum-size packet, e.g. by dropping a vlan header.) - * - * The kernel does not require this, but it ensures that we always access - * valid memory in grabbing the sockaddr below. */ - pad_to_minimum_length(buffer); - - /* Construct packet sockaddr, which SOCK_PACKET requires. */ - spkt.spkt_family = AF_PACKET; - strncpy((char *) spkt.spkt_device, netdev->name, sizeof spkt.spkt_device); - eh = buffer_at_assert(buffer, 0, sizeof *eh); - spkt.spkt_protocol = eh->eth_type; + + /* Pull out the Ethernet header. */ + if (buffer->size < ETH_HEADER_LEN) { + VLOG_WARN_RL(&rl, "cannot send %zu-byte frame on %s", + buffer->size, netdev->name); + return EMSGSIZE; + } + eh = ofpbuf_at_assert(buffer, 0, sizeof *eh); do { - n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0, - (const struct sockaddr *) &spkt, sizeof spkt); + n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0, NULL, 0); } while (n_bytes < 0 && errno == EINTR); if (n_bytes < 0) { @@ -471,13 +512,14 @@ netdev_send(struct netdev *netdev, struct buffer *buffer) if (errno == ENOBUFS) { return EAGAIN; } else if (errno != EAGAIN) { - VLOG_WARN("error sending Ethernet packet on %s: %s", - netdev->name, strerror(errno)); + VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s", + netdev->name, strerror(errno)); } return errno; } else if (n_bytes != buffer->size) { - VLOG_WARN("send partial Ethernet packet (%d bytes of %zu) on %s", - (int) n_bytes, buffer->size, netdev->name); + VLOG_WARN_RL(&rl, + "send partial Ethernet packet (%d bytes of %zu) on %s", + (int) n_bytes, buffer->size, netdev->name); return EMSGSIZE; } else { return 0; @@ -522,20 +564,274 @@ netdev_get_mtu(const struct netdev *netdev) return netdev->mtu; } -/* Returns the current speed of the network device that 'netdev' represents, in - * megabits per second, or 0 if the speed is unknown. */ +/* Checks the link status. Returns 1 or 0 to indicate the link is active + * or not, respectively. Any other return value indicates an error. */ int -netdev_get_speed(const struct netdev *netdev) +netdev_get_link_status(const struct netdev *netdev) { - return netdev->speed; + struct ifreq ifr; + struct ethtool_value edata; + + memset(&ifr, 0, sizeof ifr); + strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name); + ifr.ifr_data = (caddr_t) &edata; + + memset(&edata, 0, sizeof edata); + edata.cmd = ETHTOOL_GLINK; + if (ioctl(netdev->fd, SIOCETHTOOL, &ifr) == 0) { + if (edata.data) { + return 1; + } else { + return 0; + } + } + + return -1; } -/* Returns the features supported by 'netdev', as a bitmap of bits from enum - * ofp_phy_port, in host byte order. */ +/* Returns the features supported by 'netdev' of type 'type', as a bitmap + * of bits from enum ofp_phy_features, in host byte order. */ uint32_t -netdev_get_features(const struct netdev *netdev) +netdev_get_features(struct netdev *netdev, int type) +{ + do_ethtool(netdev); + switch (type) { + case NETDEV_FEAT_CURRENT: + return netdev->curr; + case NETDEV_FEAT_ADVERTISED: + return netdev->advertised; + case NETDEV_FEAT_SUPPORTED: + return netdev->supported; + case NETDEV_FEAT_PEER: + return netdev->peer; + default: + VLOG_WARN("Unknown feature type: %d\n", type); + return 0; + } +} + +/* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if + * 'in4' is non-null) and returns true. Otherwise, returns false. */ +bool +netdev_get_in4(const struct netdev *netdev, struct in_addr *in4) +{ + struct ifreq ifr; + struct in_addr ip = { INADDR_ANY }; + + strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name); + ifr.ifr_addr.sa_family = AF_INET; + if (ioctl(af_inet_sock, SIOCGIFADDR, &ifr) == 0) { + struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr; + ip = sin->sin_addr; + } else { + VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFADDR) failed: %s", + netdev->name, strerror(errno)); + } + if (in4) { + *in4 = ip; + } + return ip.s_addr != INADDR_ANY; +} + +static void +make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr) +{ + struct sockaddr_in sin; + memset(&sin, 0, sizeof sin); + sin.sin_family = AF_INET; + sin.sin_addr = addr; + sin.sin_port = 0; + + memset(sa, 0, sizeof *sa); + memcpy(sa, &sin, sizeof sin); +} + +static int +do_set_addr(struct netdev *netdev, int sock, + int ioctl_nr, const char *ioctl_name, struct in_addr addr) +{ + struct ifreq ifr; + int error; + + strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name); + make_in4_sockaddr(&ifr.ifr_addr, addr); + error = ioctl(sock, ioctl_nr, &ifr) < 0 ? errno : 0; + if (error) { + VLOG_WARN("ioctl(%s): %s", ioctl_name, strerror(error)); + } + return error; +} + +/* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If + * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a + * positive errno value. */ +int +netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask) +{ + int error; + + error = do_set_addr(netdev, af_inet_sock, + SIOCSIFADDR, "SIOCSIFADDR", addr); + if (!error && addr.s_addr != INADDR_ANY) { + error = do_set_addr(netdev, af_inet_sock, + SIOCSIFNETMASK, "SIOCSIFNETMASK", mask); + } + return error; +} + +/* Adds 'router' as a default gateway for 'netdev''s IP address. */ +int +netdev_add_router(struct netdev *netdev, struct in_addr router) +{ + struct in_addr any = { INADDR_ANY }; + struct rtentry rt; + int error; + + memset(&rt, 0, sizeof rt); + make_in4_sockaddr(&rt.rt_dst, any); + 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; + if (error) { + VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error)); + } + return error; +} + +/* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if + * 'in6' is non-null) and returns true. Otherwise, returns false. */ +bool +netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6) +{ + if (in6) { + *in6 = netdev->in6; + } + return memcmp(&netdev->in6, &in6addr_any, sizeof netdev->in6) != 0; +} + +/* Obtains the current flags for 'netdev' and stores them into '*flagsp'. + * Returns 0 if successful, otherwise a positive errno value. */ +int +netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp) +{ + int error, flags; + + error = get_flags(netdev, &flags); + if (error) { + return error; + } + + *flagsp = 0; + if (flags & IFF_UP) { + *flagsp |= NETDEV_UP; + } + if (flags & IFF_PROMISC) { + *flagsp |= NETDEV_PROMISC; + } + return 0; +} + +static int +nd_to_iff_flags(enum netdev_flags nd) +{ + int iff = 0; + if (nd & NETDEV_UP) { + iff |= IFF_UP; + } + if (nd & NETDEV_PROMISC) { + iff |= IFF_PROMISC; + } + return iff; +} + +/* On 'netdev', turns off the flags in 'off' and then turns on the flags in + * 'on'. If 'permanent' is true, the changes will persist; otherwise, they + * will be reverted when 'netdev' is closed or the program exits. Returns 0 if + * successful, otherwise a positive errno value. */ +static int +do_update_flags(struct netdev *netdev, enum netdev_flags off, + enum netdev_flags on, bool permanent) +{ + int old_flags, new_flags; + int error; + + error = get_flags(netdev, &old_flags); + if (error) { + return error; + } + + new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on); + if (!permanent) { + netdev->changed_flags |= new_flags ^ old_flags; + } + if (new_flags != old_flags) { + error = set_flags(netdev, new_flags); + } + return error; +} + +/* Sets the flags for 'netdev' to 'flags'. + * If 'permanent' is true, the changes will persist; otherwise, they + * will be reverted when 'netdev' is closed or the program exits. + * Returns 0 if successful, otherwise a positive errno value. */ +int +netdev_set_flags(struct netdev *netdev, enum netdev_flags flags, + bool permanent) { - return netdev->features; + return do_update_flags(netdev, -1, flags, permanent); +} + +/* Turns on the specified 'flags' on 'netdev'. + * If 'permanent' is true, the changes will persist; otherwise, they + * will be reverted when 'netdev' is closed or the program exits. + * Returns 0 if successful, otherwise a positive errno value. */ +int +netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags, + bool permanent) +{ + return do_update_flags(netdev, 0, flags, permanent); +} + +/* Turns off the specified 'flags' on 'netdev'. + * If 'permanent' is true, the changes will persist; otherwise, they + * will be reverted when 'netdev' is closed or the program exits. + * Returns 0 if successful, otherwise a positive errno value. */ +int +netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags, + bool permanent) +{ + return do_update_flags(netdev, flags, 0, permanent); +} + +/* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be + * successfully retrieved, it stores the corresponding MAC address in 'mac' and + * returns 0. Otherwise, it returns a positive errno value; in particular, + * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */ +int +netdev_arp_lookup(const struct netdev *netdev, + uint32_t ip, uint8_t mac[ETH_ADDR_LEN]) +{ + struct arpreq r; + struct sockaddr_in *pa; + int retval; + + memset(&r, 0, sizeof r); + pa = (struct sockaddr_in *) &r.arp_pa; + pa->sin_family = AF_INET; + pa->sin_addr.s_addr = ip; + pa->sin_port = 0; + r.arp_ha.sa_family = ARPHRD_ETHER; + r.arp_flags = 0; + strncpy(r.arp_dev, netdev->name, sizeof r.arp_dev); + retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0; + if (!retval) { + memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN); + } else if (retval != ENXIO) { + VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s", + netdev->name, IP_ARGS(&ip), strerror(retval)); + } + return retval; } static void restore_all_flags(void *aux); @@ -548,7 +844,11 @@ init_netdev(void) static bool inited; if (!inited) { inited = true; - fatal_signal_add_hook(restore_all_flags, NULL); + fatal_signal_add_hook(restore_all_flags, NULL, true); + af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0); + if (af_inet_sock < 0) { + ofp_fatal(errno, "socket(AF_INET)"); + } } } @@ -561,6 +861,7 @@ static int restore_flags(struct netdev *netdev) { struct ifreq ifr; + int restore_flags; /* Get current flags. */ strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name); @@ -569,9 +870,10 @@ restore_flags(struct netdev *netdev) } /* Restore flags that we might have changed, if necessary. */ - if ((ifr.ifr_flags ^ netdev->save_flags) & (IFF_PROMISC | IFF_UP)) { - ifr.ifr_flags &= ~(IFF_PROMISC | IFF_UP); - ifr.ifr_flags |= netdev->save_flags & (IFF_PROMISC | IFF_UP); + restore_flags = netdev->changed_flags & (IFF_PROMISC | IFF_UP); + if ((ifr.ifr_flags ^ netdev->save_flags) & restore_flags) { + ifr.ifr_flags &= ~restore_flags; + ifr.ifr_flags |= netdev->save_flags & restore_flags; if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) { return errno; } @@ -590,3 +892,31 @@ restore_all_flags(void *aux UNUSED) restore_flags(netdev); } } + +static int +get_flags(const struct netdev *netdev, int *flags) +{ + struct ifreq ifr; + strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name); + if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) { + VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s", + netdev->name, strerror(errno)); + return errno; + } + *flags = ifr.ifr_flags; + return 0; +} + +static int +set_flags(struct netdev *netdev, int flags) +{ + struct ifreq ifr; + strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name); + ifr.ifr_flags = flags; + if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) { + VLOG_ERR("ioctl(SIOCSIFFLAGS) on %s device failed: %s", + netdev->name, strerror(errno)); + return errno; + } + return 0; +}