refactor for new netdev_rx class
[sliver-openvswitch.git] / lib / netdev-tunnel.c
index 6bbf2cb..18242a6 100644 (file)
@@ -41,7 +41,6 @@ VLOG_DEFINE_THIS_MODULE(netdev_tunnel);
 struct netdev_dev_tunnel {
     struct netdev_dev netdev_dev;
     uint8_t hwaddr[ETH_ADDR_LEN];
-    int mtu;
     struct netdev_stats stats;
     enum netdev_flags flags;
     int sockfd;
@@ -57,14 +56,23 @@ struct netdev_tunnel {
     struct netdev netdev;
 };
 
+struct netdev_rx_tunnel {
+    struct netdev_rx up;
+    int fd;
+};
+
+static const struct netdev_rx_class netdev_rx_tunnel_class;
+
+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
+
 static struct shash tunnel_netdev_devs = SHASH_INITIALIZER(&tunnel_netdev_devs);
 
 static int netdev_tunnel_create(const struct netdev_class *, const char *,
                                struct netdev_dev **);
-static void netdev_tunnel_poll_notify(const struct netdev *);
+static void netdev_tunnel_update_seq(struct netdev_dev_tunnel *);
 
 static bool
-is_tunnel_class(const struct netdev_class *class)
+is_netdev_tunnel_class(const struct netdev_class *class)
 {
     return class->create == netdev_tunnel_create;
 }
@@ -72,7 +80,7 @@ is_tunnel_class(const struct netdev_class *class)
 static struct netdev_dev_tunnel *
 netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
 {
-    assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
+    ovs_assert(is_netdev_tunnel_class(netdev_dev_get_class(netdev_dev)));
     return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
 }
 
@@ -80,27 +88,33 @@ static struct netdev_tunnel *
 netdev_tunnel_cast(const struct netdev *netdev)
 {
     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
-    assert(is_tunnel_class(netdev_dev_get_class(netdev_dev)));
+    ovs_assert(is_netdev_tunnel_class(netdev_dev_get_class(netdev_dev)));
     return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
 }
 
+static struct netdev_rx_tunnel *
+netdev_rx_tunnel_cast(const struct netdev_rx *rx)
+{
+    netdev_rx_assert_class(rx, &netdev_rx_tunnel_class);
+    return CONTAINER_OF(rx, struct netdev_rx_tunnel, up);
+}
+
 static int
 netdev_tunnel_create(const struct netdev_class *class, const char *name,
                     struct netdev_dev **netdev_devp)
 {
-    static unsigned int n = 0xaa550000;
+    static unsigned int n = 0;
     struct netdev_dev_tunnel *netdev_dev;
     int error;
 
     netdev_dev = xzalloc(sizeof *netdev_dev);
     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
-    netdev_dev->hwaddr[0] = 0x55;
-    netdev_dev->hwaddr[1] = 0xaa;
-    netdev_dev->hwaddr[2] = n >> 24;
+    netdev_dev->hwaddr[0] = 0xfe;
+    netdev_dev->hwaddr[1] = 0xff;
+    netdev_dev->hwaddr[2] = 0xff;
     netdev_dev->hwaddr[3] = n >> 16;
     netdev_dev->hwaddr[4] = n >> 8;
     netdev_dev->hwaddr[5] = n;
-    netdev_dev->mtu = 1500;
     netdev_dev->flags = 0;
     netdev_dev->change_seq = 1;
     memset(&netdev_dev->remote_addr, 0, sizeof(netdev_dev->remote_addr));
@@ -147,7 +161,6 @@ netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
 static int
 netdev_tunnel_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
 {
-    struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
     struct netdev_tunnel *netdev;
 
     netdev = xmalloc(sizeof *netdev);
@@ -165,16 +178,16 @@ netdev_tunnel_close(struct netdev *netdev_)
 }
 
 static int
-netdev_tunnel_get_config(struct netdev_dev *dev_, struct shash *args)
+netdev_tunnel_get_config(struct netdev_dev *dev_, struct smap *args)
 {
     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
 
     if (netdev_dev->valid_remote_ip)
-       shash_add(args, "remote_ip",
-           xasprintf(IP_FMT, IP_ARGS(&netdev_dev->remote_addr.sin_addr)));
+       smap_add_format(args, "remote_ip", IP_FMT,
+               IP_ARGS(netdev_dev->remote_addr.sin_addr.s_addr));
     if (netdev_dev->valid_remote_port)
-        shash_add(args, "remote_port",
-           xasprintf("%"PRIu16, ntohs(netdev_dev->remote_addr.sin_port)));
+        smap_add_format(args, "remote_port", "%"PRIu16,
+               ntohs(netdev_dev->remote_addr.sin_port));
     return 0;
 }
 
@@ -190,19 +203,20 @@ netdev_tunnel_connect(struct netdev_dev_tunnel *dev)
         return errno;
     }
     dev->connected = true;
+    netdev_tunnel_update_seq(dev);
     VLOG_DBG("%s: connected to (%s, %d)", netdev_dev_get_name(&dev->netdev_dev),
         inet_ntoa(dev->remote_addr.sin_addr), ntohs(dev->remote_addr.sin_port));
     return 0;
 }
 
 static int
-netdev_tunnel_set_config(struct netdev_dev *dev_, const struct shash *args)
+netdev_tunnel_set_config(struct netdev_dev *dev_, const struct smap *args)
 {
     struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(dev_);
     struct shash_node *node;
 
     VLOG_DBG("tunnel_set_config(%s)", netdev_dev_get_name(dev_));
-    SHASH_FOR_EACH(node, args) {
+    SMAP_FOR_EACH(node, args) {
         VLOG_DBG("arg: %s->%s", node->name, (char*)node->data);
        if (!strcmp(node->name, "remote_ip")) {
            struct in_addr addr;
@@ -224,29 +238,53 @@ netdev_tunnel_set_config(struct netdev_dev *dev_, const struct shash *args)
 }
 
 static int
-netdev_tunnel_listen(struct netdev *netdev_)
-{
+netdev_tunnel_rx_open(struct netdev *netdev_, struct netdev_rx **rxp)
+{   
+    struct netdev_dev_tunnel *netdev_dev =
+        netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
+    struct netdev_rx_tunnel *rx;
+    rx = xmalloc(sizeof *rx);
+    netdev_rx_init(&rx->up, netdev_get_dev(netdev_), &netdev_rx_tunnel_class);
+    rx->fd = netdev_dev->sockfd;
+    *rxp = &rx->up;
     return 0;
 }
 
+static void
+netdev_rx_tunnel_destroy(struct netdev_rx *rx_)
+{
+    struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
+    free(rx);
+}
+
 static int
-netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
+netdev_rx_tunnel_recv(struct netdev_rx *rx_, void *buffer, size_t size)
 {
-    struct netdev_dev_tunnel *dev = 
-       netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
-    if (!dev->connected)
+    struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_);
+    struct netdev_dev_tunnel *netdev_dev =
+        netdev_dev_tunnel_cast(rx_->netdev_dev);
+    if (!netdev_dev->connected)
         return -EAGAIN;
     for (;;) {
         ssize_t retval;
-        retval = recv(dev->sockfd, buffer, size, MSG_TRUNC);
-       VLOG_DBG("%s: recv(%x, %d, MSG_TRUNC) = %d",
-                       netdev_get_name(netdev_), buffer, size, retval);
+        retval = recv(rx->fd, buffer, size, MSG_TRUNC);
+           VLOG_DBG("%s: recv(%"PRIxPTR", %zu, MSG_TRUNC) = %zd",
+                   netdev_rx_get_name(rx_), (uintptr_t)buffer, size, retval);
         if (retval >= 0) {
-            return retval <= size ? retval : -EMSGSIZE;
+           netdev_dev->stats.rx_packets++;
+           netdev_dev->stats.rx_bytes += retval;
+            if (retval <= size) {
+                   return retval;
+            } else {
+                netdev_dev->stats.rx_errors++;
+                netdev_dev->stats.rx_length_errors++;
+                return -EMSGSIZE;
+            }
         } else if (errno != EINTR) {
             if (errno != EAGAIN) {
-                VLOG_WARN("error receiveing Ethernet packet on %s: %s",
-                    strerror(errno), netdev_get_name(netdev_));
+                VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s",
+                    netdev_rx_get_name(rx_), strerror(errno));
+                   netdev_dev->stats.rx_errors++;
             }
             return -errno;
         }
@@ -254,12 +292,12 @@ netdev_tunnel_recv(struct netdev *netdev_, void *buffer, size_t size)
 }
 
 static void
-netdev_tunnel_recv_wait(struct netdev *netdev_)
+netdev_rx_tunnel_wait(struct netdev_rx *rx_)
 {
-    struct netdev_dev_tunnel *dev = 
-       netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
-    if (dev->sockfd >= 0) {
-        poll_fd_wait(dev->sockfd, POLLIN);
+    struct netdev_rx_tunnel *rx = 
+       netdev_rx_tunnel_cast(rx_);
+    if (rx->fd >= 0) {
+        poll_fd_wait(rx->fd, POLLIN);
     }
 }
 
@@ -269,19 +307,28 @@ netdev_tunnel_send(struct netdev *netdev_, const void *buffer, size_t size)
     struct netdev_dev_tunnel *dev = 
        netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
     if (!dev->connected)
-        return -EAGAIN;
+        return EAGAIN;
     for (;;) {
         ssize_t retval;
         retval = send(dev->sockfd, buffer, size, 0);
-       VLOG_DBG("%s: send(%x, %d) = %d", netdev_get_name(netdev_), buffer, size, retval);
+       VLOG_DBG("%s: send(%"PRIxPTR", %zu) = %zd",
+                netdev_get_name(netdev_), (uintptr_t)buffer, size, retval);
         if (retval >= 0) {
-            return retval;
+           dev->stats.tx_packets++;
+           dev->stats.tx_bytes += retval;
+           if (retval != size) {
+               VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
+                            "%zu) on %s", retval, size, netdev_get_name(netdev_));
+               dev->stats.tx_errors++;
+           }
+            return 0;
         } else if (errno != EINTR) {
             if (errno != EAGAIN) {
-                VLOG_WARN("error sending Ethernet packet on %s: %s",
-                    strerror(errno), netdev_get_name(netdev_));
+                VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
+                    netdev_get_name(netdev_), strerror(errno));
+               dev->stats.tx_errors++;
             }
-            return -errno;
+            return errno;
         }
     }
 }
@@ -297,17 +344,19 @@ netdev_tunnel_send_wait(struct netdev *netdev_)
 }
 
 static int
-netdev_tunnel_drain(struct netdev *netdev_)
+netdev_rx_tunnel_drain(struct netdev_rx *rx_)
 {
-    struct netdev_dev_tunnel *dev = 
-       netdev_dev_tunnel_cast(netdev_get_dev(netdev_));
+    struct netdev_dev_tunnel *netdev_dev =
+        netdev_dev_tunnel_cast(rx_->netdev_dev);
+    struct netdev_rx_tunnel *rx = 
+       netdev_rx_tunnel_cast(rx_);
     char buffer[128];
     int error;
 
-    if (!dev->connected)
+    if (!netdev_dev->connected)
        return 0;
     for (;;) {
-       error = recv(dev->sockfd, buffer, 128, MSG_TRUNC);
+       error = recv(rx->fd, buffer, 128, MSG_TRUNC);
        if (error) {
             if (error == -EAGAIN)
                break;
@@ -327,7 +376,7 @@ netdev_tunnel_set_etheraddr(struct netdev *netdev,
 
     if (!eth_addr_equals(dev->hwaddr, mac)) {
         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
-        netdev_tunnel_poll_notify(netdev);
+        netdev_tunnel_update_seq(dev);
     }
 
     return 0;
@@ -344,35 +393,6 @@ netdev_tunnel_get_etheraddr(const struct netdev *netdev,
     return 0;
 }
 
-static int
-netdev_tunnel_get_mtu(const struct netdev *netdev, int *mtup)
-{
-    const struct netdev_dev_tunnel *dev =
-        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
-
-    *mtup = dev->mtu;
-    return 0;
-}
-
-static int
-netdev_tunnel_set_mtu(const struct netdev *netdev, int mtu)
-{
-    struct netdev_dev_tunnel *dev =
-        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
-
-    dev->mtu = mtu;
-    return 0;
-}
-
-static int
-netdev_tunnel_get_carrier(const struct netdev *netdev, bool *carrier)
-{
-    struct netdev_dev_tunnel *dev =
-        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
-    VLOG_DBG("tunnel_get_carrier(%s)", netdev_get_name(netdev));
-    *carrier = dev->connected;
-    return 0;
-}
 
 static int
 netdev_tunnel_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
@@ -395,22 +415,23 @@ netdev_tunnel_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
 }
 
 static int
-netdev_tunnel_update_flags(struct netdev *netdev,
+netdev_tunnel_update_flags(struct netdev_dev *dev_,
                           enum netdev_flags off, enum netdev_flags on,
                           enum netdev_flags *old_flagsp)
 {
-    struct netdev_dev_tunnel *dev =
-        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
+    struct netdev_dev_tunnel *netdev_dev =
+        netdev_dev_tunnel_cast(dev_);
 
     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
         return EINVAL;
     }
 
-    *old_flagsp = dev->flags;
-    dev->flags |= on;
-    dev->flags &= ~off;
-    if (*old_flagsp != dev->flags) {
-        netdev_tunnel_poll_notify(netdev);
+    // XXX should we actually do something with these flags?
+    *old_flagsp = netdev_dev->flags;
+    netdev_dev->flags |= on;
+    netdev_dev->flags &= ~off;
+    if (*old_flagsp != netdev_dev->flags) {
+        netdev_tunnel_update_seq(netdev_dev);
     }
     return 0;
 }
@@ -424,11 +445,8 @@ netdev_tunnel_change_seq(const struct netdev *netdev)
 /* Helper functions. */
 
 static void
-netdev_tunnel_poll_notify(const struct netdev *netdev)
+netdev_tunnel_update_seq(struct netdev_dev_tunnel *dev)
 {
-    struct netdev_dev_tunnel *dev =
-        netdev_dev_tunnel_cast(netdev_get_dev(netdev));
-
     dev->change_seq++;
     if (!dev->change_seq) {
         dev->change_seq++;
@@ -437,10 +455,9 @@ netdev_tunnel_poll_notify(const struct netdev *netdev)
 
 static void
 netdev_tunnel_get_port(struct unixctl_conn *conn,
-                     int argc, const char *argv[], void *aux OVS_UNUSED)
+                     int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
 {
     struct netdev_dev_tunnel *tunnel_dev;
-    uint16_t port;
     char buf[6];
 
     tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
@@ -453,12 +470,50 @@ netdev_tunnel_get_port(struct unixctl_conn *conn,
     unixctl_command_reply(conn, buf);
 }
 
+static void
+netdev_tunnel_get_tx_bytes(struct unixctl_conn *conn,
+                     int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
+{
+    struct netdev_dev_tunnel *tunnel_dev;
+    char buf[128];
+
+    tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
+    if (!tunnel_dev) {
+        unixctl_command_reply_error(conn, "no such tunnel netdev");
+        return;
+    }
+
+    sprintf(buf, "%"PRIu64, tunnel_dev->stats.tx_bytes);
+    unixctl_command_reply(conn, buf);
+}
+
+static void
+netdev_tunnel_get_rx_bytes(struct unixctl_conn *conn,
+                     int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
+{
+    struct netdev_dev_tunnel *tunnel_dev;
+    char buf[128];
+
+    tunnel_dev = shash_find_data(&tunnel_netdev_devs, argv[1]);
+    if (!tunnel_dev) {
+        unixctl_command_reply_error(conn, "no such tunnel netdev");
+        return;
+    }
+
+    sprintf(buf, "%"PRIu64, tunnel_dev->stats.rx_bytes);
+    unixctl_command_reply(conn, buf);
+}
+
 
 static int
 netdev_tunnel_init(void)
 {
     unixctl_command_register("netdev-tunnel/get-port", "NAME",
                              1, 1, netdev_tunnel_get_port, NULL);
+    unixctl_command_register("netdev-tunnel/get-tx-bytes", "NAME",
+                             1, 1, netdev_tunnel_get_tx_bytes, NULL);
+    unixctl_command_register("netdev-tunnel/get-rx-bytes", "NAME",
+                             1, 1, netdev_tunnel_get_rx_bytes, NULL);
     return 0;
 }
 
@@ -472,24 +527,22 @@ const struct netdev_class netdev_tunnel_class = {
     netdev_tunnel_destroy,
     netdev_tunnel_get_config,
     netdev_tunnel_set_config, 
+    NULL,                                  /* get_tunnel_config */
 
     netdev_tunnel_open,
     netdev_tunnel_close,
 
-    netdev_tunnel_listen,
-    netdev_tunnel_recv,
-    netdev_tunnel_recv_wait,
-    netdev_tunnel_drain,
+    netdev_tunnel_rx_open,
 
     netdev_tunnel_send, 
     netdev_tunnel_send_wait,  
 
     netdev_tunnel_set_etheraddr,
     netdev_tunnel_get_etheraddr,
-    netdev_tunnel_get_mtu,
-    netdev_tunnel_set_mtu,
+    NULL,                                  /* get_mtu */
+    NULL,                                  /* set_mtu */
     NULL,                       /* get_ifindex */
-    netdev_tunnel_get_carrier,  /* get_carrier */
+    NULL,                                  /* get_carrier */
     NULL,                       /* get_carrier_resets */
     NULL,                       /* get_miimon */
     netdev_tunnel_get_stats,
@@ -522,3 +575,12 @@ const struct netdev_class netdev_tunnel_class = {
 
     netdev_tunnel_change_seq
 };
+
+
+static const struct netdev_rx_class netdev_rx_tunnel_class = {
+    netdev_rx_tunnel_destroy,
+    netdev_rx_tunnel_recv,
+    netdev_rx_tunnel_wait,
+    netdev_rx_tunnel_drain,
+};
+