2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/format.h>
18 #include <vlib/unix/cj.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <dpdk/device/dpdk.h>
24 #include <dpdk/device/dpdk_priv.h>
25 #include <vppinfra/error.h>
28 dpdk_device_error (dpdk_device_t * xd, char *str, int rv)
30 xd->errors = clib_error_return (xd->errors, "%s[port:%d, errno:%d]: %s",
31 str, xd->device_index, rv,
36 dpdk_device_setup (dpdk_device_t * xd)
38 dpdk_main_t *dm = &dpdk_main;
39 vnet_main_t *vnm = vnet_get_main ();
40 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, xd->vlib_sw_if_index);
44 ASSERT (vlib_get_thread_index () == 0);
46 clib_error_free (xd->errors);
47 sw->flags &= ~VNET_SW_INTERFACE_FLAG_ERROR;
49 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
51 vnet_hw_interface_set_flags (dm->vnet_main, xd->hw_if_index, 0);
52 dpdk_device_stop (xd);
55 rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
56 xd->tx_q_used, &xd->port_conf);
60 dpdk_device_error (xd, "rte_eth_dev_configure", rv);
64 /* Set up one TX-queue per worker thread */
65 for (j = 0; j < xd->tx_q_used; j++)
67 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
68 xd->cpu_socket, &xd->tx_conf);
70 /* retry with any other CPU socket */
72 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
73 SOCKET_ID_ANY, &xd->tx_conf);
75 dpdk_device_error (xd, "rte_eth_tx_queue_setup", rv);
78 for (j = 0; j < xd->rx_q_used; j++)
80 uword tidx = vnet_get_device_input_thread_index (dm->vnet_main,
82 unsigned lcore = vlib_worker_threads[tidx].lcore_id;
83 u16 socket_id = rte_lcore_to_socket_id (lcore);
85 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
87 dm->pktmbuf_pools[socket_id]);
89 /* retry with any other CPU socket */
91 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
93 dm->pktmbuf_pools[socket_id]);
96 dpdk_device_error (xd, "rte_eth_rx_queue_setup", rv);
99 if (vec_len (xd->errors))
102 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
103 dpdk_device_start (xd);
105 if (vec_len (xd->errors))
111 xd->flags |= DPDK_DEVICE_FLAG_PMD_INIT_FAIL;
112 sw->flags |= VNET_SW_INTERFACE_FLAG_ERROR;
116 dpdk_device_start (dpdk_device_t * xd)
120 if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
123 rv = rte_eth_dev_start (xd->device_index);
127 dpdk_device_error (xd, "rte_eth_dev_start", rv);
131 if (xd->default_mac_address)
133 rte_eth_dev_default_mac_addr_set (xd->device_index,
134 (struct ether_addr *)
135 xd->default_mac_address);
138 dpdk_device_error (xd, "rte_eth_dev_default_mac_addr_set", rv);
140 if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
141 rte_eth_promiscuous_enable (xd->device_index);
143 rte_eth_promiscuous_disable (xd->device_index);
145 rte_eth_allmulticast_enable (xd->device_index);
147 if (xd->pmd == VNET_DPDK_PMD_BOND)
150 int nlink = rte_eth_bond_slaves_get (xd->device_index, slink, 16);
153 u8 dpdk_port = slink[--nlink];
154 rte_eth_allmulticast_enable (dpdk_port);
160 dpdk_device_stop (dpdk_device_t * xd)
162 if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
165 rte_eth_allmulticast_disable (xd->device_index);
166 rte_eth_dev_stop (xd->device_index);
168 /* For bonded interface, stop slave links */
169 if (xd->pmd == VNET_DPDK_PMD_BOND)
172 int nlink = rte_eth_bond_slaves_get (xd->device_index, slink, 16);
175 u8 dpdk_port = slink[--nlink];
176 rte_eth_dev_stop (dpdk_port);
182 * fd.io coding-style-patch-verification: ON
185 * eval: (c-set-style "gnu")