netdev-pltap: Make access to AF_INET socket thread-safe.
[sliver-openvswitch.git] / lib / netdev-pltap.c
index cfa3dbb..8f4a67a 100644 (file)
@@ -70,8 +70,6 @@ struct netdev_rx_pltap {
     int fd;
 };
 
-static int af_inet_sock = -1;
-
 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
 
 static struct shash pltap_netdevs = SHASH_INITIALIZER(&pltap_netdevs);
@@ -149,7 +147,7 @@ netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *nam
     netdev->fd = tun_alloc(IFF_TAP, netdev->real_name);
     if (netdev->fd < 0) {
         error = errno;
-        VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", name, strerror(error));
+        VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", name, ovs_strerror(error));
         goto cleanup;
     }
     VLOG_DBG("real_name = %s", netdev->real_name);
@@ -249,13 +247,13 @@ static int vsys_transaction(const char *script,
 
     ofd = open(ofname, O_RDONLY | O_NONBLOCK);
     if (ofd < 0) {
-        VLOG_ERR("Cannot open %s: %s", ofname, strerror(errno));
+        VLOG_ERR("Cannot open %s: %s", ofname, ovs_strerror(errno));
        error = errno;
        goto cleanup;
     }
     ifd = open(ifname, O_WRONLY | O_NONBLOCK);
     if (ifd < 0) {
-        VLOG_ERR("Cannot open %s: %s", ifname, strerror(errno));
+        VLOG_ERR("Cannot open %s: %s", ifname, ovs_strerror(errno));
        error = errno;
        goto cleanup;
     }
@@ -278,7 +276,7 @@ static int vsys_transaction(const char *script,
        if (select(maxfd + 1, &readset, &writeset, &errorset, NULL) < 0) {
            if (errno == EINTR)
                continue;
-           VLOG_ERR("selec error: %s", strerror(errno));
+           VLOG_ERR("selec error: %s", ovs_strerror(errno));
            error = errno;
            goto cleanup;
        }
@@ -290,7 +288,7 @@ static int vsys_transaction(const char *script,
            ssize_t n = write(ifd, msg + bytes_written, bytes_to_write);    
            if (n < 0) {
                if (errno != EAGAIN && errno != EINTR) {
-                   VLOG_ERR("write on %s: %s", ifname, strerror(errno));
+                   VLOG_ERR("write on %s: %s", ifname, ovs_strerror(errno));
                    error = errno;
                    goto cleanup;
                }
@@ -305,7 +303,7 @@ static int vsys_transaction(const char *script,
            ssize_t n = read(ofd, reply + bytes_read, bytes_to_read);    
            if (n < 0) {
                if (errno != EAGAIN && errno != EINTR) {
-                   VLOG_ERR("read on %s: %s", ofname, strerror(errno));
+                   VLOG_ERR("read on %s: %s", ofname, ovs_strerror(errno));
                    error = errno;
                    goto cleanup;
                }
@@ -456,9 +454,9 @@ static int
 netdev_rx_pltap_recv(struct netdev_rx *rx_, void *buffer, size_t size)
 {
     struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_);
-    char prefix[4];
+    struct tun_pi pi;
     struct iovec iov[2] = {
-        { .iov_base = prefix, .iov_len = 4 },
+        { .iov_base = &pi, .iov_len = sizeof(pi) },
        { .iov_base = buffer, .iov_len = size }
     };
     for (;;) {
@@ -473,7 +471,7 @@ netdev_rx_pltap_recv(struct netdev_rx *rx_, void *buffer, size_t size)
         } else if (errno != EINTR) {
             if (errno != EAGAIN) {
                 VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
-                    netdev_rx_get_name(rx_), strerror(errno));
+                    netdev_rx_get_name(rx_), ovs_strerror(errno));
             }
             return -errno;
         }
@@ -496,9 +494,9 @@ netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
 {
     struct netdev_pltap *dev = 
        netdev_pltap_cast(netdev_);
-    char prefix[4] = { 0, 0, 8, 6 };
+    struct tun_pi pi = { 0, 0x86 };
     struct iovec iov[2] = {
-        { .iov_base = prefix, .iov_len = 4 },
+        { .iov_base = &pi, .iov_len = sizeof(pi) },
        { .iov_base = (char*) buffer, .iov_len = size }
     };
     if (dev->fd < 0)
@@ -515,7 +513,7 @@ netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size)
         } else if (errno != EINTR) {
             if (errno != EAGAIN) {
                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
-                    netdev_get_name(netdev_), strerror(errno));
+                    netdev_get_name(netdev_), ovs_strerror(errno));
             }
             return errno;
         }
@@ -567,16 +565,13 @@ get_etheraddr(struct netdev_pltap *dev, uint8_t ea[ETH_ADDR_LEN])
 {
     struct ifreq ifr;
     int hwaddr_family;
+    int error;
 
     memset(&ifr, 0, sizeof ifr);
     ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name);
-    if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
-        /* 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,
-             "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
-             dev->real_name, strerror(errno));
+    error = af_inet_ifreq_ioctl(dev->real_name, &ifr,
+        SIOCGIFHWADDR, "SIOCGIFHWADDR");
+    if (error) {
         return errno;
     }
     hwaddr_family = ifr.ifr_hwaddr.sa_family;
@@ -592,11 +587,13 @@ static int
 get_flags(struct netdev_pltap *dev, enum netdev_flags *flags)
 {
     struct ifreq ifr;
+    int error;
 
-    memset(&ifr, 0, sizeof ifr);
-    ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name);
-    if (ioctl(af_inet_sock, SIOCGIFFLAGS, &ifr) < 0)
-       return errno;
+    error = af_inet_ifreq_ioctl(dev->real_name, &ifr,
+        SIOCGIFFLAGS, "SIOCGIFFLAGS");
+    if (error) {
+        return error;
+    }
     *flags = 0;
     if (ifr.ifr_flags & IFF_UP)
        *flags |= NETDEV_UP;
@@ -698,10 +695,6 @@ static int
 netdev_pltap_init(void)
 {
     list_init(&sync_list);
-    af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
-    if (af_inet_sock < 0) {
-        VLOG_ERR("failed to create inet socket: %s", strerror(errno));
-    }
     unixctl_command_register("netdev-pltap/get-tapname", "port",
                              1, 1, netdev_pltap_get_real_name, NULL);
     return 0;