dpdk: improve error handling during device initialization
[vpp.git] / src / plugins / dpdk / device / common.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/format.h>
18 #include <vlib/unix/cj.h>
19 #include <assert.h>
20
21 #include <vnet/ethernet/ethernet.h>
22 #include <dpdk/device/dpdk.h>
23
24 #include <dpdk/device/dpdk_priv.h>
25 #include <vppinfra/error.h>
26
27 void
28 dpdk_device_error (dpdk_device_t * xd, char *str, int rv)
29 {
30   xd->errors = clib_error_return (xd->errors, "%s[port:%d, errno:%d]: %s",
31                                   str, xd->device_index, rv,
32                                   rte_strerror (rv));
33 }
34
35 void
36 dpdk_device_setup (dpdk_device_t * xd)
37 {
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);
41   int rv;
42   int j;
43
44   ASSERT (vlib_get_thread_index () == 0);
45
46   clib_error_free (xd->errors);
47   sw->flags &= ~VNET_SW_INTERFACE_FLAG_ERROR;
48
49   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
50     {
51       vnet_hw_interface_set_flags (dm->vnet_main, xd->hw_if_index, 0);
52       dpdk_device_stop (xd);
53     }
54
55   rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
56                               xd->tx_q_used, &xd->port_conf);
57
58   if (rv < 0)
59     {
60       dpdk_device_error (xd, "rte_eth_dev_configure", rv);
61       goto error;
62     }
63
64   /* Set up one TX-queue per worker thread */
65   for (j = 0; j < xd->tx_q_used; j++)
66     {
67       rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
68                                    xd->cpu_socket, &xd->tx_conf);
69
70       /* retry with any other CPU socket */
71       if (rv < 0)
72         rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
73                                      SOCKET_ID_ANY, &xd->tx_conf);
74       if (rv < 0)
75         dpdk_device_error (xd, "rte_eth_tx_queue_setup", rv);
76     }
77
78   for (j = 0; j < xd->rx_q_used; j++)
79     {
80       uword tidx = vnet_get_device_input_thread_index (dm->vnet_main,
81                                                        xd->hw_if_index, j);
82       unsigned lcore = vlib_worker_threads[tidx].lcore_id;
83       u16 socket_id = rte_lcore_to_socket_id (lcore);
84
85       rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
86                                    xd->cpu_socket, 0,
87                                    dm->pktmbuf_pools[socket_id]);
88
89       /* retry with any other CPU socket */
90       if (rv < 0)
91         rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
92                                      SOCKET_ID_ANY, 0,
93                                      dm->pktmbuf_pools[socket_id]);
94
95       if (rv < 0)
96         dpdk_device_error (xd, "rte_eth_rx_queue_setup", rv);
97     }
98
99   if (vec_len (xd->errors))
100     goto error;
101
102   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
103     dpdk_device_start (xd);
104
105   if (vec_len (xd->errors))
106     goto error;
107
108   return;
109
110 error:
111   xd->flags |= DPDK_DEVICE_FLAG_PMD_INIT_FAIL;
112   sw->flags |= VNET_SW_INTERFACE_FLAG_ERROR;
113 }
114
115 void
116 dpdk_device_start (dpdk_device_t * xd)
117 {
118   int rv;
119
120   if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
121     return;
122
123   rv = rte_eth_dev_start (xd->device_index);
124
125   if (rv)
126     {
127       dpdk_device_error (xd, "rte_eth_dev_start", rv);
128       return;
129     }
130
131   if (xd->default_mac_address)
132     rv =
133       rte_eth_dev_default_mac_addr_set (xd->device_index,
134                                         (struct ether_addr *)
135                                         xd->default_mac_address);
136
137   if (rv)
138     dpdk_device_error (xd, "rte_eth_dev_default_mac_addr_set", rv);
139
140   if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
141     rte_eth_promiscuous_enable (xd->device_index);
142   else
143     rte_eth_promiscuous_disable (xd->device_index);
144
145   rte_eth_allmulticast_enable (xd->device_index);
146
147   if (xd->pmd == VNET_DPDK_PMD_BOND)
148     {
149       u8 slink[16];
150       int nlink = rte_eth_bond_slaves_get (xd->device_index, slink, 16);
151       while (nlink >= 1)
152         {
153           u8 dpdk_port = slink[--nlink];
154           rte_eth_allmulticast_enable (dpdk_port);
155         }
156     }
157 }
158
159 void
160 dpdk_device_stop (dpdk_device_t * xd)
161 {
162   if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
163     return;
164
165   rte_eth_allmulticast_disable (xd->device_index);
166   rte_eth_dev_stop (xd->device_index);
167
168   /* For bonded interface, stop slave links */
169   if (xd->pmd == VNET_DPDK_PMD_BOND)
170     {
171       u8 slink[16];
172       int nlink = rte_eth_bond_slaves_get (xd->device_index, slink, 16);
173       while (nlink >= 1)
174         {
175           u8 dpdk_port = slink[--nlink];
176           rte_eth_dev_stop (dpdk_port);
177         }
178     }
179 }
180
181 /*
182  * fd.io coding-style-patch-verification: ON
183  *
184  * Local Variables:
185  * eval: (c-set-style "gnu")
186  * End:
187  */