net/bonding: support flow API
[dpdk.git] / drivers / net / bonding / rte_eth_bond_pmd.c
index 92ad688..7a1ef60 100644 (file)
@@ -17,6 +17,7 @@
 #include <rte_bus_vdev.h>
 #include <rte_alarm.h>
 #include <rte_cycles.h>
+#include <rte_string_fns.h>
 
 #include "rte_eth_bond.h"
 #include "rte_eth_bond_private.h"
@@ -617,7 +618,7 @@ mode6_debug(const char __attribute__((unused)) *info, struct ether_hdr *eth_h,
        uint16_t offset = get_vlan_offset(eth_h, &ether_type);
 
 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
-       snprintf(buf, 16, "%s", info);
+       strlcpy(buf, info, 16);
 #endif
 
        if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
@@ -800,7 +801,7 @@ burst_xmit_l2_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
 
                hash = ether_hash(eth_hdr);
 
-               slaves[i++] = (hash ^= hash >> 8) % slave_count;
+               slaves[i] = (hash ^= hash >> 8) % slave_count;
        }
 }
 
@@ -838,7 +839,7 @@ burst_xmit_l23_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
                hash ^= hash >> 16;
                hash ^= hash >> 8;
 
-               slaves[i++] = hash % slave_count;
+               slaves[i] = hash % slave_count;
        }
 }
 
@@ -907,7 +908,7 @@ burst_xmit_l34_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
                hash ^= hash >> 16;
                hash ^= hash >> 8;
 
-               slaves[i++] = hash % slave_count;
+               slaves[i] = hash % slave_count;
        }
 }
 
@@ -1229,7 +1230,7 @@ bond_ethdev_tx_burst_balance(void *queue, struct rte_mbuf **bufs,
        /* Number of mbufs for transmission on each slave */
        uint16_t slave_nb_bufs[RTE_MAX_ETHPORTS] = { 0 };
        /* Mapping array generated by hash function to map mbufs to slaves */
-       uint16_t bufs_slave_port_idxs[RTE_MAX_ETHPORTS] = { 0 };
+       uint16_t bufs_slave_port_idxs[nb_bufs];
 
        uint16_t slave_tx_count, slave_tx_fail_count[RTE_MAX_ETHPORTS] = { 0 };
        uint16_t total_tx_count = 0, total_tx_fail_count = 0;
@@ -1818,8 +1819,13 @@ slave_configure(struct rte_eth_dev *bonded_eth_dev,
                                bonded_eth_dev->data->dev_conf.rxmode.mq_mode;
        }
 
-       slave_eth_dev->data->dev_conf.rxmode.hw_vlan_filter =
-                       bonded_eth_dev->data->dev_conf.rxmode.hw_vlan_filter;
+       if (bonded_eth_dev->data->dev_conf.rxmode.offloads &
+                       DEV_RX_OFFLOAD_VLAN_FILTER)
+               slave_eth_dev->data->dev_conf.rxmode.offloads |=
+                               DEV_RX_OFFLOAD_VLAN_FILTER;
+       else
+               slave_eth_dev->data->dev_conf.rxmode.offloads &=
+                               ~DEV_RX_OFFLOAD_VLAN_FILTER;
 
        nb_rx_queues = bonded_eth_dev->data->nb_rx_queues;
        nb_tx_queues = bonded_eth_dev->data->nb_tx_queues;
@@ -1831,6 +1837,14 @@ slave_configure(struct rte_eth_dev *bonded_eth_dev,
                }
        }
 
+       errval = rte_eth_dev_set_mtu(slave_eth_dev->data->port_id,
+                                    bonded_eth_dev->data->mtu);
+       if (errval != 0 && errval != -ENOTSUP) {
+               RTE_BOND_LOG(ERR, "rte_eth_dev_set_mtu: port %u, err (%d)",
+                               slave_eth_dev->data->port_id, errval);
+               return errval;
+       }
+
        /* Configure device */
        errval = rte_eth_dev_configure(slave_eth_dev->data->port_id,
                        nb_rx_queues, nb_tx_queues,
@@ -1950,10 +1964,19 @@ slave_remove(struct bond_dev_private *internals,
                                slave_eth_dev->data->port_id)
                        break;
 
-       if (i < (internals->slave_count - 1))
+       if (i < (internals->slave_count - 1)) {
+               struct rte_flow *flow;
+
                memmove(&internals->slaves[i], &internals->slaves[i + 1],
                                sizeof(internals->slaves[0]) *
                                (internals->slave_count - i - 1));
+               TAILQ_FOREACH(flow, &internals->flow_list, next) {
+                       memmove(&flow->flows[i], &flow->flows[i + 1],
+                               sizeof(flow->flows[0]) *
+                               (internals->slave_count - i - 1));
+                       flow->flows[internals->slave_count - 1] = NULL;
+               }
+       }
 
        internals->slave_count--;
 
@@ -2026,7 +2049,7 @@ bond_ethdev_start(struct rte_eth_dev *eth_dev)
 
        if (internals->slave_count == 0) {
                RTE_BOND_LOG(ERR, "Cannot start port since there are no slave devices");
-               return -1;
+               goto out_err;
        }
 
        if (internals->user_defined_mac == 0) {
@@ -2037,18 +2060,18 @@ bond_ethdev_start(struct rte_eth_dev *eth_dev)
                                new_mac_addr = &internals->slaves[i].persisted_mac_addr;
 
                if (new_mac_addr == NULL)
-                       return -1;
+                       goto out_err;
 
                if (mac_address_set(eth_dev, new_mac_addr) != 0) {
                        RTE_BOND_LOG(ERR, "bonded port (%d) failed to update MAC address",
                                        eth_dev->data->port_id);
-                       return -1;
+                       goto out_err;
                }
        }
 
        /* Update all slave devices MACs*/
        if (mac_address_slaves_update(eth_dev) != 0)
-               return -1;
+               goto out_err;
 
        /* If bonded device is configure in promiscuous mode then re-apply config */
        if (internals->promiscuous_en)
@@ -2073,7 +2096,7 @@ bond_ethdev_start(struct rte_eth_dev *eth_dev)
                                "bonded port (%d) failed to reconfigure slave device (%d)",
                                eth_dev->data->port_id,
                                internals->slaves[i].port_id);
-                       return -1;
+                       goto out_err;
                }
                /* We will need to poll for link status if any slave doesn't
                 * support interrupts
@@ -2081,6 +2104,7 @@ bond_ethdev_start(struct rte_eth_dev *eth_dev)
                if (internals->slaves[i].link_status_poll_enabled)
                        internals->link_status_polling_enabled = 1;
        }
+
        /* start polling if needed */
        if (internals->link_status_polling_enabled) {
                rte_eal_alarm_set(
@@ -2100,6 +2124,10 @@ bond_ethdev_start(struct rte_eth_dev *eth_dev)
                bond_tlb_enable(internals);
 
        return 0;
+
+out_err:
+       eth_dev->data->dev_started = 0;
+       return -1;
 }
 
 static void
@@ -2172,6 +2200,7 @@ bond_ethdev_close(struct rte_eth_dev *dev)
        struct bond_dev_private *internals = dev->data->dev_private;
        uint8_t bond_port_id = internals->port_id;
        int skipped = 0;
+       struct rte_flow_error ferror;
 
        RTE_LOG(INFO, EAL, "Closing bonded device %s\n", dev->device->name);
        while (internals->slave_count != skipped) {
@@ -2186,6 +2215,7 @@ bond_ethdev_close(struct rte_eth_dev *dev)
                        skipped++;
                }
        }
+       bond_flow_ops.flush(dev, &ferror);
        bond_ethdev_free_queues(dev);
        rte_bitmap_reset(internals->vlan_filter_bmp);
 }
@@ -2244,6 +2274,8 @@ bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 
        dev_info->rx_offload_capa = internals->rx_offload_capa;
        dev_info->tx_offload_capa = internals->tx_offload_capa;
+       dev_info->rx_queue_offload_capa = internals->rx_queue_offload_capa;
+       dev_info->tx_queue_offload_capa = internals->tx_queue_offload_capa;
        dev_info->flow_type_rss_offloads = internals->flow_type_rss_offloads;
 
        dev_info->reta_size = internals->reta_size;
@@ -2851,11 +2883,26 @@ bond_ethdev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
        return 0;
 }
 
-static void
+static int
 bond_ethdev_mac_address_set(struct rte_eth_dev *dev, struct ether_addr *addr)
 {
-       if (mac_address_set(dev, addr))
+       if (mac_address_set(dev, addr)) {
                RTE_BOND_LOG(ERR, "Failed to update MAC address");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int
+bond_filter_ctrl(struct rte_eth_dev *dev __rte_unused,
+                enum rte_filter_type type, enum rte_filter_op op, void *arg)
+{
+       if (type == RTE_ETH_FILTER_GENERIC && op == RTE_ETH_FILTER_GET) {
+               *(const void **)arg = &bond_flow_ops;
+               return 0;
+       }
+       return -ENOTSUP;
 }
 
 const struct eth_dev_ops default_dev_ops = {
@@ -2879,7 +2926,8 @@ const struct eth_dev_ops default_dev_ops = {
        .rss_hash_update      = bond_ethdev_rss_hash_update,
        .rss_hash_conf_get    = bond_ethdev_rss_hash_conf_get,
        .mtu_set              = bond_ethdev_mtu_set,
-       .mac_addr_set         = bond_ethdev_mac_address_set
+       .mac_addr_set         = bond_ethdev_mac_address_set,
+       .filter_ctrl          = bond_filter_ctrl
 };
 
 static int
@@ -2936,6 +2984,8 @@ bond_alloc(struct rte_vdev_device *dev, uint8_t mode)
        internals->active_slave_count = 0;
        internals->rx_offload_capa = 0;
        internals->tx_offload_capa = 0;
+       internals->rx_queue_offload_capa = 0;
+       internals->tx_queue_offload_capa = 0;
        internals->candidate_max_rx_pktlen = 0;
        internals->max_rx_pktlen = 0;
 
@@ -2945,10 +2995,13 @@ bond_alloc(struct rte_vdev_device *dev, uint8_t mode)
        memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
        memset(internals->slaves, 0, sizeof(internals->slaves));
 
+       TAILQ_INIT(&internals->flow_list);
+       internals->flow_isolated_valid = 0;
+
        /* Set mode 4 default configuration */
        bond_mode_8023ad_setup(eth_dev, NULL);
        if (bond_ethdev_mode_set(eth_dev, mode)) {
-               RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode too %d",
+               RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode to %d\n",
                                 eth_dev->data->port_id, mode);
                goto err;
        }
@@ -2994,6 +3047,7 @@ bond_probe(struct rte_vdev_device *dev)
        uint8_t bonding_mode, socket_id/*, agg_mode*/;
        int  arg_count, port_id;
        uint8_t agg_mode;
+       struct rte_eth_dev *eth_dev;
 
        if (!dev)
                return -EINVAL;
@@ -3001,6 +3055,18 @@ bond_probe(struct rte_vdev_device *dev)
        name = rte_vdev_device_name(dev);
        RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
 
+       if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
+           strlen(rte_vdev_device_args(dev)) == 0) {
+               eth_dev = rte_eth_dev_attach_secondary(name);
+               if (!eth_dev) {
+                       RTE_LOG(ERR, PMD, "Failed to probe %s\n", name);
+                       return -1;
+               }
+               /* TODO: request info from primary to set up Rx and Tx */
+               eth_dev->dev_ops = &default_dev_ops;
+               return 0;
+       }
+
        kvlist = rte_kvargs_parse(rte_vdev_device_args(dev),
                pmd_bond_init_valid_arguments);
        if (kvlist == NULL)
@@ -3118,6 +3184,10 @@ bond_remove(struct rte_vdev_device *dev)
        eth_dev->tx_pkt_burst = NULL;
 
        internals = eth_dev->data->dev_private;
+       /* Try to release mempool used in mode6. If the bond
+        * device is not mode6, free the NULL is not problem.
+        */
+       rte_mempool_free(internals->mode6.mempool);
        rte_bitmap_free(internals->vlan_filter_bmp);
        rte_free(internals->vlan_filter_bmpmem);
        rte_free(eth_dev->data->dev_private);