net/virtio: fix memory leak when reinitializing device
[dpdk.git] / drivers / net / virtio / virtio_ethdev.c
index 1f0e239..4f20e47 100644 (file)
@@ -8,7 +8,7 @@
 #include <errno.h>
 #include <unistd.h>
 
-#include <rte_ethdev.h>
+#include <rte_ethdev_driver.h>
 #include <rte_ethdev_pci.h>
 #include <rte_memcpy.h>
 #include <rte_string_fns.h>
@@ -19,6 +19,8 @@
 #include <rte_pci.h>
 #include <rte_bus_pci.h>
 #include <rte_ether.h>
+#include <rte_ip.h>
+#include <rte_arp.h>
 #include <rte_common.h>
 #include <rte_errno.h>
 #include <rte_cpuflags.h>
@@ -81,6 +83,9 @@ static int virtio_dev_queue_stats_mapping_set(
 int virtio_logtype_init;
 int virtio_logtype_driver;
 
+static void virtio_notify_peers(struct rte_eth_dev *dev);
+static void virtio_ack_link_announce(struct rte_eth_dev *dev);
+
 /*
  * The set of PCI devices this driver supports
  */
@@ -274,17 +279,6 @@ virtio_dev_queue_release(void *queue __rte_unused)
        /* do nothing */
 }
 
-static int
-virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
-{
-       if (vtpci_queue_idx == hw->max_queue_pairs * 2)
-               return VTNET_CQ;
-       else if (vtpci_queue_idx % 2 == 0)
-               return VTNET_RQ;
-       else
-               return VTNET_TQ;
-}
-
 static uint16_t
 virtio_get_nr_vq(struct virtio_hw *hw)
 {
@@ -1275,9 +1269,46 @@ virtio_inject_pkts(struct rte_eth_dev *dev, struct rte_mbuf **tx_pkts,
        return ret;
 }
 
+static void
+virtio_notify_peers(struct rte_eth_dev *dev)
+{
+       struct virtio_hw *hw = dev->data->dev_private;
+       struct virtnet_rx *rxvq = dev->data->rx_queues[0];
+       struct rte_mbuf *rarp_mbuf;
+
+       rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
+                       (struct ether_addr *)hw->mac_addr);
+       if (rarp_mbuf == NULL) {
+               PMD_DRV_LOG(ERR, "failed to make RARP packet.");
+               return;
+       }
+
+       /* If virtio port just stopped, no need to send RARP */
+       if (virtio_dev_pause(dev) < 0) {
+               rte_pktmbuf_free(rarp_mbuf);
+               return;
+       }
+
+       virtio_inject_pkts(dev, &rarp_mbuf, 1);
+       virtio_dev_resume(dev);
+}
+
+static void
+virtio_ack_link_announce(struct rte_eth_dev *dev)
+{
+       struct virtio_hw *hw = dev->data->dev_private;
+       struct virtio_pmd_ctrl ctrl;
+
+       ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
+       ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
+
+       virtio_send_command(hw->cvq, &ctrl, NULL, 0);
+}
+
 /*
- * Process Virtio Config changed interrupt and call the callback
- * if link state changed.
+ * Process virtio config changed interrupt. Call the callback
+ * if link state changed, generate gratuitous RARP packet if
+ * the status indicates an ANNOUNCE.
  */
 void
 virtio_interrupt_handler(void *param)
@@ -1300,6 +1331,10 @@ virtio_interrupt_handler(void *param)
                                                      NULL);
        }
 
+       if (isr & VIRTIO_NET_S_ANNOUNCE) {
+               virtio_notify_peers(dev);
+               virtio_ack_link_announce(dev);
+       }
 }
 
 /* set rx and tx handlers according to what is supported */
@@ -1436,6 +1471,11 @@ virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
        /* Reset the device although not necessary at startup */
        vtpci_reset(hw);
 
+       if (hw->vqs) {
+               virtio_dev_free_mbufs(eth_dev);
+               virtio_free_queues(hw);
+       }
+
        /* Tell the host we've noticed this device. */
        vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
 
@@ -1819,7 +1859,7 @@ virtio_dev_configure(struct rte_eth_dev *dev)
        hw->use_simple_rx = 1;
        hw->use_simple_tx = 1;
 
-#if defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
+#if defined RTE_ARCH_ARM64 || defined RTE_ARCH_ARM
        if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
                hw->use_simple_rx = 0;
                hw->use_simple_tx = 0;
@@ -1933,6 +1973,9 @@ static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
        for (i = 0; i < dev->data->nb_rx_queues; i++) {
                struct virtnet_rx *rxvq = dev->data->rx_queues[i];
 
+               if (rxvq == NULL || rxvq->vq == NULL)
+                       continue;
+
                PMD_INIT_LOG(DEBUG,
                             "Before freeing rxq[%d] used and unused buf", i);
                VIRTQUEUE_DUMP(rxvq->vq);
@@ -1952,6 +1995,9 @@ static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
        for (i = 0; i < dev->data->nb_tx_queues; i++) {
                struct virtnet_tx *txvq = dev->data->tx_queues[i];
 
+               if (txvq == NULL || txvq->vq == NULL)
+                       continue;
+
                PMD_INIT_LOG(DEBUG,
                             "Before freeing txq[%d] used and unused bufs",
                             i);