[dpdk-dev] [PATCH 3/5] ethdev: add mtu accessors

David Marchand david.marchand at 6wind.com
Mon May 26 13:31:30 CEST 2014


From: Samuel Gauthier <samuel.gauthier at 6wind.com>

This patch adds two new functions in ethdev api to retrieve current MTU and
change MTU of a port.
These operations have been implemented for rte_em_pmd, rte_igb_pmd and
rte_ixgbe_pmd.

Signed-off-by: Samuel Gauthier <samuel.gauthier at 6wind.com>
Signed-off-by: Ivan Boule <ivan.boule at 6wind.com>
Signed-off-by: David Marchand <david.marchand at 6wind.com>
---
 lib/librte_ether/rte_ethdev.c       |   31 +++++++++++++++++
 lib/librte_ether/rte_ethdev.h       |   37 ++++++++++++++++++++
 lib/librte_pmd_e1000/e1000_ethdev.h |    4 +++
 lib/librte_pmd_e1000/em_ethdev.c    |   64 +++++++++++++++++++++++++++++++++++
 lib/librte_pmd_e1000/em_rxtx.c      |   11 ++++++
 lib/librte_pmd_e1000/igb_ethdev.c   |   64 +++++++++++++++++++++++++++++++++++
 lib/librte_pmd_e1000/igb_rxtx.c     |   10 ++++++
 lib/librte_pmd_ixgbe/ixgbe_ethdev.c |   62 +++++++++++++++++++++++++++++++++
 lib/librte_pmd_ixgbe/ixgbe_ethdev.h |    2 ++
 lib/librte_pmd_ixgbe/ixgbe_rxtx.c   |   10 ++++++
 10 files changed, 295 insertions(+)

diff --git a/lib/librte_ether/rte_ethdev.c b/lib/librte_ether/rte_ethdev.c
index 31c18ef..ece2a68 100644
--- a/lib/librte_ether/rte_ethdev.c
+++ b/lib/librte_ether/rte_ethdev.c
@@ -1058,6 +1058,37 @@ rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
 	ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
 }
 
+
+int
+rte_eth_dev_get_mtu(uint8_t port_id, uint16_t *mtu)
+{
+	struct rte_eth_dev *dev;
+
+	if (port_id >= nb_ports) {
+		PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
+		return (-ENODEV);
+	}
+
+	dev = &rte_eth_devices[port_id];
+	FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_get, -ENOTSUP);
+	return (*dev->dev_ops->mtu_get)(dev, mtu);
+}
+
+int
+rte_eth_dev_set_mtu(uint8_t port_id, uint16_t *mtu)
+{
+	struct rte_eth_dev *dev;
+
+	if (port_id >= nb_ports) {
+		PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
+		return (-ENODEV);
+	}
+
+	dev = &rte_eth_devices[port_id];
+	FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
+	return (*dev->dev_ops->mtu_set)(dev, mtu);
+}
+
 int
 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
 {
diff --git a/lib/librte_ether/rte_ethdev.h b/lib/librte_ether/rte_ethdev.h
index 39351ea..177a6ec 100644
--- a/lib/librte_ether/rte_ethdev.h
+++ b/lib/librte_ether/rte_ethdev.h
@@ -890,6 +890,12 @@ typedef uint32_t (*eth_rx_queue_count_t)(struct rte_eth_dev *dev,
 typedef int (*eth_rx_descriptor_done_t)(void *rxq, uint16_t offset);
 /**< @Check DD bit of specific RX descriptor */
 
+typedef int (*mtu_get_t)(struct rte_eth_dev *dev, uint16_t *mtu);
+/**< @internal Get MTU. */
+
+typedef int (*mtu_set_t)(struct rte_eth_dev *dev, uint16_t *mtu);
+/**< @internal Set MTU. */
+
 typedef int (*vlan_filter_set_t)(struct rte_eth_dev *dev,
 				  uint16_t vlan_id,
 				  int on);
@@ -1113,6 +1119,8 @@ struct eth_dev_ops {
 	eth_queue_stats_mapping_set_t queue_stats_mapping_set;
 	/**< Configure per queue stat counter mapping. */
 	eth_dev_infos_get_t        dev_infos_get; /**< Get device info. */
+	mtu_get_t                  mtu_get; /**< Get MTU. */
+	mtu_set_t                  mtu_set; /**< Set MTU. */
 	vlan_filter_set_t          vlan_filter_set;  /**< Filter VLAN Setup. */
 	vlan_tpid_set_t            vlan_tpid_set;      /**< Outer VLAN TPID Setup. */
 	vlan_strip_queue_set_t     vlan_strip_queue_set; /**< VLAN Stripping on queue. */
@@ -1680,6 +1688,35 @@ extern void rte_eth_dev_info_get(uint8_t port_id,
 				 struct rte_eth_dev_info *dev_info);
 
 /**
+ * Retrieve the MTU of an Ethernet device.
+ *
+ * @param port_id
+ *   The port identifier of the Ethernet device.
+ * @param mtu
+ *   A pointer to a uint16_t where the retrieved MTU is to be stored.
+ * @return
+ *   - (0) if successful.
+ *   - (-ENOSUP) if operation is not supported.
+ *   - (-ENODEV) if *port_id* invalid.
+ */
+extern int rte_eth_dev_get_mtu(uint8_t port_id, uint16_t *mtu);
+
+/**
+ * Change the MTU of an Ethernet device.
+ *
+ * @param port_id
+ *   The port identifier of the Ethernet device.
+ * @param mtu
+ *   A pointer to a uint16_t where the MTU to be applied is stored.
+ * @return
+ *   - (0) if successful.
+ *   - (-ENOSUP) if operation is not supported.
+ *   - (-ENODEV) if *port_id* invalid.
+ *   - (-EINVAL) if *mtu* invalid.
+ */
+extern int rte_eth_dev_set_mtu(uint8_t port_id, uint16_t *mtu);
+
+/**
  * Enable/Disable hardware filtering by an Ethernet device of received
  * VLAN packets tagged with a given VLAN Tag Identifier.
  *
diff --git a/lib/librte_pmd_e1000/e1000_ethdev.h b/lib/librte_pmd_e1000/e1000_ethdev.h
index 8790601..5cbf436 100644
--- a/lib/librte_pmd_e1000/e1000_ethdev.h
+++ b/lib/librte_pmd_e1000/e1000_ethdev.h
@@ -138,6 +138,8 @@ uint16_t eth_igb_recv_pkts(void *rxq, struct rte_mbuf **rx_pkts,
 uint16_t eth_igb_recv_scattered_pkts(void *rxq,
 		struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
 
+void eth_igb_dev_set_rx_scatter_mode(struct rte_eth_dev *dev);
+
 int eth_igb_rss_hash_update(struct rte_eth_dev *dev,
 			    struct rte_eth_rss_conf *rss_conf);
 
@@ -192,4 +194,6 @@ uint16_t eth_em_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
 uint16_t eth_em_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
 		uint16_t nb_pkts);
 
+void eth_em_set_rx_scatter_mode(struct rte_eth_dev *dev);
+
 #endif /* _E1000_ETHDEV_H_ */
diff --git a/lib/librte_pmd_e1000/em_ethdev.c b/lib/librte_pmd_e1000/em_ethdev.c
index c148cbc..044d73f 100644
--- a/lib/librte_pmd_e1000/em_ethdev.c
+++ b/lib/librte_pmd_e1000/em_ethdev.c
@@ -94,6 +94,9 @@ static void em_hw_control_release(struct e1000_hw *hw);
 static void em_init_manageability(struct e1000_hw *hw);
 static void em_release_manageability(struct e1000_hw *hw);
 
+static int eth_em_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu);
+static int eth_em_set_mtu(struct rte_eth_dev *dev, uint16_t *mtu);
+
 static int eth_em_vlan_filter_set(struct rte_eth_dev *dev,
 		uint16_t vlan_id, int on);
 static void eth_em_vlan_offload_set(struct rte_eth_dev *dev, int mask);
@@ -145,6 +148,8 @@ static struct eth_dev_ops eth_em_ops = {
 	.stats_get            = eth_em_stats_get,
 	.stats_reset          = eth_em_stats_reset,
 	.dev_infos_get        = eth_em_infos_get,
+	.mtu_get              = eth_em_get_mtu,
+	.mtu_set              = eth_em_set_mtu,
 	.vlan_filter_set      = eth_em_vlan_filter_set,
 	.vlan_offload_set     = eth_em_vlan_offload_set,
 	.rx_queue_setup       = eth_em_rx_queue_setup,
@@ -1487,6 +1492,65 @@ eth_em_rar_clear(struct rte_eth_dev *dev, uint32_t index)
 	e1000_rar_set(hw, addr, index);
 }
 
+static int
+eth_em_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
+{
+	*mtu = (uint16_t) dev->data->dev_conf.rxmode.max_rx_pkt_len;
+	return 0;
+}
+
+static uint32_t
+em_get_rctl_buffer_size(uint32_t rctl)
+{
+	uint32_t rctl_buf_size;
+
+	static uint16_t std_buf_sizes[4] = {
+		2048, 1024, 512, 256,
+	};
+	static uint16_t ext_buf_sizes[4] = {
+		0 /* invalid */, 16384, 8192, 4096,
+	};
+
+	rctl_buf_size = ((rctl & 0x00030000) >> 16);
+	if (rctl & E1000_RCTL_BSEX)
+		return (uint32_t) ext_buf_sizes[rctl_buf_size];
+	else
+		return (uint32_t) std_buf_sizes[rctl_buf_size];
+}
+
+static int
+eth_em_set_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
+{
+	struct e1000_hw *hw;
+	uint32_t frame_size;
+	uint32_t rx_buf_size;
+	uint32_t rctl;
+
+	frame_size = *mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + VLAN_TAG_SIZE;
+	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+
+	/* check that mtu is within the allowed range */
+	if ((*mtu < 68) || (frame_size > em_get_max_pktlen(hw)))
+		return -EINVAL;
+
+	rctl = E1000_READ_REG(hw, E1000_RCTL);
+	rx_buf_size = em_get_rctl_buffer_size(rctl); /* set at init time. */
+	/* switch to jumbo mode if needed */
+	if (frame_size  > rx_buf_size) {
+		eth_em_set_rx_scatter_mode(dev);
+		dev->data->dev_conf.rxmode.jumbo_frame = 1;
+		rctl |= E1000_RCTL_LPE;
+	} else {
+		dev->data->dev_conf.rxmode.jumbo_frame = 0;
+		rctl &= ~E1000_RCTL_LPE;
+	}
+	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
+
+	/* update max frame size */
+	dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
+	return 0;
+}
+
 struct rte_driver em_pmd_drv = {
 	.type = PMD_PDEV,
 	.init = rte_em_pmd_init,
diff --git a/lib/librte_pmd_e1000/em_rxtx.c b/lib/librte_pmd_e1000/em_rxtx.c
index 4f98a3f..5e7f74d 100644
--- a/lib/librte_pmd_e1000/em_rxtx.c
+++ b/lib/librte_pmd_e1000/em_rxtx.c
@@ -1063,6 +1063,17 @@ eth_em_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
 	return (nb_rx);
 }
 
+void
+eth_em_set_rx_scatter_mode(struct rte_eth_dev *dev)
+{
+	if (dev->rx_pkt_burst != eth_em_recv_scattered_pkts) {
+		dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_scattered_pkts;
+		dev->data->scattered_rx = 1;
+		/* make sure this setting is viewed by all cores */
+		rte_wmb();
+	}
+}
+
 /*
  * Rings setup and release.
  *
diff --git a/lib/librte_pmd_e1000/igb_ethdev.c b/lib/librte_pmd_e1000/igb_ethdev.c
index e15fe5a..8f46963 100644
--- a/lib/librte_pmd_e1000/igb_ethdev.c
+++ b/lib/librte_pmd_e1000/igb_ethdev.c
@@ -87,6 +87,9 @@ static void igb_hw_control_release(struct e1000_hw *hw);
 static void igb_init_manageability(struct e1000_hw *hw);
 static void igb_release_manageability(struct e1000_hw *hw);
 
+static int  eth_igb_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu);
+static int  eth_igb_set_mtu(struct rte_eth_dev *dev, uint16_t *mtu);
+
 static int eth_igb_vlan_filter_set(struct rte_eth_dev *dev,
 		uint16_t vlan_id, int on);
 static void eth_igb_vlan_tpid_set(struct rte_eth_dev *dev, uint16_t tpid_id);
@@ -180,6 +183,8 @@ static struct eth_dev_ops eth_igb_ops = {
 	.stats_get            = eth_igb_stats_get,
 	.stats_reset          = eth_igb_stats_reset,
 	.dev_infos_get        = eth_igb_infos_get,
+	.mtu_get              = eth_igb_get_mtu,
+	.mtu_set              = eth_igb_set_mtu,
 	.vlan_filter_set      = eth_igb_vlan_filter_set,
 	.vlan_tpid_set        = eth_igb_vlan_tpid_set,
 	.vlan_offload_set     = eth_igb_vlan_offload_set,
@@ -2238,6 +2243,65 @@ eth_igb_rss_reta_query(struct rte_eth_dev *dev,
 	return 0;
 }
 
+static int
+eth_igb_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
+{
+	uint32_t rlpml;
+	struct e1000_hw *hw;
+
+	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+
+	rlpml = E1000_READ_REG(hw, E1000_RLPML);
+
+	*mtu = (uint16_t) rlpml;
+	return 0;
+}
+
+static int
+eth_igb_set_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
+{
+	uint32_t rctl;
+	struct e1000_hw *hw;
+	struct rte_eth_dev_info dev_info;
+	uint32_t frame_size = *mtu + ETHER_HDR_LEN +
+				ETHER_CRC_LEN + VLAN_TAG_SIZE;
+
+	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+
+#ifdef RTE_LIBRTE_82571_SUPPORT
+	/* XXX: not bigger than max_rx_pktlen */
+	if (hw->mac.type == e1000_82571)
+		return -ENOTSUP;
+#endif
+	eth_igb_infos_get(dev, &dev_info);
+
+	/* check that mtu is within the allowed range */
+	if ((*mtu < 68) ||
+	    (frame_size > dev_info.max_rx_pktlen))
+		return -EINVAL;
+
+	rctl = E1000_READ_REG(hw, E1000_RCTL);
+
+	/* switch to jumbo mode if needed */
+	if (frame_size  > ETHER_MAX_LEN) {
+		eth_igb_dev_set_rx_scatter_mode(dev);
+		dev->data->dev_conf.rxmode.jumbo_frame = 1;
+		rctl |= E1000_RCTL_LPE;
+	} else {
+		dev->data->dev_conf.rxmode.jumbo_frame = 0;
+		rctl &= ~E1000_RCTL_LPE;
+	}
+	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
+
+	/* update max frame size */
+	dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
+
+	E1000_WRITE_REG(hw, E1000_RLPML,
+			dev->data->dev_conf.rxmode.max_rx_pkt_len);
+
+	return 0;
+}
+
 static struct rte_driver pmd_igb_drv = {
 	.type = PMD_PDEV,
 	.init = rte_igb_pmd_init,
diff --git a/lib/librte_pmd_e1000/igb_rxtx.c b/lib/librte_pmd_e1000/igb_rxtx.c
index 6b48df5..61ec0ff 100644
--- a/lib/librte_pmd_e1000/igb_rxtx.c
+++ b/lib/librte_pmd_e1000/igb_rxtx.c
@@ -2155,6 +2155,16 @@ eth_igb_tx_init(struct rte_eth_dev *dev)
 	E1000_WRITE_REG(hw, E1000_TCTL, tctl);
 }
 
+void eth_igb_dev_set_rx_scatter_mode(struct rte_eth_dev *dev)
+{
+	if (dev->rx_pkt_burst != eth_igb_recv_scattered_pkts) {
+		dev->rx_pkt_burst = eth_igb_recv_scattered_pkts;
+		/* we must ensure that this is done when we leave the
+		   function */
+		rte_wmb();
+	}
+}
+
 /*********************************************************************
  *
  *  Enable VF receive unit.
diff --git a/lib/librte_pmd_ixgbe/ixgbe_ethdev.c b/lib/librte_pmd_ixgbe/ixgbe_ethdev.c
index c876c3e..b9db1f4 100644
--- a/lib/librte_pmd_ixgbe/ixgbe_ethdev.c
+++ b/lib/librte_pmd_ixgbe/ixgbe_ethdev.c
@@ -114,6 +114,10 @@ static int ixgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
 					     uint8_t is_rx);
 static void ixgbe_dev_info_get(struct rte_eth_dev *dev,
 				struct rte_eth_dev_info *dev_info);
+
+static int ixgbe_dev_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu);
+static int ixgbe_dev_set_mtu(struct rte_eth_dev *dev, uint16_t *mtu);
+
 static int ixgbe_vlan_filter_set(struct rte_eth_dev *dev,
 		uint16_t vlan_id, int on);
 static void ixgbe_vlan_tpid_set(struct rte_eth_dev *dev, uint16_t tpid_id);
@@ -264,6 +268,8 @@ static struct eth_dev_ops ixgbe_eth_dev_ops = {
 	.stats_reset          = ixgbe_dev_stats_reset,
 	.queue_stats_mapping_set = ixgbe_dev_queue_stats_mapping_set,
 	.dev_infos_get        = ixgbe_dev_info_get,
+	.mtu_get              = ixgbe_dev_get_mtu,
+	.mtu_set              = ixgbe_dev_set_mtu,
 	.vlan_filter_set      = ixgbe_vlan_filter_set,
 	.vlan_tpid_set        = ixgbe_vlan_tpid_set,
 	.vlan_offload_set     = ixgbe_vlan_offload_set,
@@ -2590,6 +2596,62 @@ ixgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index)
 	ixgbe_clear_rar(hw, index);
 }
 
+static int
+ixgbe_dev_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
+{
+	uint32_t maxfrs;
+	struct ixgbe_hw *hw;
+
+	hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+
+	maxfrs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
+	*mtu = (uint16_t) (((0xFFFF0000 & maxfrs) >> 16) -
+	       ETHER_HDR_LEN - ETHER_CRC_LEN);
+	return 0;
+}
+
+static int
+ixgbe_dev_set_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
+{
+	uint32_t hlreg0;
+	uint32_t maxfrs;
+	struct ixgbe_hw *hw;
+	struct rte_eth_dev_info dev_info;
+	uint32_t frame_size = *mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
+
+	ixgbe_dev_info_get(dev, &dev_info);
+
+	/* check that mtu is within the allowed range */
+	if ((*mtu < 68) ||
+	    (frame_size > dev_info.max_rx_pktlen))
+		return -EINVAL;
+
+	hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+
+	hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
+
+	/* switch to jumbo mode if needed */
+	if (frame_size  > ETHER_MAX_LEN) {
+		ixgbe_dev_set_rx_scatter_mode(dev);
+		dev->data->dev_conf.rxmode.jumbo_frame = 1;
+		hlreg0 |= IXGBE_HLREG0_JUMBOEN;
+	} else {
+		dev->data->dev_conf.rxmode.jumbo_frame = 0;
+		hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
+	}
+	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
+
+	/* update max frame size */
+	dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
+
+	maxfrs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
+	maxfrs &= 0x0000FFFF;
+	maxfrs |= (dev->data->dev_conf.rxmode.max_rx_pkt_len << 16);
+	IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, maxfrs);
+
+	return 0;
+}
+
 /*
  * Virtual Function operations
  */
diff --git a/lib/librte_pmd_ixgbe/ixgbe_ethdev.h b/lib/librte_pmd_ixgbe/ixgbe_ethdev.h
index 846db0a..a667ec2 100644
--- a/lib/librte_pmd_ixgbe/ixgbe_ethdev.h
+++ b/lib/librte_pmd_ixgbe/ixgbe_ethdev.h
@@ -235,6 +235,8 @@ uint16_t ixgbe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
 uint16_t ixgbe_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
 		uint16_t nb_pkts);
 
+void ixgbe_dev_set_rx_scatter_mode(struct rte_eth_dev *dev);
+
 int ixgbe_dev_rss_hash_update(struct rte_eth_dev *dev,
 			      struct rte_eth_rss_conf *rss_conf);
 
diff --git a/lib/librte_pmd_ixgbe/ixgbe_rxtx.c b/lib/librte_pmd_ixgbe/ixgbe_rxtx.c
index 2d2c1a0..b05c1ba 100644
--- a/lib/librte_pmd_ixgbe/ixgbe_rxtx.c
+++ b/lib/librte_pmd_ixgbe/ixgbe_rxtx.c
@@ -3892,3 +3892,13 @@ ixgbevf_dev_rxtx_start(struct rte_eth_dev *dev)
 
 	}
 }
+
+void ixgbe_dev_set_rx_scatter_mode(struct rte_eth_dev *dev)
+{
+	if (dev->rx_pkt_burst != ixgbe_recv_scattered_pkts) {
+		dev->rx_pkt_burst = ixgbe_recv_scattered_pkts;
+		/* we must ensure that this is done when we leave the
+		   function */
+		rte_wmb();
+	}
+}
-- 
1.7.10.4



More information about the dev mailing list