X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetdev.c;h=3926fed5beb5e728706b247e5562ebd656b106e5;hb=1374258f0ae5beccd71a62f710dce263922f6dc3;hp=0cd3e6d442f4a638394f1c622f1f63eae9eb3afa;hpb=2d4b692ecd5aa17d023f233c8b62dacd0bcf8351;p=sliver-openvswitch.git diff --git a/lib/netdev.c b/lib/netdev.c index 0cd3e6d44..3926fed5b 100644 --- a/lib/netdev.c +++ b/lib/netdev.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -55,9 +56,9 @@ #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" @@ -74,42 +75,34 @@ struct netdev { uint8_t etheraddr[ETH_ADDR_LEN]; int speed; int mtu; - uint32_t features; - struct in_addr in4; + + /* 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. */ + struct in6_addr in6; int save_flags; /* Initial device flags. */ int changed_flags; /* Flags that we changed. */ }; +/* All open network devices. */ static struct list netdev_list = LIST_INITIALIZER(&netdev_list); /* An AF_INET socket (used for ioctl operations). */ static int af_inet_sock = -1; +/* 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 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); -/* Obtains the IPv4 address for 'name' into 'in4'. Returns true if - * successful. */ -static bool -get_ipv4_address(const char *name, struct in_addr *in4) -{ - struct ifreq ifr; - - strncpy(ifr.ifr_name, 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; - *in4 = sin->sin_addr; - } else { - in4->s_addr = INADDR_ANY; - } - - return true; -} - /* Obtains the IPv6 address for 'name' into 'in6'. */ static void get_ipv6_address(const char *name, struct in6_addr *in6) @@ -153,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); @@ -164,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)); @@ -228,7 +282,6 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_) struct ifreq ifr; unsigned int ifindex; uint8_t etheraddr[ETH_ADDR_LEN]; - struct in_addr in4; struct in6_addr in6; int mtu; int error; @@ -297,9 +350,6 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_) } mtu = ifr.ifr_mtu; - if (!get_ipv4_address(name, &in4)) { - goto error; - } get_ipv6_address(name, &in6); /* Allocate network device. */ @@ -309,7 +359,6 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_) netdev->fd = fd; memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr); netdev->mtu = mtu; - netdev->in4 = in4; netdev->in6 = in6; /* Get speed, features. */ @@ -363,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); } } @@ -384,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 { @@ -439,18 +488,9 @@ netdev_drain(struct netdev *netdev) * 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, const struct buffer *buffer) +netdev_send(struct netdev *netdev, const struct ofpbuf *buffer) { ssize_t n_bytes; - const struct eth_header *eh; - - /* Pull out the Ethernet header. */ - if (buffer->size < ETH_HEADER_LEN) { - VLOG_WARN("cannot send %zu-byte frame on %s", - buffer->size, netdev->name); - return EMSGSIZE; - } - eh = buffer_at_assert(buffer, 0, sizeof *eh); do { n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0, NULL, 0); @@ -463,13 +503,14 @@ netdev_send(struct netdev *netdev, const 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; @@ -514,20 +555,50 @@ 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) { - return netdev->features; + 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 @@ -535,10 +606,22 @@ netdev_get_features(const struct netdev *netdev) 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 = netdev->in4; + *in4 = ip; } - return netdev->in4.s_addr != INADDR_ANY; + return ip.s_addr != INADDR_ANY; } static void @@ -580,12 +663,9 @@ netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask) error = do_set_addr(netdev, af_inet_sock, SIOCSIFADDR, "SIOCSIFADDR", addr); - if (!error) { - netdev->in4 = addr; - if (addr.s_addr != INADDR_ANY) { - error = do_set_addr(netdev, af_inet_sock, - SIOCSIFNETMASK, "SIOCSIFNETMASK", mask); - } + if (!error && addr.s_addr != INADDR_ANY) { + error = do_set_addr(netdev, af_inet_sock, + SIOCSIFNETMASK, "SIOCSIFNETMASK", mask); } return error; } @@ -739,8 +819,8 @@ netdev_arp_lookup(const struct netdev *netdev, if (!retval) { memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN); } else if (retval != ENXIO) { - VLOG_WARN("%s: could not look up ARP entry for "IP_FMT": %s", - netdev->name, IP_ARGS(&ip), strerror(retval)); + VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s", + netdev->name, IP_ARGS(&ip), strerror(retval)); } return retval; } @@ -755,10 +835,10 @@ 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) { - fatal(errno, "socket(AF_INET)"); + ofp_fatal(errno, "socket(AF_INET)"); } } }