clang: Fix the alignment warning.
[sliver-openvswitch.git] / lib / netdev-linux.c
index 1f73886..ab4feae 100644 (file)
@@ -409,9 +409,6 @@ 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. */
 
-/* A Netlink routing socket that is not subscribed to any multicast groups. */
-static struct nl_sock *rtnl_sock;
-
 /* 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);
@@ -477,15 +474,6 @@ netdev_linux_init(void)
         if (status) {
             VLOG_ERR("failed to create inet socket: %s", ovs_strerror(status));
         }
-
-        /* Create rtnetlink socket. */
-        if (!status) {
-            status = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
-            if (status) {
-                VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",
-                            ovs_strerror(status));
-            }
-        }
     }
     return status;
 }
@@ -1352,11 +1340,13 @@ static int
 netdev_linux_sys_get_stats(const struct netdev *netdev_,
                          struct netdev_stats *stats)
 {
-    static int use_netlink_stats = -1;
+    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+    static int use_netlink_stats;
     int error;
 
-    if (use_netlink_stats < 0) {
+    if (ovsthread_once_start(&once)) {
         use_netlink_stats = check_for_working_netlink_stats();
+        ovsthread_once_done(&once);
     }
 
     if (use_netlink_stats) {
@@ -2025,7 +2015,7 @@ start_queue_dump(const struct netdev *netdev, struct nl_dump *dump)
         return false;
     }
     tcmsg->tcm_parent = 0;
-    nl_dump_start(dump, rtnl_sock, &request);
+    nl_dump_start(dump, NETLINK_ROUTE, &request);
     ofpbuf_uninit(&request);
     return true;
 }
@@ -3644,7 +3634,7 @@ tc_make_request(const struct netdev *netdev, int type, unsigned int flags,
 static int
 tc_transact(struct ofpbuf *request, struct ofpbuf **replyp)
 {
-    int error = nl_sock_transact(rtnl_sock, request, replyp);
+    int error = nl_transact(NETLINK_ROUTE, request, replyp);
     ofpbuf_uninit(request);
     return error;
 }
@@ -3789,30 +3779,35 @@ read_psched(void)
      * [5] 2.6.32.21.22 (approx.) from Ubuntu 10.04 on VMware Fusion
      * [6] 2.6.34 from kernel.org on KVM
      */
+    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
     static const char fn[] = "/proc/net/psched";
     unsigned int a, b, c, d;
     FILE *stream;
 
+    if (!ovsthread_once_start(&once)) {
+        return;
+    }
+
     ticks_per_s = 1.0;
     buffer_hz = 100;
 
     stream = fopen(fn, "r");
     if (!stream) {
         VLOG_WARN("%s: open failed: %s", fn, ovs_strerror(errno));
-        return;
+        goto exit;
     }
 
     if (fscanf(stream, "%x %x %x %x", &a, &b, &c, &d) != 4) {
         VLOG_WARN("%s: read failed", fn);
         fclose(stream);
-        return;
+        goto exit;
     }
     VLOG_DBG("%s: psched parameters are: %u %u %u %u", fn, a, b, c, d);
     fclose(stream);
 
     if (!a || !c) {
         VLOG_WARN("%s: invalid scheduler parameters", fn);
-        return;
+        goto exit;
     }
 
     ticks_per_s = (double) a * c / b;
@@ -3823,6 +3818,9 @@ read_psched(void)
                   fn, a, b, c, d);
     }
     VLOG_DBG("%s: ticks_per_s=%f buffer_hz=%u", fn, ticks_per_s, buffer_hz);
+
+exit:
+    ovsthread_once_done(&once);
 }
 
 /* Returns the number of bytes that can be transmitted in 'ticks' ticks at a
@@ -3830,9 +3828,7 @@ read_psched(void)
 static unsigned int
 tc_ticks_to_bytes(unsigned int rate, unsigned int ticks)
 {
-    if (!buffer_hz) {
-        read_psched();
-    }
+    read_psched();
     return (rate * ticks) / ticks_per_s;
 }
 
@@ -3841,9 +3837,7 @@ tc_ticks_to_bytes(unsigned int rate, unsigned int ticks)
 static unsigned int
 tc_bytes_to_ticks(unsigned int rate, unsigned int size)
 {
-    if (!buffer_hz) {
-        read_psched();
-    }
+    read_psched();
     return rate ? ((unsigned long long int) ticks_per_s * size) / rate : 0;
 }
 
@@ -3852,9 +3846,7 @@ tc_bytes_to_ticks(unsigned int rate, unsigned int size)
 static unsigned int
 tc_buffer_per_jiffy(unsigned int rate)
 {
-    if (!buffer_hz) {
-        read_psched();
-    }
+    read_psched();
     return rate / buffer_hz;
 }
 
@@ -4320,7 +4312,7 @@ get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
     ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
     ifi->ifi_family = PF_UNSPEC;
     ifi->ifi_index = ifindex;
-    error = nl_sock_transact(rtnl_sock, &request, &reply);
+    error = nl_transact(NETLINK_ROUTE, &request, &reply);
     ofpbuf_uninit(&request);
     if (error) {
         return error;
@@ -4558,7 +4550,8 @@ netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
     ifr.ifr_addr.sa_family = AF_INET;
     error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
     if (!error) {
-        const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
+        const struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *,
+                                                     &ifr.ifr_addr);
         *ip = sin->sin_addr;
     }
     return error;
@@ -4568,9 +4561,10 @@ netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
 static int
 af_packet_sock(void)
 {
-    static int sock = INT_MIN;
+    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+    static int sock;
 
-    if (sock == INT_MIN) {
+    if (ovsthread_once_start(&once)) {
         sock = socket(AF_PACKET, SOCK_RAW, 0);
         if (sock >= 0) {
             int error = set_nonblocking(sock);
@@ -4583,6 +4577,7 @@ af_packet_sock(void)
             VLOG_ERR("failed to create packet socket: %s",
                      ovs_strerror(errno));
         }
+        ovsthread_once_done(&once);
     }
 
     return sock;