ethdev: add vlan type when setting ether type
[dpdk.git] / lib / librte_ether / rte_ethdev.c
index 3840775..82e06c3 100644 (file)
@@ -220,9 +220,6 @@ rte_eth_dev_create_unique_device_name(char *name, size_t size,
 {
        int ret;
 
-       if ((name == NULL) || (pci_dev == NULL))
-               return -EINVAL;
-
        ret = snprintf(name, size, "%d:%d.%d",
                        pci_dev->addr.bus, pci_dev->addr.devid,
                        pci_dev->addr.function);
@@ -505,9 +502,6 @@ rte_eth_dev_is_detachable(uint8_t port_id)
 static int
 rte_eth_dev_attach_pdev(struct rte_pci_addr *addr, uint8_t *port_id)
 {
-       if ((addr == NULL) || (port_id == NULL))
-               goto err;
-
        /* re-construct pci_device_list */
        if (rte_eal_pci_scan())
                goto err;
@@ -520,7 +514,6 @@ rte_eth_dev_attach_pdev(struct rte_pci_addr *addr, uint8_t *port_id)
 
        return 0;
 err:
-       RTE_LOG(ERR, EAL, "Driver, cannot attach the device\n");
        return -1;
 }
 
@@ -531,13 +524,6 @@ rte_eth_dev_detach_pdev(uint8_t port_id, struct rte_pci_addr *addr)
        struct rte_pci_addr freed_addr;
        struct rte_pci_addr vp;
 
-       if (addr == NULL)
-               goto err;
-
-       /* check whether the driver supports detach feature, or not */
-       if (rte_eth_dev_is_detachable(port_id))
-               goto err;
-
        /* get pci address by port id */
        if (rte_eth_dev_get_addr_by_port(port_id, &freed_addr))
                goto err;
@@ -555,7 +541,6 @@ rte_eth_dev_detach_pdev(uint8_t port_id, struct rte_pci_addr *addr)
        *addr = freed_addr;
        return 0;
 err:
-       RTE_LOG(ERR, EAL, "Driver, cannot detach the device\n");
        return -1;
 }
 
@@ -566,9 +551,6 @@ rte_eth_dev_attach_vdev(const char *vdevargs, uint8_t *port_id)
        char *name = NULL, *args = NULL;
        int ret = -1;
 
-       if ((vdevargs == NULL) || (port_id == NULL))
-               goto end;
-
        /* parse vdevargs, then retrieve device name and args */
        if (rte_eal_parse_devargs_str(vdevargs, &name, &args))
                goto end;
@@ -586,13 +568,9 @@ rte_eth_dev_attach_vdev(const char *vdevargs, uint8_t *port_id)
 
        ret = 0;
 end:
-       if (name)
-               free(name);
-       if (args)
-               free(args);
+       free(name);
+       free(args);
 
-       if (ret < 0)
-               RTE_LOG(ERR, EAL, "Driver, cannot attach the device\n");
        return ret;
 }
 
@@ -602,13 +580,6 @@ rte_eth_dev_detach_vdev(uint8_t port_id, char *vdevname)
 {
        char name[RTE_ETH_NAME_MAX_LEN];
 
-       if (vdevname == NULL)
-               goto err;
-
-       /* check whether the driver supports detach feature, or not */
-       if (rte_eth_dev_is_detachable(port_id))
-               goto err;
-
        /* get device name by port id */
        if (rte_eth_dev_get_name_by_port(port_id, name))
                goto err;
@@ -620,7 +591,6 @@ rte_eth_dev_detach_vdev(uint8_t port_id, char *vdevname)
        strncpy(vdevname, name, sizeof(name));
        return 0;
 err:
-       RTE_LOG(ERR, EAL, "Driver, cannot detach the device\n");
        return -1;
 }
 
@@ -629,14 +599,27 @@ int
 rte_eth_dev_attach(const char *devargs, uint8_t *port_id)
 {
        struct rte_pci_addr addr;
+       int ret = -1;
 
-       if ((devargs == NULL) || (port_id == NULL))
-               return -EINVAL;
+       if ((devargs == NULL) || (port_id == NULL)) {
+               ret = -EINVAL;
+               goto err;
+       }
 
-       if (eal_parse_pci_DomBDF(devargs, &addr) == 0)
-               return rte_eth_dev_attach_pdev(&addr, port_id);
-       else
-               return rte_eth_dev_attach_vdev(devargs, port_id);
+       if (eal_parse_pci_DomBDF(devargs, &addr) == 0) {
+               ret = rte_eth_dev_attach_pdev(&addr, port_id);
+               if (ret < 0)
+                       goto err;
+       } else {
+               ret = rte_eth_dev_attach_vdev(devargs, port_id);
+               if (ret < 0)
+                       goto err;
+       }
+
+       return 0;
+err:
+       RTE_LOG(ERR, EAL, "Driver, cannot attach the device\n");
+       return ret;
 }
 
 /* detach the device, then store the name of the device */
@@ -644,26 +627,41 @@ int
 rte_eth_dev_detach(uint8_t port_id, char *name)
 {
        struct rte_pci_addr addr;
-       int ret;
+       int ret = -1;
 
-       if (name == NULL)
-               return -EINVAL;
+       if (name == NULL) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       /* check whether the driver supports detach feature, or not */
+       if (rte_eth_dev_is_detachable(port_id))
+               goto err;
 
        if (rte_eth_dev_get_device_type(port_id) == RTE_ETH_DEV_PCI) {
                ret = rte_eth_dev_get_addr_by_port(port_id, &addr);
                if (ret < 0)
-                       return ret;
+                       goto err;
 
                ret = rte_eth_dev_detach_pdev(port_id, &addr);
-               if (ret == 0)
-                       snprintf(name, RTE_ETH_NAME_MAX_LEN,
-                               "%04x:%02x:%02x.%d",
-                               addr.domain, addr.bus,
-                               addr.devid, addr.function);
+               if (ret < 0)
+                       goto err;
 
-               return ret;
-       } else
-               return rte_eth_dev_detach_vdev(port_id, name);
+               snprintf(name, RTE_ETH_NAME_MAX_LEN,
+                       "%04x:%02x:%02x.%d",
+                       addr.domain, addr.bus,
+                       addr.devid, addr.function);
+       } else {
+               ret = rte_eth_dev_detach_vdev(port_id, name);
+               if (ret < 0)
+                       goto err;
+       }
+
+       return 0;
+
+err:
+       RTE_LOG(ERR, EAL, "Driver, cannot detach the device\n");
+       return ret;
 }
 
 static int
@@ -673,7 +671,7 @@ rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
        void **rxq;
        unsigned i;
 
-       if (dev->data->rx_queues == NULL) { /* first time configuration */
+       if (dev->data->rx_queues == NULL && nb_queues != 0) { /* first time configuration */
                dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
                                sizeof(dev->data->rx_queues[0]) * nb_queues,
                                RTE_CACHE_LINE_SIZE);
@@ -681,7 +679,7 @@ rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
                        dev->data->nb_rx_queues = 0;
                        return -(ENOMEM);
                }
-       } else { /* re-configure */
+       } else if (dev->data->rx_queues != NULL && nb_queues != 0) { /* re-configure */
                RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
 
                rxq = dev->data->rx_queues;
@@ -701,6 +699,13 @@ rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
 
                dev->data->rx_queues = rxq;
 
+       } else if (dev->data->rx_queues != NULL && nb_queues == 0) {
+               RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
+
+               rxq = dev->data->rx_queues;
+
+               for (i = nb_queues; i < old_nb_queues; i++)
+                       (*dev->dev_ops->rx_queue_release)(rxq[i]);
        }
        dev->data->nb_rx_queues = nb_queues;
        return 0;
@@ -711,10 +716,6 @@ rte_eth_dev_rx_queue_start(uint8_t port_id, uint16_t rx_queue_id)
 {
        struct rte_eth_dev *dev;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -741,10 +742,6 @@ rte_eth_dev_rx_queue_stop(uint8_t port_id, uint16_t rx_queue_id)
 {
        struct rte_eth_dev *dev;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -771,10 +768,6 @@ rte_eth_dev_tx_queue_start(uint8_t port_id, uint16_t tx_queue_id)
 {
        struct rte_eth_dev *dev;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -801,10 +794,6 @@ rte_eth_dev_tx_queue_stop(uint8_t port_id, uint16_t tx_queue_id)
 {
        struct rte_eth_dev *dev;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -833,7 +822,7 @@ rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
        void **txq;
        unsigned i;
 
-       if (dev->data->tx_queues == NULL) { /* first time configuration */
+       if (dev->data->tx_queues == NULL && nb_queues != 0) { /* first time configuration */
                dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
                                                   sizeof(dev->data->tx_queues[0]) * nb_queues,
                                                   RTE_CACHE_LINE_SIZE);
@@ -841,7 +830,7 @@ rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
                        dev->data->nb_tx_queues = 0;
                        return -(ENOMEM);
                }
-       } else { /* re-configure */
+       } else if (dev->data->tx_queues != NULL && nb_queues != 0) { /* re-configure */
                RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
 
                txq = dev->data->tx_queues;
@@ -861,6 +850,13 @@ rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
 
                dev->data->tx_queues = txq;
 
+       } else if (dev->data->tx_queues != NULL && nb_queues == 0) {
+               RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
+
+               txq = dev->data->tx_queues;
+
+               for (i = nb_queues; i < old_nb_queues; i++)
+                       (*dev->dev_ops->tx_queue_release)(txq[i]);
        }
        dev->data->nb_tx_queues = nb_queues;
        return 0;
@@ -874,10 +870,6 @@ rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
        struct rte_eth_dev_info dev_info;
        int diag;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        if (nb_rx_q > RTE_MAX_QUEUES_PER_PORT) {
@@ -911,25 +903,23 @@ rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
         * configured device.
         */
        (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
+
+       if (nb_rx_q == 0 && nb_tx_q == 0) {
+               RTE_PMD_DEBUG_TRACE("ethdev port_id=%d both rx and tx queue cannot be 0\n", port_id);
+               return -EINVAL;
+       }
+
        if (nb_rx_q > dev_info.max_rx_queues) {
                RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
                                port_id, nb_rx_q, dev_info.max_rx_queues);
                return -EINVAL;
        }
-       if (nb_rx_q == 0) {
-               RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0\n", port_id);
-               return -EINVAL;
-       }
 
        if (nb_tx_q > dev_info.max_tx_queues) {
                RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
                                port_id, nb_tx_q, dev_info.max_tx_queues);
                return -EINVAL;
        }
-       if (nb_tx_q == 0) {
-               RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0\n", port_id);
-               return -EINVAL;
-       }
 
        /* Copy the dev_conf parameter into the dev structure */
        memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
@@ -1059,10 +1049,6 @@ rte_eth_dev_start(uint8_t port_id)
        struct rte_eth_dev *dev;
        int diag;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -1096,10 +1082,6 @@ rte_eth_dev_stop(uint8_t port_id)
 {
        struct rte_eth_dev *dev;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_RET();
-
        RTE_ETH_VALID_PORTID_OR_RET(port_id);
        dev = &rte_eth_devices[port_id];
 
@@ -1121,10 +1103,6 @@ rte_eth_dev_set_link_up(uint8_t port_id)
 {
        struct rte_eth_dev *dev;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -1138,10 +1116,6 @@ rte_eth_dev_set_link_down(uint8_t port_id)
 {
        struct rte_eth_dev *dev;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -1155,10 +1129,6 @@ rte_eth_dev_close(uint8_t port_id)
 {
        struct rte_eth_dev *dev;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_RET();
-
        RTE_ETH_VALID_PORTID_OR_RET(port_id);
        dev = &rte_eth_devices[port_id];
 
@@ -1183,10 +1153,6 @@ rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
        struct rte_eth_dev *dev;
        struct rte_eth_dev_info dev_info;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -1266,10 +1232,6 @@ rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
        struct rte_eth_dev *dev;
        struct rte_eth_dev_info dev_info;
 
-       /* This function is only safe when called from the primary process
-        * in a multi-process setup*/
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
@@ -1308,6 +1270,52 @@ rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
                                               socket_id, tx_conf);
 }
 
+void
+rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
+               void *userdata __rte_unused)
+{
+       unsigned i;
+
+       for (i = 0; i < unsent; i++)
+               rte_pktmbuf_free(pkts[i]);
+}
+
+void
+rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
+               void *userdata)
+{
+       uint64_t *count = userdata;
+       unsigned i;
+
+       for (i = 0; i < unsent; i++)
+               rte_pktmbuf_free(pkts[i]);
+
+       *count += unsent;
+}
+
+int
+rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
+               buffer_tx_error_fn cbfn, void *userdata)
+{
+       buffer->error_callback = cbfn;
+       buffer->error_userdata = userdata;
+       return 0;
+}
+
+int
+rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
+{
+       if (buffer == NULL)
+               return -EINVAL;
+
+       buffer->size = size;
+       if (buffer->error_callback == NULL)
+               rte_eth_tx_buffer_set_err_callback(buffer,
+                               rte_eth_tx_buffer_drop_callback, NULL);
+
+       return 0;
+}
+
 void
 rte_eth_promiscuous_enable(uint8_t port_id)
 {
@@ -1456,6 +1464,7 @@ rte_eth_stats_reset(uint8_t port_id)
 
        RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
        (*dev->dev_ops->stats_reset)(dev);
+       dev->data->rx_mbuf_alloc_failed = 0;
 }
 
 /* retrieve ethdev extended statistics */
@@ -1694,16 +1703,17 @@ rte_eth_dev_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int o
 }
 
 int
-rte_eth_dev_set_vlan_ether_type(uint8_t port_id, uint16_t tpid)
+rte_eth_dev_set_vlan_ether_type(uint8_t port_id,
+                               enum rte_vlan_type vlan_type,
+                               uint16_t tpid)
 {
        struct rte_eth_dev *dev;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
        dev = &rte_eth_devices[port_id];
        RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
-       (*dev->dev_ops->vlan_tpid_set)(dev, tpid);
 
-       return 0;
+       return (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type, tpid);
 }
 
 int
@@ -1856,7 +1866,7 @@ rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
 static int
 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
                         uint16_t reta_size,
-                        uint8_t max_rxq)
+                        uint16_t max_rxq)
 {
        uint16_t i, idx, shift;
 
@@ -2478,7 +2488,7 @@ rte_eth_dev_callback_register(uint8_t port_id,
        /* create a new callback. */
        if (user_cb == NULL)
                user_cb = rte_zmalloc("INTR_USER_CALLBACK",
-                                     sizeof(struct rte_eth_dev_callback), 0);
+                                       sizeof(struct rte_eth_dev_callback), 0);
        if (user_cb != NULL) {
                user_cb->cb_fn = cb_fn;
                user_cb->cb_arg = cb_arg;
@@ -2605,7 +2615,7 @@ rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
        if (mz)
                return mz;
 
-       if (is_xen_dom0_supported())
+       if (rte_xen_dom0_supported())
                return rte_memzone_reserve_bounded(z_name, size, socket_id,
                                                   0, align, RTE_PGSIZE_2M);
        else