Get rid of per-port STP implemented bits, by reducing OFPP_MAX to 255.
[sliver-openvswitch.git] / lib / netdev.c
index 4eadc9c..7b55aec 100644 (file)
@@ -55,9 +55,9 @@
 #include <string.h>
 #include <unistd.h>
 
-#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"
@@ -75,41 +75,26 @@ struct netdev {
     int speed;
     int mtu;
     uint32_t features;
-    struct in_addr in4;
     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)
@@ -228,7 +213,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 +281,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 +290,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 +343,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 +364,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 +419,18 @@ 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);
+        VLOG_WARN_RL(&rl, "cannot send %zu-byte frame on %s",
+                     buffer->size, netdev->name);
         return EMSGSIZE;
     }
-    eh = buffer_at_assert(buffer, 0, sizeof *eh);
+    eh = ofpbuf_at_assert(buffer, 0, sizeof *eh);
 
     do {
         n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0, NULL, 0);
@@ -463,13 +443,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;
@@ -560,10 +541,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
@@ -605,12 +598,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;
 }
@@ -764,8 +754,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;
 }
@@ -780,10 +770,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)");
         }
     }
 }