Support rte_eth_dev_set_mtu for pcap ifaces vdevs by
setting the underlying OS network interface's MTU.
Support is for pcap ifaces only and not for pcap files.
Support is for Linux only.

Bugzilla ID: 961
Signed-off-by: Ido Goshen <i...@cgstowernetworks.com>

---
v5:
Alternative approach
Instead of checking MTU in the pmd set it on the OS interface and
let it do the enforcment.

v4:
1. Add release notes comment
2. Access pmd internals via queue struct
3. eth_mtu_set code convention fixes

v3:
Preserve pcap behavior to support max size packets by default
alternative to v2 in order to limit the code change to pcap only and
avoid abi change.
Enforce mtu only in case rte_eth_dev_set_mtu was explicitly called.

v2:
Preserve pcap behavior to support max size packets by default.
---
 doc/guides/rel_notes/release_22_07.rst |  3 ++
 drivers/net/pcap/pcap_ethdev.c         | 43 ++++++++++++++++++++++++++
 drivers/net/pcap/pcap_osdep.h          |  1 +
 drivers/net/pcap/pcap_osdep_freebsd.c  |  7 +++++
 drivers/net/pcap/pcap_osdep_linux.c    | 21 +++++++++++++
 drivers/net/pcap/pcap_osdep_windows.c  |  7 +++++
 6 files changed, 82 insertions(+)

diff --git a/doc/guides/rel_notes/release_22_07.rst 
b/doc/guides/rel_notes/release_22_07.rst
index 0ed4f92820..b90cfefc55 100644
--- a/doc/guides/rel_notes/release_22_07.rst
+++ b/doc/guides/rel_notes/release_22_07.rst
@@ -95,6 +95,9 @@ New Features
   * Added AH mode support in lookaside protocol (IPsec) for CN9K & CN10K.
   * Added AES-GMAC support in lookaside protocol (IPsec) for CN9K & CN10K.
 
+* **Updated pcap driver.**
+
+ * Added support for MTU on Linux network interfaces
 
 Removed Items
 -------------
diff --git a/drivers/net/pcap/pcap_ethdev.c b/drivers/net/pcap/pcap_ethdev.c
index ec29fd6bc5..3d76d50ef1 100644
--- a/drivers/net/pcap/pcap_ethdev.c
+++ b/drivers/net/pcap/pcap_ethdev.c
@@ -807,6 +807,48 @@ eth_stats_reset(struct rte_eth_dev *dev)
        return 0;
 }
 
+static int
+eth_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
+{
+       unsigned int i;
+       struct pmd_internals *internals = dev->data->dev_private;
+       int is_supported = 0;
+       int is_err = 0;
+
+
+       for (i = 0; i < dev->data->nb_rx_queues; i++) {
+               struct pcap_rx_queue* queue = &internals->rx_queue[i];
+
+               if ((strcmp(queue->type, ETH_PCAP_IFACE_ARG) == 0) ||
+                               (strcmp(queue->type, ETH_PCAP_RX_IFACE_ARG) == 
0) ||
+                               (strcmp(queue->type, ETH_PCAP_RX_IFACE_IN_ARG) 
== 0)) {
+                       is_supported = 1;
+                       if (osdep_iface_mtu_set(queue->name, mtu) < 0)
+                               is_err = 1;
+               }
+       }
+
+       for (i = 0; i < dev->data->nb_tx_queues; i++) {
+               struct pcap_tx_queue* queue = &internals->tx_queue[i];
+
+               if ((strcmp(queue->type, ETH_PCAP_IFACE_ARG) == 0) ||
+                               (strcmp(queue->type, ETH_PCAP_TX_IFACE_ARG) == 
0)) {
+                       is_supported = 1;
+                       if (osdep_iface_mtu_set(queue->name, mtu) < 0)
+                               is_err = 1;
+               }
+       }
+
+       if (!is_supported)
+               return -ENOTSUP;
+
+       if (is_err)
+               return -1;
+
+       PMD_LOG(INFO, "MTU set %s %u\n", dev->device->name, mtu);
+       return 0;
+}
+
 static inline void
 infinite_rx_ring_free(struct rte_ring *pkts)
 {
@@ -1004,6 +1046,7 @@ static const struct eth_dev_ops ops = {
        .link_update = eth_link_update,
        .stats_get = eth_stats_get,
        .stats_reset = eth_stats_reset,
+       .mtu_set = eth_mtu_set,
 };
 
 static int
diff --git a/drivers/net/pcap/pcap_osdep.h b/drivers/net/pcap/pcap_osdep.h
index bf41cba982..ef8be7543c 100644
--- a/drivers/net/pcap/pcap_osdep.h
+++ b/drivers/net/pcap/pcap_osdep.h
@@ -14,5 +14,6 @@ extern int eth_pcap_logtype;
 
 int osdep_iface_index_get(const char *name);
 int osdep_iface_mac_get(const char *name, struct rte_ether_addr *mac);
+int osdep_iface_mtu_set(const char *if_name, uint16_t mtu);
 
 #endif
diff --git a/drivers/net/pcap/pcap_osdep_freebsd.c 
b/drivers/net/pcap/pcap_osdep_freebsd.c
index 20556b3e92..52d4a0e6db 100644
--- a/drivers/net/pcap/pcap_osdep_freebsd.c
+++ b/drivers/net/pcap/pcap_osdep_freebsd.c
@@ -57,3 +57,10 @@ osdep_iface_mac_get(const char *if_name, struct 
rte_ether_addr *mac)
        rte_free(buf);
        return 0;
 }
+
+int
+osdep_iface_mtu_set(const char *if_name, uint16_t mtu)
+{
+       PMD_LOG(WARN, "mtu set not supported on freebsd\n");
+       return -ENOTSUP;
+}
diff --git a/drivers/net/pcap/pcap_osdep_linux.c 
b/drivers/net/pcap/pcap_osdep_linux.c
index 97033f57c5..b0b4a716fe 100644
--- a/drivers/net/pcap/pcap_osdep_linux.c
+++ b/drivers/net/pcap/pcap_osdep_linux.c
@@ -40,3 +40,24 @@ osdep_iface_mac_get(const char *if_name, struct 
rte_ether_addr *mac)
        close(if_fd);
        return 0;
 }
+
+int
+osdep_iface_mtu_set(const char *if_name, uint16_t mtu)
+{
+       struct ifreq ifr;
+       int if_fd = socket(AF_INET, SOCK_DGRAM, 0);
+
+       if (if_fd == -1)
+               return -1;
+
+       rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
+       ifr.ifr_mtu = mtu;
+       if (ioctl(if_fd, SIOCSIFMTU, &ifr)) {
+               PMD_LOG(ERR, "%s mtu set to %d failed\n", if_name, mtu);
+               close(if_fd);
+               return -1;
+       }
+
+       close(if_fd);
+       return 0;
+}
diff --git a/drivers/net/pcap/pcap_osdep_windows.c 
b/drivers/net/pcap/pcap_osdep_windows.c
index 1d398dc7ed..15205a521e 100644
--- a/drivers/net/pcap/pcap_osdep_windows.c
+++ b/drivers/net/pcap/pcap_osdep_windows.c
@@ -116,3 +116,10 @@ osdep_iface_mac_get(const char *device_name, struct 
rte_ether_addr *mac)
        free(info);
        return ret;
 }
+
+int
+osdep_iface_mtu_set(const char *if_name, uint16_t mtu)
+{
+       PMD_LOG(WARN, "mtu set not supported on Windows\n", sys_ret);
+       return -ENOTSUP;
+}
-- 
2.17.1

Reply via email to