dpdk: refactor setup, start, stop code
[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 clib_error_t *
28 dpdk_error_return (clib_error_t * error, char *str, dpdk_device_t * xd,
29                    int rv)
30 {
31   return clib_error_return (error, "%s[%d]: %s(%d)", str, xd->device_index,
32                             rte_strerror (rv), rv);
33 }
34
35 clib_error_t *
36 dpdk_device_setup (dpdk_device_t * xd)
37 {
38   dpdk_main_t *dm = &dpdk_main;
39   clib_error_t *err = 0;
40   int rv;
41   int j;
42
43   ASSERT (vlib_get_thread_index () == 0);
44
45   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
46     {
47       vnet_hw_interface_set_flags (dm->vnet_main, xd->hw_if_index, 0);
48       dpdk_device_stop (xd);
49     }
50
51   rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
52                               xd->tx_q_used, &xd->port_conf);
53
54   if (rv < 0)
55     return dpdk_error_return (err, "rte_eth_dev_configure", xd, rv);
56
57   /* Set up one TX-queue per worker thread */
58   for (j = 0; j < xd->tx_q_used; j++)
59     {
60       rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
61                                    xd->cpu_socket, &xd->tx_conf);
62
63       /* retry with any other CPU socket */
64       if (rv < 0)
65         rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
66                                      SOCKET_ID_ANY, &xd->tx_conf);
67       if (rv < 0)
68         err = dpdk_error_return (err, "rte_eth_tx_queue_setup", xd, rv);
69     }
70
71   for (j = 0; j < xd->rx_q_used; j++)
72     {
73       uword tidx = vnet_get_device_input_thread_index (dm->vnet_main,
74                                                        xd->hw_if_index, j);
75       unsigned lcore = vlib_worker_threads[tidx].lcore_id;
76       u16 socket_id = rte_lcore_to_socket_id (lcore);
77
78       rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
79                                    xd->cpu_socket, 0,
80                                    dm->pktmbuf_pools[socket_id]);
81
82       /* retry with any other CPU socket */
83       if (rv < 0)
84         rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
85                                      SOCKET_ID_ANY, 0,
86                                      dm->pktmbuf_pools[socket_id]);
87
88       if (rv < 0)
89         err = dpdk_error_return (err, "rte_eth_rx_queue_setup", xd, rv);
90     }
91
92   if (err)
93     return err;
94
95   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
96     err = dpdk_device_start (xd);
97
98   return err;
99 }
100
101 clib_error_t *
102 dpdk_device_start (dpdk_device_t * xd)
103 {
104   int rv;
105   clib_error_t *err = 0;
106
107   rv = rte_eth_dev_start (xd->device_index);
108
109   if (rv)
110     return dpdk_error_return (err, "rte_eth_dev_start", xd, rv);
111
112   if (xd->default_mac_address)
113     rv =
114       rte_eth_dev_default_mac_addr_set (xd->device_index,
115                                         (struct ether_addr *)
116                                         xd->default_mac_address);
117
118   if (rv)
119     err = dpdk_error_return (err, "rte_eth_dev_default_mac_addr_set", xd, rv);
120
121   if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
122     rte_eth_promiscuous_enable (xd->device_index);
123   else
124     rte_eth_promiscuous_disable (xd->device_index);
125
126   rte_eth_allmulticast_enable (xd->device_index);
127
128   if (xd->pmd == VNET_DPDK_PMD_BOND)
129     {
130       u8 slink[16];
131       int nlink = rte_eth_bond_slaves_get (xd->device_index, slink, 16);
132       while (nlink >= 1)
133         {
134           u8 dpdk_port = slink[--nlink];
135           rte_eth_allmulticast_enable (dpdk_port);
136         }
137     }
138
139   return err;
140 }
141
142 clib_error_t *
143 dpdk_device_stop (dpdk_device_t * xd)
144 {
145   rte_eth_dev_stop (xd->device_index);
146
147   return 0;
148 }
149
150 /*
151  * fd.io coding-style-patch-verification: ON
152  *
153  * Local Variables:
154  * eval: (c-set-style "gnu")
155  * End:
156  */