blob: 4c040d20af7f0e2a79ac3fdc7f78f17e6e80ba4d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 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/error.h>
18#include <vppinfra/format.h>
John Lobcebbb92016-04-05 15:47:43 -040019#include <vppinfra/bitmap.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020
21#include <vnet/ethernet/ethernet.h>
22#include <vnet/devices/dpdk/dpdk.h>
23#include <vlib/unix/physmem.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020024#include <vlib/pci/pci.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <sys/stat.h>
30#include <sys/mount.h>
31#include <string.h>
32#include <fcntl.h>
33
34#include "dpdk_priv.h"
35
36dpdk_main_t dpdk_main;
37
38/* force linker to link functions used by vlib and declared weak */
39void *vlib_weakly_linked_functions[] = {
40 &rte_pktmbuf_init,
41 &rte_pktmbuf_pool_init,
42};
43
44#define LINK_STATE_ELOGS 0
45
46#define DEFAULT_HUGE_DIR "/run/vpp/hugepages"
47#define VPP_RUN_DIR "/run/vpp"
48
49/* Port configuration, mildly modified Intel app values */
50
51static struct rte_eth_conf port_conf_template = {
52 .rxmode = {
Damjan Marion25665672016-08-16 18:56:08 +020053 .split_hdr_size = 0,
54 .header_split = 0, /**< Header Split disabled */
55 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
56 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
57 .hw_strip_crc = 0, /**< CRC stripped by hardware */
58 },
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 .txmode = {
Damjan Marion25665672016-08-16 18:56:08 +020060 .mq_mode = ETH_MQ_TX_NONE,
61 },
Ed Warnickecb9cada2015-12-08 15:45:58 -070062};
63
64clib_error_t *
65dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd)
66{
Ed Warnickecb9cada2015-12-08 15:45:58 -070067 int rv;
68 int j;
69
Damjan Marion25665672016-08-16 18:56:08 +020070 ASSERT (os_get_cpu_number () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
Damjan Marionb28e4982016-08-22 22:34:38 +020072 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Damjan Marion25665672016-08-16 18:56:08 +020073 {
74 vnet_hw_interface_set_flags (dm->vnet_main, xd->vlib_hw_if_index, 0);
75 rte_eth_dev_stop (xd->device_index);
76 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
78 rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
Damjan Marion25665672016-08-16 18:56:08 +020079 xd->tx_q_used, &xd->port_conf);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81 if (rv < 0)
82 return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
Damjan Marion25665672016-08-16 18:56:08 +020083 xd->device_index, rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
85 /* Set up one TX-queue per worker thread */
86 for (j = 0; j < xd->tx_q_used; j++)
87 {
Damjan Marion25665672016-08-16 18:56:08 +020088 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
89 xd->cpu_socket, &xd->tx_conf);
Damjan Marionae605262016-04-21 21:42:40 +020090
91 /* retry with any other CPU socket */
92 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +020093 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
94 SOCKET_ID_ANY, &xd->tx_conf);
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +020096 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 }
98
Damjan Marion25665672016-08-16 18:56:08 +020099 if (rv < 0)
100 return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
101 xd->device_index, rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
103 for (j = 0; j < xd->rx_q_used; j++)
104 {
105
Damjan Marion25665672016-08-16 18:56:08 +0200106 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
107 xd->cpu_socket, 0,
Damjan Marion878c6092017-01-04 13:19:27 +0100108 dm->
Damjan Marion25665672016-08-16 18:56:08 +0200109 pktmbuf_pools[xd->cpu_socket_id_by_queue
110 [j]]);
Damjan Marionae605262016-04-21 21:42:40 +0200111
112 /* retry with any other CPU socket */
113 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +0200114 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
115 SOCKET_ID_ANY, 0,
Damjan Marion878c6092017-01-04 13:19:27 +0100116 dm->
Damjan Marion25665672016-08-16 18:56:08 +0200117 pktmbuf_pools[xd->cpu_socket_id_by_queue
118 [j]]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +0200120 return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
121 xd->device_index, rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 }
123
Damjan Marionb28e4982016-08-22 22:34:38 +0200124 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Damjan Marion25665672016-08-16 18:56:08 +0200125 {
Dave Barachdd522cb2016-08-10 16:56:16 -0400126 int rv;
127 rv = rte_eth_dev_start (xd->device_index);
128 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +0200129 clib_warning ("rte_eth_dev_start %d returned %d",
130 xd->device_index, rv);
131 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 return 0;
133}
134
Damjan Marion25665672016-08-16 18:56:08 +0200135static u32
136dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137{
Damjan Marion25665672016-08-16 18:56:08 +0200138 dpdk_main_t *dm = &dpdk_main;
139 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 u32 old = 0;
141
Damjan Marion25665672016-08-16 18:56:08 +0200142 if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 {
Damjan Marionb28e4982016-08-22 22:34:38 +0200144 old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
Damjan Marion25665672016-08-16 18:56:08 +0200145
Damjan Marionb28e4982016-08-22 22:34:38 +0200146 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
147 xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
148 else
149 xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
150
151 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 {
Damjan Marionb28e4982016-08-22 22:34:38 +0200153 if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
Damjan Marion25665672016-08-16 18:56:08 +0200154 rte_eth_promiscuous_enable (xd->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155 else
Damjan Marion25665672016-08-16 18:56:08 +0200156 rte_eth_promiscuous_disable (xd->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 }
158 }
Damjan Marion25665672016-08-16 18:56:08 +0200159 else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU (flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 {
161 /*
162 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
Damjan Marion25665672016-08-16 18:56:08 +0200163 * driver to dynamically change the mtu. If/when the
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 * VIC firmware gets fixed, then this should be removed.
165 */
Damjan Marion3a944752016-07-06 23:13:39 +0200166 if (xd->pmd == VNET_DPDK_PMD_ENIC)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 {
168 struct rte_eth_dev_info dev_info;
169
170 /*
171 * Restore mtu to what has been set by CIMC in the firmware cfg.
172 */
Damjan Marion25665672016-08-16 18:56:08 +0200173 rte_eth_dev_info_get (xd->device_index, &dev_info);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174 hi->max_packet_bytes = dev_info.max_rx_pktlen;
175
Damjan Marion25665672016-08-16 18:56:08 +0200176 vlib_cli_output (vlib_get_main (),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 "Cisco VIC mtu can only be changed "
178 "using CIMC then rebooting the server!");
179 }
180 else
181 {
182 int rv;
Damjan Marion25665672016-08-16 18:56:08 +0200183
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
185
Damjan Marionb28e4982016-08-22 22:34:38 +0200186 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 rte_eth_dev_stop (xd->device_index);
188
Damjan Marion25665672016-08-16 18:56:08 +0200189 rv = rte_eth_dev_configure
190 (xd->device_index, xd->rx_q_used, xd->tx_q_used, &xd->port_conf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
192 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +0200193 vlib_cli_output (vlib_get_main (),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 "rte_eth_dev_configure[%d]: err %d",
195 xd->device_index, rv);
196
Damjan Marion25665672016-08-16 18:56:08 +0200197 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
Damjan Marionb28e4982016-08-22 22:34:38 +0200199 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Damjan Marion25665672016-08-16 18:56:08 +0200200 {
201 int rv = rte_eth_dev_start (xd->device_index);
202 if (rv < 0)
203 clib_warning ("rte_eth_dev_start %d returned %d",
204 xd->device_index, rv);
205 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 }
207 }
208 return old;
209}
210
Damjan Marion85cdbd02016-02-12 18:00:23 +0100211void
Damjan Marion25665672016-08-16 18:56:08 +0200212dpdk_device_lock_init (dpdk_device_t * xd)
Damjan Marion85cdbd02016-02-12 18:00:23 +0100213{
214 int q;
Damjan Marion25665672016-08-16 18:56:08 +0200215 vec_validate (xd->lockp, xd->tx_q_used - 1);
Damjan Marion85cdbd02016-02-12 18:00:23 +0100216 for (q = 0; q < xd->tx_q_used; q++)
217 {
218 xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
Damjan Marion25665672016-08-16 18:56:08 +0200219 CLIB_CACHE_LINE_BYTES);
Damjan Marion85cdbd02016-02-12 18:00:23 +0100220 memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion25665672016-08-16 18:56:08 +0200221 }
Damjan Marion85cdbd02016-02-12 18:00:23 +0100222}
223
224void
Damjan Marion25665672016-08-16 18:56:08 +0200225dpdk_device_lock_free (dpdk_device_t * xd)
Damjan Marion85cdbd02016-02-12 18:00:23 +0100226{
227 int q;
228
Damjan Marion25665672016-08-16 18:56:08 +0200229 for (q = 0; q < vec_len (xd->lockp); q++)
230 clib_mem_free ((void *) xd->lockp[q]);
231 vec_free (xd->lockp);
Damjan Marion85cdbd02016-02-12 18:00:23 +0100232 xd->lockp = 0;
233}
234
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235static clib_error_t *
236dpdk_lib_init (dpdk_main_t * dm)
237{
238 u32 nports;
239 u32 nb_desc = 0;
240 int i;
Damjan Marion25665672016-08-16 18:56:08 +0200241 clib_error_t *error;
242 vlib_main_t *vm = vlib_get_main ();
243 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion25665672016-08-16 18:56:08 +0200244 vnet_sw_interface_t *sw;
245 vnet_hw_interface_t *hi;
246 dpdk_device_t *xd;
Damjan Marion32f7bc12016-07-21 03:42:37 -0700247 vlib_pci_addr_t last_pci_addr;
248 u32 last_pci_addr_port = 0;
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100249 vlib_thread_registration_t *tr, *tr_hqos;
250 uword *p, *p_hqos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100252 u32 next_cpu = 0, next_hqos_cpu = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 u8 af_packet_port_id = 0;
Damjan Marion32f7bc12016-07-21 03:42:37 -0700254 last_pci_addr.as_u32 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
256 dm->input_cpu_first_index = 0;
257 dm->input_cpu_count = 1;
258
259 /* find out which cpus will be used for input */
Damjan Marionbf741472016-06-13 22:49:44 +0200260 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
262
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 if (tr && tr->count > 0)
264 {
265 dm->input_cpu_first_index = tr->first_index;
266 dm->input_cpu_count = tr->count;
267 }
268
269 vec_validate_aligned (dm->devices_by_cpu, tm->n_vlib_mains - 1,
Damjan Marion25665672016-08-16 18:56:08 +0200270 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
272 vec_validate_aligned (dm->workers, tm->n_vlib_mains - 1,
Damjan Marion25665672016-08-16 18:56:08 +0200273 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100275 dm->hqos_cpu_first_index = 0;
276 dm->hqos_cpu_count = 0;
277
278 /* find out which cpus will be used for I/O TX */
279 p_hqos = hash_get_mem (tm->thread_registrations_by_name, "hqos-threads");
280 tr_hqos = p_hqos ? (vlib_thread_registration_t *) p_hqos[0] : 0;
281
282 if (tr_hqos && tr_hqos->count > 0)
283 {
284 dm->hqos_cpu_first_index = tr_hqos->first_index;
285 dm->hqos_cpu_count = tr_hqos->count;
286 }
287
288 vec_validate_aligned (dm->devices_by_hqos_cpu, tm->n_vlib_mains - 1,
289 CLIB_CACHE_LINE_BYTES);
290
291 vec_validate_aligned (dm->hqos_threads, tm->n_vlib_mains - 1,
292 CLIB_CACHE_LINE_BYTES);
293
Damjan Marion25665672016-08-16 18:56:08 +0200294 nports = rte_eth_dev_count ();
295 if (nports < 1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 {
297 clib_warning ("DPDK drivers found no ports...");
298 }
299
300 if (CLIB_DEBUG > 0)
301 clib_warning ("DPDK drivers found %d ports...", nports);
302
Damjan Marion25665672016-08-16 18:56:08 +0200303 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 * All buffers are all allocated from the same rte_mempool.
305 * Thus they all have the same number of data bytes.
306 */
Damjan Marion25665672016-08-16 18:56:08 +0200307 dm->vlib_buffer_free_list_index =
308 vlib_buffer_get_or_create_free_list (vm,
309 VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
310 "dpdk rx");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
Damjan Marion64ae6692016-05-25 18:40:13 +0200312 if (dm->conf->enable_tcp_udp_checksum)
313 dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
314 | IP_BUFFER_L4_CHECKSUM_COMPUTED);
315
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 for (i = 0; i < nports; i++)
317 {
318 u8 addr[6];
John Loce3e9712016-07-07 13:54:44 -0400319 u8 vlan_strip = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 int j;
321 struct rte_eth_dev_info dev_info;
Damjan Marion25665672016-08-16 18:56:08 +0200322 clib_error_t *rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 struct rte_eth_link l;
Damjan Marion25665672016-08-16 18:56:08 +0200324 dpdk_device_config_t *devconf = 0;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200325 vlib_pci_addr_t pci_addr;
Damjan Marion25665672016-08-16 18:56:08 +0200326 uword *p = 0;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200327
Damjan Marion25665672016-08-16 18:56:08 +0200328 rte_eth_dev_info_get (i, &dev_info);
329 if (dev_info.pci_dev) /* bonded interface has no pci info */
330 {
John Lo55ef1b12016-06-08 23:21:35 -0400331 pci_addr.domain = dev_info.pci_dev->addr.domain;
332 pci_addr.bus = dev_info.pci_dev->addr.bus;
333 pci_addr.slot = dev_info.pci_dev->addr.devid;
334 pci_addr.function = dev_info.pci_dev->addr.function;
Damjan Marion25665672016-08-16 18:56:08 +0200335 p =
336 hash_get (dm->conf->device_config_index_by_pci_addr,
337 pci_addr.as_u32);
338 }
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200339
340 if (p)
341 devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
342 else
343 devconf = &dm->conf->default_devconf;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
345 /* Create vnet interface */
346 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
347 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
348 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
Damjan Marion25665672016-08-16 18:56:08 +0200349 xd->cpu_socket = (i8) rte_eth_dev_socket_id (i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
Damjan Marion32f7bc12016-07-21 03:42:37 -0700351 /* Handle interface naming for devices with multiple ports sharing same PCI ID */
352 if (dev_info.pci_dev)
353 {
Damjan Marion25665672016-08-16 18:56:08 +0200354 struct rte_eth_dev_info di = { 0 };
Damjan Marion32f7bc12016-07-21 03:42:37 -0700355 rte_eth_dev_info_get (i + 1, &di);
356 if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
Damjan Marion25665672016-08-16 18:56:08 +0200357 memcmp (&dev_info.pci_dev->addr, &di.pci_dev->addr,
358 sizeof (struct rte_pci_addr)) == 0)
Damjan Marion32f7bc12016-07-21 03:42:37 -0700359 {
360 xd->interface_name_suffix = format (0, "0");
361 last_pci_addr.as_u32 = pci_addr.as_u32;
362 last_pci_addr_port = i;
363 }
364 else if (pci_addr.as_u32 == last_pci_addr.as_u32)
365 {
Damjan Marion25665672016-08-16 18:56:08 +0200366 xd->interface_name_suffix =
367 format (0, "%u", i - last_pci_addr_port);
Damjan Marion32f7bc12016-07-21 03:42:37 -0700368 }
369 else
370 {
371 last_pci_addr.as_u32 = ~0;
372 }
373 }
374 else
375 last_pci_addr.as_u32 = ~0;
376
Damjan Marion25665672016-08-16 18:56:08 +0200377 clib_memcpy (&xd->tx_conf, &dev_info.default_txconf,
378 sizeof (struct rte_eth_txconf));
Damjan Marion64ae6692016-05-25 18:40:13 +0200379 if (dm->conf->no_multi_seg)
Damjan Marion25665672016-08-16 18:56:08 +0200380 {
381 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
382 port_conf_template.rxmode.jumbo_frame = 0;
383 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 else
Damjan Marion25665672016-08-16 18:56:08 +0200385 {
386 xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
387 port_conf_template.rxmode.jumbo_frame = 1;
Damjan Marion7dc41462016-11-15 19:47:58 +0100388 xd->flags |= DPDK_DEVICE_FLAG_MAYBE_MULTISEG;
Damjan Marion25665672016-08-16 18:56:08 +0200389 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Damjan Marion25665672016-08-16 18:56:08 +0200391 clib_memcpy (&xd->port_conf, &port_conf_template,
392 sizeof (struct rte_eth_conf));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393
Damjan Marion25665672016-08-16 18:56:08 +0200394 xd->tx_q_used = clib_min (dev_info.max_tx_queues, tm->n_vlib_mains);
Damjan Marion85cdbd02016-02-12 18:00:23 +0100395
Damjan Marion25665672016-08-16 18:56:08 +0200396 if (devconf->num_tx_queues > 0
397 && devconf->num_tx_queues < xd->tx_q_used)
398 xd->tx_q_used = clib_min (xd->tx_q_used, devconf->num_tx_queues);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200400 if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
401 {
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200402 dm->use_rss = 1;
403 }
404
Damjan Marion25665672016-08-16 18:56:08 +0200405 if (devconf->num_rx_queues > 1
406 && dev_info.max_rx_queues >= devconf->num_rx_queues)
407 {
408 xd->rx_q_used = devconf->num_rx_queues;
409 xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
410 if (devconf->rss_fn == 0)
411 xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
412 ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
413 else
414 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
415 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 else
Damjan Marion25665672016-08-16 18:56:08 +0200417 xd->rx_q_used = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Damjan Marion56431702016-09-19 13:18:09 +0200419 xd->flags |= DPDK_DEVICE_FLAG_PMD;
Damjan Marion2068e982016-01-27 16:59:04 +0100420
421 /* workaround for drivers not setting driver_name */
John Lo55ef1b12016-06-08 23:21:35 -0400422 if ((!dev_info.driver_name) && (dev_info.pci_dev))
Damjan Marion39162272016-10-25 14:54:38 +0200423#if RTE_VERSION < RTE_VERSION_NUM(16, 11, 0, 0)
Damjan Marion25665672016-08-16 18:56:08 +0200424 dev_info.driver_name = dev_info.pci_dev->driver->name;
Damjan Marion39162272016-10-25 14:54:38 +0200425#else
426 dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
427#endif
Damjan Marion25665672016-08-16 18:56:08 +0200428 ASSERT (dev_info.driver_name);
Damjan Marion2068e982016-01-27 16:59:04 +0100429
Damjan Marion25665672016-08-16 18:56:08 +0200430 if (!xd->pmd)
431 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432
433
Dave Barachdd522cb2016-08-10 16:56:16 -0400434#define _(s,f) else if (dev_info.driver_name && \
435 !strcmp(dev_info.driver_name, s)) \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 xd->pmd = VNET_DPDK_PMD_##f;
Damjan Marion25665672016-08-16 18:56:08 +0200437 if (0)
438 ;
439 foreach_dpdk_pmd
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440#undef _
Damjan Marion25665672016-08-16 18:56:08 +0200441 else
442 xd->pmd = VNET_DPDK_PMD_UNKNOWN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Damjan Marion696f1ad2016-12-23 22:42:41 +0100444 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
445 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
446 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
Damjan Marion25665672016-08-16 18:56:08 +0200448 switch (xd->pmd)
449 {
450 /* 1G adapters */
451 case VNET_DPDK_PMD_E1000EM:
452 case VNET_DPDK_PMD_IGB:
453 case VNET_DPDK_PMD_IGBVF:
454 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
455 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
Damjan Marion25665672016-08-16 18:56:08 +0200457 /* 10G adapters */
458 case VNET_DPDK_PMD_IXGBE:
459 case VNET_DPDK_PMD_IXGBEVF:
460 case VNET_DPDK_PMD_THUNDERX:
461 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
Damjan Marion25665672016-08-16 18:56:08 +0200462 break;
463 case VNET_DPDK_PMD_DPAA2:
464 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
465 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
Damjan Marion25665672016-08-16 18:56:08 +0200467 /* Cisco VIC */
468 case VNET_DPDK_PMD_ENIC:
469 rte_eth_link_get_nowait (i, &l);
John Loa60d4cb2016-12-17 03:09:58 -0500470 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
Damjan Marion25665672016-08-16 18:56:08 +0200471 if (l.link_speed == 40000)
Damjan Marion696f1ad2016-12-23 22:42:41 +0100472 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
Damjan Marion25665672016-08-16 18:56:08 +0200473 else
Damjan Marion696f1ad2016-12-23 22:42:41 +0100474 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
Damjan Marion25665672016-08-16 18:56:08 +0200475 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476
Damjan Marion25665672016-08-16 18:56:08 +0200477 /* Intel Fortville */
478 case VNET_DPDK_PMD_I40E:
479 case VNET_DPDK_PMD_I40EVF:
John Loa60d4cb2016-12-17 03:09:58 -0500480 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
Damjan Marion25665672016-08-16 18:56:08 +0200481 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482
Damjan Marion25665672016-08-16 18:56:08 +0200483 switch (dev_info.pci_dev->id.device_id)
484 {
485 case I40E_DEV_ID_10G_BASE_T:
486 case I40E_DEV_ID_SFP_XL710:
487 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
488 break;
489 case I40E_DEV_ID_QSFP_A:
490 case I40E_DEV_ID_QSFP_B:
491 case I40E_DEV_ID_QSFP_C:
492 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
493 break;
494 case I40E_DEV_ID_VF:
495 rte_eth_link_get_nowait (i, &l);
496 xd->port_type = l.link_speed == 10000 ?
497 VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
498 break;
499 default:
500 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
501 }
502 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
Damjan Marion25665672016-08-16 18:56:08 +0200504 case VNET_DPDK_PMD_CXGBE:
505 switch (dev_info.pci_dev->id.device_id)
506 {
507 case 0x540d: /* T580-CR */
508 case 0x5410: /* T580-LP-cr */
Damjan Marion25665672016-08-16 18:56:08 +0200509 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
510 break;
511 case 0x5403: /* T540-CR */
Damjan Marion25665672016-08-16 18:56:08 +0200512 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
513 break;
514 default:
Damjan Marion25665672016-08-16 18:56:08 +0200515 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
516 }
517 break;
Damjan Marion2068e982016-01-27 16:59:04 +0100518
Damjan Marion696f1ad2016-12-23 22:42:41 +0100519 case VNET_DPDK_PMD_MLX5:
520 {
521 char *pn_100g[] = { "MCX415A-CCAT", "MCX416A-CCAT", 0 };
522 char *pn_40g[] = { "MCX413A-BCAT", "MCX414A-BCAT",
523 "MCX415A-BCAT", "MCX416A-BCAT", "MCX4131A-BCAT", 0
524 };
525 char *pn_10g[] = { "MCX4111A-XCAT", "MCX4121A-XCAT", 0 };
526
527 vlib_pci_device_t *pd = vlib_get_pci_device (&pci_addr);
528 u8 *pn = 0;
529 char **c;
530 int found = 0;
531 pn = format (0, "%U%c",
532 format_vlib_pci_vpd, pd->vpd_r, "PN", 0);
533
534 if (!pn)
535 break;
536
537 c = pn_100g;
538 while (!found && c[0])
539 {
540 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
541 {
542 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_100G;
543 break;
544 }
545 c++;
546 }
547
548 c = pn_40g;
549 while (!found && c[0])
550 {
551 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
552 {
553 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
554 break;
555 }
556 c++;
557 }
558
559 c = pn_10g;
560 while (!found && c[0])
561 {
562 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
563 {
564 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
565 break;
566 }
567 c++;
568 }
569
570 vec_free (pn);
571 }
572
573 break;
Damjan Marion25665672016-08-16 18:56:08 +0200574 /* Intel Red Rock Canyon */
575 case VNET_DPDK_PMD_FM10K:
576 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
Damjan Marion25665672016-08-16 18:56:08 +0200577 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578
Damjan Marion25665672016-08-16 18:56:08 +0200579 /* virtio */
580 case VNET_DPDK_PMD_VIRTIO:
581 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
582 xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
583 xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
584 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Damjan Marion25665672016-08-16 18:56:08 +0200586 /* vmxnet3 */
587 case VNET_DPDK_PMD_VMXNET3:
588 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
589 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
590 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
Damjan Marion25665672016-08-16 18:56:08 +0200592 case VNET_DPDK_PMD_AF_PACKET:
593 xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
594 xd->af_packet_port_id = af_packet_port_id++;
595 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
Damjan Marion25665672016-08-16 18:56:08 +0200597 case VNET_DPDK_PMD_BOND:
John Loa60d4cb2016-12-17 03:09:58 -0500598 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
Damjan Marion25665672016-08-16 18:56:08 +0200599 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
600 break;
John Lod9bf9ab2016-02-25 11:17:55 -0500601
Damjan Marion25665672016-08-16 18:56:08 +0200602 default:
603 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
604 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
Damjan Marion25665672016-08-16 18:56:08 +0200606 if (devconf->num_rx_desc)
607 xd->nb_rx_desc = devconf->num_rx_desc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
Damjan Marion25665672016-08-16 18:56:08 +0200609 if (devconf->num_tx_desc)
610 xd->nb_tx_desc = devconf->num_tx_desc;
611 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612
613 /*
614 * Ensure default mtu is not > the mtu read from the hardware.
615 * Otherwise rte_eth_dev_configure() will fail and the port will
616 * not be available.
617 */
Jurek Matuszewskia69318b2016-03-28 15:15:15 -0400618 if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
Damjan Marion25665672016-08-16 18:56:08 +0200619 {
620 /*
621 * This device does not support the platforms's max frame
622 * size. Use it's advertised mru instead.
623 */
624 xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
625 }
Jurek Matuszewskia69318b2016-03-28 15:15:15 -0400626 else
Damjan Marion25665672016-08-16 18:56:08 +0200627 {
628 xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
Jurek Matuszewskia69318b2016-03-28 15:15:15 -0400629
Damjan Marion25665672016-08-16 18:56:08 +0200630 /*
631 * Some platforms do not account for Ethernet FCS (4 bytes) in
632 * MTU calculations. To interop with them increase mru but only
633 * if the device's settings can support it.
634 */
635 if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
636 xd->port_conf.rxmode.hw_strip_crc)
637 {
638 /*
639 * Allow additional 4 bytes (for Ethernet FCS). These bytes are
640 * stripped by h/w and so will not consume any buffer memory.
641 */
642 xd->port_conf.rxmode.max_rx_pkt_len += 4;
643 }
644 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646 if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
Damjan Marion25665672016-08-16 18:56:08 +0200647 {
648 f64 now = vlib_time_now (vm);
649 u32 rnd;
650 rnd = (u32) (now * 1e6);
651 rnd = random_u32 (&rnd);
652 clib_memcpy (addr + 2, &rnd, sizeof (rnd));
653 addr[0] = 2;
654 addr[1] = 0xfe;
655 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656 else
Damjan Marion25665672016-08-16 18:56:08 +0200657 rte_eth_macaddr_get (i, (struct ether_addr *) addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
659 if (xd->tx_q_used < tm->n_vlib_mains)
Damjan Marion25665672016-08-16 18:56:08 +0200660 dpdk_device_lock_init (xd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661
662 xd->device_index = xd - dm->devices;
Damjan Marion25665672016-08-16 18:56:08 +0200663 ASSERT (i == xd->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 xd->per_interface_next_index = ~0;
665
666 /* assign interface to input thread */
Damjan Marion25665672016-08-16 18:56:08 +0200667 dpdk_device_and_queue_t *dq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 int q;
669
Damjan Marion0b140722016-06-14 00:36:09 +0200670 if (devconf->workers)
671 {
672 int i;
673 q = 0;
Damjan Marion25665672016-08-16 18:56:08 +0200674 /* *INDENT-OFF* */
Damjan Marion0b140722016-06-14 00:36:09 +0200675 clib_bitmap_foreach (i, devconf->workers, ({
676 int cpu = dm->input_cpu_first_index + i;
Pavel Kotucek98765202016-10-07 08:37:28 +0200677 unsigned lcore = vlib_worker_threads[cpu].lcore_id;
Damjan Marion0b140722016-06-14 00:36:09 +0200678 vec_validate(xd->cpu_socket_id_by_queue, q);
679 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
680 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
681 dq->device = xd->device_index;
682 dq->queue_id = q++;
683 }));
Damjan Marion25665672016-08-16 18:56:08 +0200684 /* *INDENT-ON* */
Damjan Marion0b140722016-06-14 00:36:09 +0200685 }
686 else
687 for (q = 0; q < xd->rx_q_used; q++)
688 {
689 int cpu = dm->input_cpu_first_index + next_cpu;
Pavel Kotucek98765202016-10-07 08:37:28 +0200690 unsigned lcore = vlib_worker_threads[cpu].lcore_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
Damjan Marion0b140722016-06-14 00:36:09 +0200692 /*
693 * numa node for worker thread handling this queue
694 * needed for taking buffers from the right mempool
695 */
Damjan Marion25665672016-08-16 18:56:08 +0200696 vec_validate (xd->cpu_socket_id_by_queue, q);
697 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id (lcore);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
Damjan Marion0b140722016-06-14 00:36:09 +0200699 /*
700 * construct vector of (device,queue) pairs for each worker thread
701 */
Damjan Marion25665672016-08-16 18:56:08 +0200702 vec_add2 (dm->devices_by_cpu[cpu], dq, 1);
Damjan Marion0b140722016-06-14 00:36:09 +0200703 dq->device = xd->device_index;
704 dq->queue_id = q;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Damjan Marion0b140722016-06-14 00:36:09 +0200706 next_cpu++;
707 if (next_cpu == dm->input_cpu_count)
708 next_cpu = 0;
709 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100711
712 if (devconf->hqos_enabled)
713 {
714 xd->flags |= DPDK_DEVICE_FLAG_HQOS;
715
716 if (devconf->hqos.hqos_thread_valid)
717 {
718 int cpu = dm->hqos_cpu_first_index + devconf->hqos.hqos_thread;
719
720 if (devconf->hqos.hqos_thread >= dm->hqos_cpu_count)
721 return clib_error_return (0, "invalid HQoS thread index");
722
723 vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
724 dq->device = xd->device_index;
725 dq->queue_id = 0;
726 }
727 else
728 {
729 int cpu = dm->hqos_cpu_first_index + next_hqos_cpu;
730
731 if (dm->hqos_cpu_count == 0)
732 return clib_error_return (0, "no HQoS threads available");
733
734 vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
735 dq->device = xd->device_index;
736 dq->queue_id = 0;
737
738 next_hqos_cpu++;
739 if (next_hqos_cpu == dm->hqos_cpu_count)
740 next_hqos_cpu = 0;
741
742 devconf->hqos.hqos_thread_valid = 1;
743 devconf->hqos.hqos_thread = cpu;
744 }
745 }
746
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
Damjan Marion25665672016-08-16 18:56:08 +0200748 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 for (j = 0; j < tm->n_vlib_mains; j++)
Damjan Marion25665672016-08-16 18:56:08 +0200750 {
Damjan Marionb58598b2016-09-19 13:44:37 +0200751 vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
Damjan Marion25665672016-08-16 18:56:08 +0200752 sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
753 vec_reset_length (xd->tx_vectors[j]);
754 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755
756 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
Damjan Marion25665672016-08-16 18:56:08 +0200757 CLIB_CACHE_LINE_BYTES);
758 for (j = 0; j < xd->rx_q_used; j++)
759 {
760 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
761 CLIB_CACHE_LINE_BYTES);
762 vec_reset_length (xd->rx_vectors[j]);
763 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
Damjan Marion4015b192016-11-18 11:41:37 +0100765 vec_validate_aligned (xd->d_trace_buffers, tm->n_vlib_mains,
766 CLIB_CACHE_LINE_BYTES);
767
Damjan Marion25665672016-08-16 18:56:08 +0200768 rv = dpdk_port_setup (dm, xd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
Dave Barachdd522cb2016-08-10 16:56:16 -0400770 if (rv)
Damjan Marion25665672016-08-16 18:56:08 +0200771 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100773 if (devconf->hqos_enabled)
774 {
775 rv = dpdk_port_setup_hqos (xd, &devconf->hqos);
Dave Barach8d2e0e02016-11-09 11:28:36 -0500776 if (rv)
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100777 return rv;
778 }
779
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780 /* count the number of descriptors used for this device */
781 nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
782
783 error = ethernet_register_interface
Damjan Marion25665672016-08-16 18:56:08 +0200784 (dm->vnet_main, dpdk_device_class.index, xd->device_index,
785 /* ethernet address */ addr,
786 &xd->vlib_hw_if_index, dpdk_flag_change);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 if (error)
Damjan Marion25665672016-08-16 18:56:08 +0200788 return error;
789
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
791 xd->vlib_sw_if_index = sw->sw_if_index;
792 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
793
794 /*
795 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
Damjan Marion25665672016-08-16 18:56:08 +0200796 * driver to dynamically change the mtu. If/when the
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 * VIC firmware gets fixed, then this should be removed.
798 */
Damjan Marion3a944752016-07-06 23:13:39 +0200799 if (xd->pmd == VNET_DPDK_PMD_ENIC)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 {
801 /*
802 * Initialize mtu to what has been set by CIMC in the firmware cfg.
803 */
804 hi->max_packet_bytes = dev_info.max_rx_pktlen;
John Loce3e9712016-07-07 13:54:44 -0400805 if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
Damjan Marion25665672016-08-16 18:56:08 +0200806 vlan_strip = 1; /* remove vlan tag from VIC port by default */
John Loce3e9712016-07-07 13:54:44 -0400807 else
Damjan Marion25665672016-08-16 18:56:08 +0200808 clib_warning ("VLAN strip disabled for interface\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809 }
John Loce3e9712016-07-07 13:54:44 -0400810 else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
811 vlan_strip = 1;
812
813 if (vlan_strip)
Damjan Marion25665672016-08-16 18:56:08 +0200814 {
John Loce3e9712016-07-07 13:54:44 -0400815 int vlan_off;
Damjan Marion25665672016-08-16 18:56:08 +0200816 vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
John Loce3e9712016-07-07 13:54:44 -0400817 vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
jerryian5165cbc2016-11-18 11:20:19 +0800818 xd->port_conf.rxmode.hw_vlan_strip = vlan_off;
Damjan Marion25665672016-08-16 18:56:08 +0200819 if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
820 clib_warning ("VLAN strip enabled for interface\n");
John Loce3e9712016-07-07 13:54:44 -0400821 else
Damjan Marion25665672016-08-16 18:56:08 +0200822 clib_warning ("VLAN strip cannot be supported by interface\n");
823 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824
Damjan Marion25665672016-08-16 18:56:08 +0200825 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
826 xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827
Damjan Marion25665672016-08-16 18:56:08 +0200828 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829 }
830
Damjan Marion25665672016-08-16 18:56:08 +0200831 if (nb_desc > dm->conf->num_mbufs)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832 clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
Damjan Marion25665672016-08-16 18:56:08 +0200833 dm->conf->num_mbufs, nb_desc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835 return 0;
836}
837
Damjan Mariona42cd342016-04-13 18:03:20 +0200838static void
Damjan Marion64ae6692016-05-25 18:40:13 +0200839dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700840{
Damjan Marion25665672016-08-16 18:56:08 +0200841 vlib_pci_main_t *pm = &pci_main;
842 clib_error_t *error;
843 vlib_pci_device_t *d;
Damjan Marion25665672016-08-16 18:56:08 +0200844 u8 *pci_addr = 0;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200845 int num_whitelisted = vec_len (conf->dev_confs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846
Damjan Marion25665672016-08-16 18:56:08 +0200847 /* *INDENT-OFF* */
Damjan Mariona42cd342016-04-13 18:03:20 +0200848 pool_foreach (d, pm->pci_devs, ({
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200849 dpdk_device_config_t * devconf = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200850 vec_reset_length (pci_addr);
851 pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852
Damjan Marionb1b1a142016-08-23 00:53:22 +0200853 if (d->device_class != PCI_CLASS_NETWORK_ETHERNET)
Damjan Mariona42cd342016-04-13 18:03:20 +0200854 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200856 if (num_whitelisted)
857 {
858 uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
859
860 if (!p)
861 continue;
862
863 devconf = pool_elt_at_index (conf->dev_confs, p[0]);
864 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865
Damjan Mariona42cd342016-04-13 18:03:20 +0200866 /* virtio */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200867 if (d->vendor_id == 0x1af4 && d->device_id == 0x1000)
Damjan Mariona42cd342016-04-13 18:03:20 +0200868 ;
869 /* vmxnet3 */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200870 else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
Damjan Mariona42cd342016-04-13 18:03:20 +0200871 ;
872 /* all Intel devices */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200873 else if (d->vendor_id == 0x8086)
Damjan Mariona42cd342016-04-13 18:03:20 +0200874 ;
875 /* Cisco VIC */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200876 else if (d->vendor_id == 0x1137 && d->device_id == 0x0043)
Damjan Mariona42cd342016-04-13 18:03:20 +0200877 ;
878 /* Chelsio T4/T5 */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200879 else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
Damjan Mariona42cd342016-04-13 18:03:20 +0200880 ;
881 else
882 {
883 clib_warning ("Unsupported Ethernet PCI device 0x%04x:0x%04x found "
Damjan Marionb1b1a142016-08-23 00:53:22 +0200884 "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
Damjan Mariona42cd342016-04-13 18:03:20 +0200885 pci_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886 continue;
Damjan Mariona42cd342016-04-13 18:03:20 +0200887 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
Damjan Marion64ae6692016-05-25 18:40:13 +0200889 error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890
Damjan Mariona42cd342016-04-13 18:03:20 +0200891 if (error)
892 {
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200893 if (devconf == 0)
894 {
895 pool_get (conf->dev_confs, devconf);
896 hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
897 devconf - conf->dev_confs);
898 devconf->pci_addr.as_u32 = d->bus_address.as_u32;
899 }
900 devconf->is_blacklisted = 1;
Damjan Mariona42cd342016-04-13 18:03:20 +0200901 clib_error_report (error);
902 }
903 }));
Damjan Marion25665672016-08-16 18:56:08 +0200904 /* *INDENT-ON* */
Damjan Mariona42cd342016-04-13 18:03:20 +0200905 vec_free (pci_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906}
907
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908static clib_error_t *
Damjan Marion25665672016-08-16 18:56:08 +0200909dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
910 unformat_input_t * input, u8 is_default)
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200911{
Damjan Marion25665672016-08-16 18:56:08 +0200912 clib_error_t *error = 0;
913 uword *p;
914 dpdk_device_config_t *devconf;
Srivatsa Sangli820e3632016-06-14 13:10:55 -0700915 unformat_input_t sub_input;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200916
917 if (is_default)
918 {
919 devconf = &conf->default_devconf;
920 }
921 else
922 {
923 p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
924
925 if (!p)
926 {
927 pool_get (conf->dev_confs, devconf);
Damjan Marion25665672016-08-16 18:56:08 +0200928 hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
929 devconf - conf->dev_confs);
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200930 }
931 else
Damjan Marion25665672016-08-16 18:56:08 +0200932 return clib_error_return (0,
933 "duplicate configuration for PCI address %U",
934 format_vlib_pci_addr, &pci_addr);
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200935 }
936
937 devconf->pci_addr.as_u32 = pci_addr.as_u32;
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100938 devconf->hqos_enabled = 0;
939 dpdk_device_config_hqos_default (&devconf->hqos);
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200940
941 if (!input)
942 return 0;
943
Pierre Pfister446e1f52016-11-02 10:06:49 +0000944 unformat_skip_white_space (input);
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200945 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
946 {
947 if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
948 ;
Damjan Marion39da6212016-06-06 13:21:04 +0200949 else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
950 ;
951 else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
952 ;
953 else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200954 ;
Damjan Marion0b140722016-06-14 00:36:09 +0200955 else if (unformat (input, "workers %U", unformat_bitmap_list,
956 &devconf->workers))
957 ;
Damjan Marion25665672016-08-16 18:56:08 +0200958 else
959 if (unformat
960 (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
961 {
962 error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
963 if (error)
964 break;
965 }
John Loce3e9712016-07-07 13:54:44 -0400966 else if (unformat (input, "vlan-strip-offload off"))
Damjan Marion25665672016-08-16 18:56:08 +0200967 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
John Loce3e9712016-07-07 13:54:44 -0400968 else if (unformat (input, "vlan-strip-offload on"))
Damjan Marion25665672016-08-16 18:56:08 +0200969 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200970 else
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100971 if (unformat
972 (input, "hqos %U", unformat_vlib_cli_sub_input, &sub_input))
973 {
974 devconf->hqos_enabled = 1;
975 error = unformat_hqos (&sub_input, &devconf->hqos);
976 if (error)
977 break;
978 }
979 else if (unformat (input, "hqos"))
980 {
981 devconf->hqos_enabled = 1;
982 }
983 else
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200984 {
985 error = clib_error_return (0, "unknown input `%U'",
986 format_unformat_error, input);
987 break;
988 }
989 }
Damjan Marion0b140722016-06-14 00:36:09 +0200990
991 if (error)
992 return error;
993
994 if (devconf->workers && devconf->num_rx_queues == 0)
Damjan Marion25665672016-08-16 18:56:08 +0200995 devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
Damjan Marion0b140722016-06-14 00:36:09 +0200996 else if (devconf->workers &&
Damjan Marion25665672016-08-16 18:56:08 +0200997 clib_bitmap_count_set_bits (devconf->workers) !=
998 devconf->num_rx_queues)
999 error =
1000 clib_error_return (0,
1001 "%U: number of worker threadds must be "
1002 "equal to number of rx queues", format_vlib_pci_addr,
1003 &pci_addr);
Damjan Marion0b140722016-06-14 00:36:09 +02001004
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001005 return error;
1006}
1007
1008static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009dpdk_config (vlib_main_t * vm, unformat_input_t * input)
1010{
Damjan Marion25665672016-08-16 18:56:08 +02001011 clib_error_t *error = 0;
1012 dpdk_main_t *dm = &dpdk_main;
1013 dpdk_config_main_t *conf = &dpdk_config_main;
1014 vlib_thread_main_t *tm = vlib_get_thread_main ();
1015 dpdk_device_config_t *devconf;
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001016 vlib_pci_addr_t pci_addr;
1017 unformat_input_t sub_input;
Damjan Marion25665672016-08-16 18:56:08 +02001018 u8 *s, *tmp = 0;
1019 u8 *rte_cmd = 0, *ethname = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020 u32 log_level;
1021 int ret, i;
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001022 int num_whitelisted = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023 u8 no_pci = 0;
1024 u8 no_huge = 0;
1025 u8 huge_dir = 0;
1026 u8 file_prefix = 0;
Damjan Marion25665672016-08-16 18:56:08 +02001027 u8 *socket_mem = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001029 conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
1030
Damjan Marion25665672016-08-16 18:56:08 +02001031 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032 {
1033 /* Prime the pump */
1034 if (unformat (input, "no-hugetlb"))
Damjan Marion25665672016-08-16 18:56:08 +02001035 {
1036 vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
1037 no_huge = 1;
1038 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039
Dave Barachd81566f2016-02-15 11:34:13 -05001040 else if (unformat (input, "enable-tcp-udp-checksum"))
Damjan Marion64ae6692016-05-25 18:40:13 +02001041 conf->enable_tcp_udp_checksum = 1;
Dave Barachd81566f2016-02-15 11:34:13 -05001042
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043 else if (unformat (input, "decimal-interface-names"))
Damjan Marion25665672016-08-16 18:56:08 +02001044 conf->interface_name_format_decimal = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045
1046 else if (unformat (input, "no-multi-seg"))
Damjan Marion25665672016-08-16 18:56:08 +02001047 conf->no_multi_seg = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001049 else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1050 &sub_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051 {
Damjan Marion25665672016-08-16 18:56:08 +02001052 error =
1053 dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
1054 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001056 if (error)
1057 return error;
1058 }
Damjan Marion25665672016-08-16 18:56:08 +02001059 else
1060 if (unformat
1061 (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1062 unformat_vlib_cli_sub_input, &sub_input))
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001063 {
1064 error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1065
1066 if (error)
1067 return error;
1068
1069 num_whitelisted++;
1070 }
1071 else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1072 {
1073 error = dpdk_device_config (conf, pci_addr, 0, 0);
1074
1075 if (error)
1076 return error;
1077
1078 num_whitelisted++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079 }
Damjan Marion64ae6692016-05-25 18:40:13 +02001080 else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
Damjan Marion25665672016-08-16 18:56:08 +02001081 ;
Damjan Marion64ae6692016-05-25 18:40:13 +02001082 else if (unformat (input, "kni %d", &conf->num_kni))
Damjan Marion25665672016-08-16 18:56:08 +02001083 ;
Damjan Marion64ae6692016-05-25 18:40:13 +02001084 else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085 ;
Damjan Marion7a2a3782016-04-15 20:24:55 +02001086 else if (unformat (input, "socket-mem %s", &socket_mem))
1087 ;
Damjan Marionb022cd12016-08-16 13:25:59 +02001088 else if (unformat (input, "no-pci"))
1089 {
1090 no_pci = 1;
1091 tmp = format (0, "--no-pci%c", 0);
1092 vec_add1 (conf->eal_init_args, tmp);
1093 }
Dave Barach08602d12016-06-04 14:10:59 -04001094 else if (unformat (input, "poll-sleep %d", &dm->poll_sleep))
Damjan Marion25665672016-08-16 18:56:08 +02001095 ;
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001096
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097#define _(a) \
1098 else if (unformat(input, #a)) \
1099 { \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100 tmp = format (0, "--%s%c", #a, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001101 vec_add1 (conf->eal_init_args, tmp); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 }
1103 foreach_eal_double_hyphen_predicate_arg
1104#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105#define _(a) \
1106 else if (unformat(input, #a " %s", &s)) \
1107 { \
1108 if (!strncmp(#a, "huge-dir", 8)) \
1109 huge_dir = 1; \
1110 else if (!strncmp(#a, "file-prefix", 11)) \
1111 file_prefix = 1; \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112 tmp = format (0, "--%s%c", #a, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001113 vec_add1 (conf->eal_init_args, tmp); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114 vec_add1 (s, 0); \
Billy McFall0d3c1cc2017-01-06 17:55:25 -05001115 if (!strncmp(#a, "vdev", 4)) \
1116 if (strstr((char*)s, "af_packet")) \
1117 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001118 vec_add1 (conf->eal_init_args, s); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 }
1120 foreach_eal_double_hyphen_arg
1121#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122#define _(a,b) \
1123 else if (unformat(input, #a " %s", &s)) \
1124 { \
1125 tmp = format (0, "-%s%c", #b, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001126 vec_add1 (conf->eal_init_args, tmp); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127 vec_add1 (s, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001128 vec_add1 (conf->eal_init_args, s); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129 }
Damjan Marion25665672016-08-16 18:56:08 +02001130 foreach_eal_single_hyphen_arg
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132#define _(a,b) \
1133 else if (unformat(input, #a " %s", &s)) \
1134 { \
1135 tmp = format (0, "-%s%c", #b, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001136 vec_add1 (conf->eal_init_args, tmp); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137 vec_add1 (s, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001138 vec_add1 (conf->eal_init_args, s); \
1139 conf->a##_set_manually = 1; \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140 }
Damjan Marion25665672016-08-16 18:56:08 +02001141 foreach_eal_single_hyphen_mandatory_arg
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142#undef _
Damjan Marion25665672016-08-16 18:56:08 +02001143 else if (unformat (input, "default"))
1144 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145
Damjan Marion4c20e712016-10-05 09:50:38 +02001146 else if (unformat_skip_white_space (input))
Damjan Marion61448022016-09-13 10:47:35 +02001147 ;
Damjan Marion25665672016-08-16 18:56:08 +02001148 else
1149 {
1150 error = clib_error_return (0, "unknown input `%U'",
1151 format_unformat_error, input);
1152 goto done;
1153 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154 }
1155
Damjan Marion64ae6692016-05-25 18:40:13 +02001156 if (!conf->uio_driver_name)
1157 conf->uio_driver_name = format (0, "igb_uio%c", 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158
1159 /*
1160 * Use 1G huge pages if available.
1161 */
1162 if (!no_huge && !huge_dir)
1163 {
Damjan Marion25665672016-08-16 18:56:08 +02001164 u32 x, *mem_by_socket = 0;
Damjan Marion7a2a3782016-04-15 20:24:55 +02001165 uword c = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166 u8 use_1g = 1;
1167 u8 use_2m = 1;
Damjan Marion7a2a3782016-04-15 20:24:55 +02001168 u8 less_than_1g = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169 int rv;
1170
Damjan Marion25665672016-08-16 18:56:08 +02001171 umount (DEFAULT_HUGE_DIR);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001172
1173 /* Process "socket-mem" parameter value */
1174 if (vec_len (socket_mem))
Damjan Marion7a2a3782016-04-15 20:24:55 +02001175 {
1176 unformat_input_t in;
Damjan Marion25665672016-08-16 18:56:08 +02001177 unformat_init_vector (&in, socket_mem);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001178 while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1179 {
1180 if (unformat (&in, "%u,", &x))
1181 ;
1182 else if (unformat (&in, "%u", &x))
1183 ;
1184 else if (unformat (&in, ","))
1185 x = 0;
1186 else
1187 break;
1188
Damjan Marion25665672016-08-16 18:56:08 +02001189 vec_add1 (mem_by_socket, x);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001190
1191 if (x > 1023)
1192 less_than_1g = 0;
1193 }
Damjan Marion25665672016-08-16 18:56:08 +02001194 /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1195 unformat_free (&in);
1196 socket_mem = 0;
Damjan Marion7a2a3782016-04-15 20:24:55 +02001197 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198 else
Damjan Marion7a2a3782016-04-15 20:24:55 +02001199 {
Damjan Marion25665672016-08-16 18:56:08 +02001200 /* *INDENT-OFF* */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001201 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1202 {
1203 vec_validate(mem_by_socket, c);
Damjan Mariona06dfb32016-06-26 20:16:57 +02001204 mem_by_socket[c] = 256; /* default per-socket mem */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001205 }
1206 ));
Damjan Marion25665672016-08-16 18:56:08 +02001207 /* *INDENT-ON* */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001208 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
1210 /* check if available enough 1GB pages for each socket */
Damjan Marion25665672016-08-16 18:56:08 +02001211 /* *INDENT-OFF* */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001212 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1213 {
Damjan Marion20f64412016-09-27 17:51:13 +02001214 int pages_avail, page_size, mem;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215
Damjan Marion7a2a3782016-04-15 20:24:55 +02001216 vec_validate(mem_by_socket, c);
1217 mem = mem_by_socket[c];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218
Damjan Marion7a2a3782016-04-15 20:24:55 +02001219 page_size = 1024;
Damjan Marion20f64412016-09-27 17:51:13 +02001220 pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001221
Damjan Marion20f64412016-09-27 17:51:13 +02001222 if (pages_avail < 0 || page_size * pages_avail < mem)
Damjan Marion7a2a3782016-04-15 20:24:55 +02001223 use_1g = 0;
1224
1225 page_size = 2;
Damjan Marion20f64412016-09-27 17:51:13 +02001226 pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001227
Damjan Marion20f64412016-09-27 17:51:13 +02001228 if (pages_avail < 0 || page_size * pages_avail < mem)
Damjan Marion7a2a3782016-04-15 20:24:55 +02001229 use_2m = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230 }));
Damjan Marion25665672016-08-16 18:56:08 +02001231 /* *INDENT-ON* */
Dave Barachdd522cb2016-08-10 16:56:16 -04001232
1233 if (mem_by_socket == 0)
Damjan Marion25665672016-08-16 18:56:08 +02001234 {
1235 error = clib_error_return (0, "mem_by_socket NULL");
1236 goto done;
1237 }
Damjan Marion7a2a3782016-04-15 20:24:55 +02001238 _vec_len (mem_by_socket) = c + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239
Damjan Marion7a2a3782016-04-15 20:24:55 +02001240 /* regenerate socket_mem string */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001241 vec_foreach_index (x, mem_by_socket)
1242 socket_mem = format (socket_mem, "%s%u",
Damjan Marion25665672016-08-16 18:56:08 +02001243 socket_mem ? "," : "", mem_by_socket[x]);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001244 socket_mem = format (socket_mem, "%c", 0);
1245
1246 vec_free (mem_by_socket);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001247
Damjan Marion25665672016-08-16 18:56:08 +02001248 rv = mkdir (VPP_RUN_DIR, 0755);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249 if (rv && errno != EEXIST)
Damjan Marion25665672016-08-16 18:56:08 +02001250 {
1251 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1252 VPP_RUN_DIR, errno);
1253 goto done;
1254 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255
Damjan Marion25665672016-08-16 18:56:08 +02001256 rv = mkdir (DEFAULT_HUGE_DIR, 0755);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257 if (rv && errno != EEXIST)
Damjan Marion25665672016-08-16 18:56:08 +02001258 {
1259 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1260 DEFAULT_HUGE_DIR, errno);
1261 goto done;
1262 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263
Damjan Marion7a2a3782016-04-15 20:24:55 +02001264 if (use_1g && !(less_than_1g && use_2m))
Damjan Marion25665672016-08-16 18:56:08 +02001265 {
1266 rv =
1267 mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1268 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269 else if (use_2m)
Damjan Marion25665672016-08-16 18:56:08 +02001270 {
1271 rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1272 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273 else
Damjan Marion25665672016-08-16 18:56:08 +02001274 {
1275 return clib_error_return (0, "not enough free huge pages");
1276 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001277
1278 if (rv)
Damjan Marion25665672016-08-16 18:56:08 +02001279 {
1280 error = clib_error_return (0, "mount failed %d", errno);
1281 goto done;
1282 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283
1284 tmp = format (0, "--huge-dir%c", 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001285 vec_add1 (conf->eal_init_args, tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286 tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001287 vec_add1 (conf->eal_init_args, tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001288 if (!file_prefix)
Damjan Marion25665672016-08-16 18:56:08 +02001289 {
1290 tmp = format (0, "--file-prefix%c", 0);
1291 vec_add1 (conf->eal_init_args, tmp);
1292 tmp = format (0, "vpp%c", 0);
1293 vec_add1 (conf->eal_init_args, tmp);
1294 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001295 }
1296
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297 vec_free (rte_cmd);
1298 vec_free (ethname);
1299
1300 if (error)
1301 return error;
1302
1303 /* I'll bet that -c and -n must be the first and second args... */
Damjan Marion64ae6692016-05-25 18:40:13 +02001304 if (!conf->coremask_set_manually)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305 {
Damjan Marion25665672016-08-16 18:56:08 +02001306 vlib_thread_registration_t *tr;
1307 uword *coremask = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001308 int i;
1309
1310 /* main thread core */
Damjan Marion25665672016-08-16 18:56:08 +02001311 coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312
1313 for (i = 0; i < vec_len (tm->registrations); i++)
Damjan Marion25665672016-08-16 18:56:08 +02001314 {
1315 tr = tm->registrations[i];
1316 coremask = clib_bitmap_or (coremask, tr->coremask);
1317 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001318
Damjan Marion64ae6692016-05-25 18:40:13 +02001319 vec_insert (conf->eal_init_args, 2, 1);
1320 conf->eal_init_args[1] = (u8 *) "-c";
Damjan Marion14a44d32016-02-05 23:33:21 +01001321 tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001322 conf->eal_init_args[2] = tmp;
Damjan Marion25665672016-08-16 18:56:08 +02001323 clib_bitmap_free (coremask);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001324 }
1325
Damjan Marion64ae6692016-05-25 18:40:13 +02001326 if (!conf->nchannels_set_manually)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327 {
Damjan Marion64ae6692016-05-25 18:40:13 +02001328 vec_insert (conf->eal_init_args, 2, 3);
1329 conf->eal_init_args[3] = (u8 *) "-n";
1330 tmp = format (0, "%d", conf->nchannels);
1331 conf->eal_init_args[4] = tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332 }
1333
Damjan Marion25665672016-08-16 18:56:08 +02001334 if (no_pci == 0 && geteuid () == 0)
1335 dpdk_bind_devices_to_uio (conf);
Damjan Mariona42cd342016-04-13 18:03:20 +02001336
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001337#define _(x) \
1338 if (devconf->x == 0 && conf->default_devconf.x > 0) \
1339 devconf->x = conf->default_devconf.x ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001340
Damjan Marion25665672016-08-16 18:56:08 +02001341 /* *INDENT-OFF* */
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001342 pool_foreach (devconf, conf->dev_confs, ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001344 /* default per-device config items */
1345 foreach_dpdk_device_config_item
1346
1347 /* add DPDK EAL whitelist/blacklist entry */
1348 if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1349 {
1350 tmp = format (0, "-w%c", 0);
1351 vec_add1 (conf->eal_init_args, tmp);
Damjan Marionc883fa72016-06-07 21:49:56 +02001352 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001353 vec_add1 (conf->eal_init_args, tmp);
1354 }
1355 else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1356 {
1357 tmp = format (0, "-b%c", 0);
1358 vec_add1 (conf->eal_init_args, tmp);
Damjan Marionc883fa72016-06-07 21:49:56 +02001359 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001360 vec_add1 (conf->eal_init_args, tmp);
1361 }
1362 }));
Damjan Marion25665672016-08-16 18:56:08 +02001363 /* *INDENT-ON* */
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001364
1365#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366
Ed Warnickecb9cada2015-12-08 15:45:58 -07001367 /* set master-lcore */
1368 tmp = format (0, "--master-lcore%c", 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001369 vec_add1 (conf->eal_init_args, tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001370 tmp = format (0, "%u%c", tm->main_lcore, 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001371 vec_add1 (conf->eal_init_args, tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001372
Damjan Marion7a2a3782016-04-15 20:24:55 +02001373 /* set socket-mem */
1374 tmp = format (0, "--socket-mem%c", 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001375 vec_add1 (conf->eal_init_args, tmp);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001376 tmp = format (0, "%s%c", socket_mem, 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001377 vec_add1 (conf->eal_init_args, tmp);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001378
Ed Warnickecb9cada2015-12-08 15:45:58 -07001379 /* NULL terminate the "argv" vector, in case of stupidity */
Damjan Marion64ae6692016-05-25 18:40:13 +02001380 vec_add1 (conf->eal_init_args, 0);
Damjan Marion25665672016-08-16 18:56:08 +02001381 _vec_len (conf->eal_init_args) -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001382
1383 /* Set up DPDK eal and packet mbuf pool early. */
1384
1385 log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1386
1387 rte_set_log_level (log_level);
1388
Damjan Marion64ae6692016-05-25 18:40:13 +02001389 vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390
Damjan Mariond8ff0e12016-02-16 12:29:57 +01001391 /* make copy of args as rte_eal_init tends to mess up with arg array */
Damjan Marion25665672016-08-16 18:56:08 +02001392 for (i = 1; i < vec_len (conf->eal_init_args); i++)
1393 conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1394 conf->eal_init_args[i]);
Damjan Mariond8ff0e12016-02-16 12:29:57 +01001395
Damjan Marion25665672016-08-16 18:56:08 +02001396 ret =
1397 rte_eal_init (vec_len (conf->eal_init_args),
1398 (char **) conf->eal_init_args);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001399
1400 /* lazy umount hugepages */
Damjan Marion25665672016-08-16 18:56:08 +02001401 umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001402
1403 if (ret < 0)
1404 return clib_error_return (0, "rte_eal_init returned %d", ret);
1405
Sean Hope98efd022016-02-22 15:21:31 -05001406 /* Dump the physical memory layout prior to creating the mbuf_pool */
Damjan Marion25665672016-08-16 18:56:08 +02001407 fprintf (stdout, "DPDK physical memory layout:\n");
1408 rte_dump_physmem_layout (stdout);
Sean Hope98efd022016-02-22 15:21:31 -05001409
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410 /* main thread 1st */
Damjan Marion25665672016-08-16 18:56:08 +02001411 error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001412 if (error)
1413 return error;
1414
1415 for (i = 0; i < RTE_MAX_LCORE; i++)
1416 {
Damjan Marion25665672016-08-16 18:56:08 +02001417 error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1418 rte_lcore_to_socket_id (i));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001419 if (error)
Damjan Marion25665672016-08-16 18:56:08 +02001420 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001421 }
1422
Damjan Marion25665672016-08-16 18:56:08 +02001423done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424 return error;
1425}
1426
1427VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1428
Damjan Marion25665672016-08-16 18:56:08 +02001429void
1430dpdk_update_link_state (dpdk_device_t * xd, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001431{
Damjan Marion25665672016-08-16 18:56:08 +02001432 vnet_main_t *vnm = vnet_get_main ();
1433 struct rte_eth_link prev_link = xd->link;
1434 u32 hw_flags = 0;
1435 u8 hw_flags_chg = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436
Damjan Marion25665672016-08-16 18:56:08 +02001437 /* only update link state for PMD interfaces */
Damjan Marion56431702016-09-19 13:18:09 +02001438 if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
Damjan Marion25665672016-08-16 18:56:08 +02001439 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001440
Damjan Marion25665672016-08-16 18:56:08 +02001441 xd->time_last_link_update = now ? now : xd->time_last_link_update;
1442 memset (&xd->link, 0, sizeof (xd->link));
1443 rte_eth_link_get_nowait (xd->device_index, &xd->link);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001444
Damjan Marion25665672016-08-16 18:56:08 +02001445 if (LINK_STATE_ELOGS)
1446 {
1447 vlib_main_t *vm = vlib_get_main ();
1448 ELOG_TYPE_DECLARE (e) =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001449 {
Damjan Marion25665672016-08-16 18:56:08 +02001450 .format =
1451 "update-link-state: sw_if_index %d, admin_up %d,"
1452 "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
Ed Warnickecb9cada2015-12-08 15:45:58 -07001453
Damjan Marion25665672016-08-16 18:56:08 +02001454 struct
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455 {
Damjan Marion25665672016-08-16 18:56:08 +02001456 u32 sw_if_index;
1457 u8 admin_up;
1458 u8 old_link_state;
1459 u8 new_link_state;
1460 } *ed;
1461 ed = ELOG_DATA (&vm->elog_main, e);
1462 ed->sw_if_index = xd->vlib_sw_if_index;
Damjan Marionb28e4982016-08-22 22:34:38 +02001463 ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
Damjan Marion25665672016-08-16 18:56:08 +02001464 ed->old_link_state = (u8)
1465 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1466 ed->new_link_state = (u8) xd->link.link_status;
1467 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468
Damjan Marionb28e4982016-08-22 22:34:38 +02001469 if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
Damjan Marion25665672016-08-16 18:56:08 +02001470 ((xd->link.link_status != 0) ^
1471 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1472 {
1473 hw_flags_chg = 1;
1474 hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1475 }
1476
1477 if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1478 {
1479 hw_flags_chg = 1;
1480 switch (xd->link.link_duplex)
1481 {
1482 case ETH_LINK_HALF_DUPLEX:
1483 hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1484 break;
1485 case ETH_LINK_FULL_DUPLEX:
1486 hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1487 break;
1488 default:
1489 break;
1490 }
1491 }
Damjan Marion25665672016-08-16 18:56:08 +02001492 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1493 {
1494 hw_flags_chg = 1;
1495 switch (xd->link.link_speed)
1496 {
1497 case ETH_SPEED_NUM_10M:
1498 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1499 break;
1500 case ETH_SPEED_NUM_100M:
1501 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1502 break;
1503 case ETH_SPEED_NUM_1G:
1504 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1505 break;
1506 case ETH_SPEED_NUM_10G:
1507 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1508 break;
1509 case ETH_SPEED_NUM_40G:
1510 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1511 break;
1512 case 0:
1513 break;
1514 default:
1515 clib_warning ("unknown link speed %d", xd->link.link_speed);
1516 break;
1517 }
1518 }
Damjan Marion25665672016-08-16 18:56:08 +02001519 if (hw_flags_chg)
1520 {
1521 if (LINK_STATE_ELOGS)
1522 {
1523 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001524
Damjan Marion25665672016-08-16 18:56:08 +02001525 ELOG_TYPE_DECLARE (e) =
1526 {
1527 .format =
1528 "update-link-state: sw_if_index %d, new flags %d",.format_args
1529 = "i4i4",};
Ed Warnickecb9cada2015-12-08 15:45:58 -07001530
Damjan Marion25665672016-08-16 18:56:08 +02001531 struct
1532 {
1533 u32 sw_if_index;
1534 u32 flags;
1535 } *ed;
1536 ed = ELOG_DATA (&vm->elog_main, e);
1537 ed->sw_if_index = xd->vlib_sw_if_index;
1538 ed->flags = hw_flags;
1539 }
1540 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1541 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001542}
1543
1544static uword
Damjan Marion25665672016-08-16 18:56:08 +02001545dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546{
Damjan Marion25665672016-08-16 18:56:08 +02001547 clib_error_t *error;
1548 vnet_main_t *vnm = vnet_get_main ();
1549 dpdk_main_t *dm = &dpdk_main;
1550 ethernet_main_t *em = &ethernet_main;
1551 dpdk_device_t *xd;
1552 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001553 int i;
1554
1555 error = dpdk_lib_init (dm);
1556
Damjan Marion25665672016-08-16 18:56:08 +02001557 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558 * Turn on the input node if we found some devices to drive
1559 * and we're not running worker threads or i/o threads
1560 */
1561
Damjan Marion25665672016-08-16 18:56:08 +02001562 if (error == 0 && vec_len (dm->devices) > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563 {
Damjan Marion25665672016-08-16 18:56:08 +02001564 if (tm->n_vlib_mains == 1)
1565 vlib_node_set_state (vm, dpdk_input_node.index,
1566 VLIB_NODE_STATE_POLLING);
1567 else
1568 for (i = 0; i < tm->n_vlib_mains; i++)
1569 if (vec_len (dm->devices_by_cpu[i]) > 0)
1570 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1571 VLIB_NODE_STATE_POLLING);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001572 }
1573
1574 if (error)
1575 clib_error_report (error);
1576
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001577 tm->worker_thread_release = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001578
1579 f64 now = vlib_time_now (vm);
1580 vec_foreach (xd, dm->devices)
Damjan Marion25665672016-08-16 18:56:08 +02001581 {
1582 dpdk_update_link_state (xd, now);
1583 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001584
Damjan Marion25665672016-08-16 18:56:08 +02001585 {
1586 /*
1587 * Extra set up for bond interfaces:
1588 * 1. Setup MACs for bond interfaces and their slave links which was set
1589 * in dpdk_port_setup() but needs to be done again here to take effect.
1590 * 2. Set up info for bond interface related CLI support.
1591 */
1592 int nports = rte_eth_dev_count ();
1593 if (nports > 0)
1594 {
1595 for (i = 0; i < nports; i++)
1596 {
1597 struct rte_eth_dev_info dev_info;
1598 rte_eth_dev_info_get (i, &dev_info);
1599 if (!dev_info.driver_name)
Damjan Marion39162272016-10-25 14:54:38 +02001600#if RTE_VERSION < RTE_VERSION_NUM(16, 11, 0, 0)
John Lod9bf9ab2016-02-25 11:17:55 -05001601 dev_info.driver_name = dev_info.pci_dev->driver->name;
Damjan Marion39162272016-10-25 14:54:38 +02001602#else
1603 dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
1604#endif
Damjan Marion25665672016-08-16 18:56:08 +02001605 ASSERT (dev_info.driver_name);
1606 if (strncmp (dev_info.driver_name, "rte_bond_pmd", 12) == 0)
1607 {
1608 u8 addr[6];
1609 u8 slink[16];
1610 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1611 if (nlink > 0)
1612 {
1613 vnet_hw_interface_t *bhi;
1614 ethernet_interface_t *bei;
1615 int rv;
Damjan Marionb022cd12016-08-16 13:25:59 +02001616
Damjan Marion25665672016-08-16 18:56:08 +02001617 /* Get MAC of 1st slave link */
1618 rte_eth_macaddr_get (slink[0],
1619 (struct ether_addr *) addr);
1620 /* Set MAC of bounded interface to that of 1st slave link */
1621 rv =
1622 rte_eth_bond_mac_address_set (i,
1623 (struct ether_addr *)
1624 addr);
1625 if (rv < 0)
1626 clib_warning ("Failed to set MAC address");
Damjan Marionb022cd12016-08-16 13:25:59 +02001627
Damjan Marion25665672016-08-16 18:56:08 +02001628 /* Populate MAC of bonded interface in VPP hw tables */
1629 bhi =
1630 vnet_get_hw_interface (vnm,
1631 dm->devices[i].vlib_hw_if_index);
1632 bei =
1633 pool_elt_at_index (em->interfaces, bhi->hw_instance);
1634 clib_memcpy (bhi->hw_address, addr, 6);
1635 clib_memcpy (bei->address, addr, 6);
1636 /* Init l3 packet size allowed on bonded interface */
Steve Shinbf1fa7f2016-09-07 08:25:04 -07001637 bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
Damjan Marion25665672016-08-16 18:56:08 +02001638 bhi->max_l3_packet_bytes[VLIB_RX] =
1639 bhi->max_l3_packet_bytes[VLIB_TX] =
1640 ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1641 while (nlink >= 1)
1642 { /* for all slave links */
1643 int slave = slink[--nlink];
1644 dpdk_device_t *sdev = &dm->devices[slave];
1645 vnet_hw_interface_t *shi;
1646 vnet_sw_interface_t *ssi;
1647 /* Add MAC to all slave links except the first one */
1648 if (nlink)
1649 rte_eth_dev_mac_addr_add (slave,
1650 (struct ether_addr *)
1651 addr, 0);
1652 /* Set slaves bitmap for bonded interface */
1653 bhi->bond_info =
1654 clib_bitmap_set (bhi->bond_info,
1655 sdev->vlib_hw_if_index, 1);
1656 /* Set slave link flags on slave interface */
1657 shi =
1658 vnet_get_hw_interface (vnm, sdev->vlib_hw_if_index);
1659 ssi =
1660 vnet_get_sw_interface (vnm, sdev->vlib_sw_if_index);
1661 shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1662 ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
Steve Shinbf1fa7f2016-09-07 08:25:04 -07001663
1664 /* Set l3 packet size allowed as the lowest of slave */
1665 if (bhi->max_l3_packet_bytes[VLIB_RX] >
1666 shi->max_l3_packet_bytes[VLIB_RX])
1667 bhi->max_l3_packet_bytes[VLIB_RX] =
1668 bhi->max_l3_packet_bytes[VLIB_TX] =
1669 shi->max_l3_packet_bytes[VLIB_RX];
1670
1671 /* Set max packet size allowed as the lowest of slave */
1672 if (bhi->max_packet_bytes > shi->max_packet_bytes)
1673 bhi->max_packet_bytes = shi->max_packet_bytes;
Damjan Marion25665672016-08-16 18:56:08 +02001674 }
John Lod9bf9ab2016-02-25 11:17:55 -05001675 }
1676 }
1677 }
1678 }
1679 }
John Lod9bf9ab2016-02-25 11:17:55 -05001680
Ed Warnickecb9cada2015-12-08 15:45:58 -07001681 while (1)
1682 {
Bud Grise02301ef2016-02-24 16:09:05 -05001683 /*
1684 * check each time through the loop in case intervals are changed
1685 */
1686 f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
Damjan Marion25665672016-08-16 18:56:08 +02001687 dm->link_state_poll_interval : dm->stat_poll_interval;
Bud Grise02301ef2016-02-24 16:09:05 -05001688
1689 vlib_process_wait_for_event_or_clock (vm, min_wait);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001690
Damjan Marion4ee3a652016-11-15 19:47:58 +01001691 if (dm->admin_up_down_in_progress)
Damjan Marion25665672016-08-16 18:56:08 +02001692 /* skip the poll if an admin up down is in progress (on any interface) */
1693 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001694
1695 vec_foreach (xd, dm->devices)
Damjan Marion25665672016-08-16 18:56:08 +02001696 {
1697 f64 now = vlib_time_now (vm);
1698 if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1699 dpdk_update_counters (xd, now);
1700 if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1701 dpdk_update_link_state (xd, now);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001702
Damjan Marion25665672016-08-16 18:56:08 +02001703 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001704 }
1705
Damjan Marion25665672016-08-16 18:56:08 +02001706 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001707}
1708
Damjan Marion25665672016-08-16 18:56:08 +02001709/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001710VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1711 .function = dpdk_process,
1712 .type = VLIB_NODE_TYPE_PROCESS,
1713 .name = "dpdk-process",
1714 .process_log2_n_stack_bytes = 17,
1715};
Damjan Marion25665672016-08-16 18:56:08 +02001716/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001717
Damjan Marion25665672016-08-16 18:56:08 +02001718int
1719dpdk_set_stat_poll_interval (f64 interval)
Bud Grise02301ef2016-02-24 16:09:05 -05001720{
1721 if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
Damjan Marion25665672016-08-16 18:56:08 +02001722 return (VNET_API_ERROR_INVALID_VALUE);
Bud Grise02301ef2016-02-24 16:09:05 -05001723
1724 dpdk_main.stat_poll_interval = interval;
1725
1726 return 0;
1727}
1728
Damjan Marion25665672016-08-16 18:56:08 +02001729int
1730dpdk_set_link_state_poll_interval (f64 interval)
Bud Grise02301ef2016-02-24 16:09:05 -05001731{
1732 if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
Damjan Marion25665672016-08-16 18:56:08 +02001733 return (VNET_API_ERROR_INVALID_VALUE);
Bud Grise02301ef2016-02-24 16:09:05 -05001734
1735 dpdk_main.link_state_poll_interval = interval;
1736
1737 return 0;
1738}
1739
Ed Warnickecb9cada2015-12-08 15:45:58 -07001740clib_error_t *
1741dpdk_init (vlib_main_t * vm)
1742{
Damjan Marion25665672016-08-16 18:56:08 +02001743 dpdk_main_t *dm = &dpdk_main;
1744 vlib_node_t *ei;
1745 clib_error_t *error = 0;
1746 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001747
1748 /* verify that structs are cacheline aligned */
Damjan Marion3b906b02016-11-01 15:16:30 +01001749 STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1750 "Cache line marker must be 1st element in dpdk_device_t");
1751 STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1752 CLIB_CACHE_LINE_BYTES,
1753 "Data in cache line 0 is bigger than cache line size");
1754 STATIC_ASSERT (offsetof (dpdk_worker_t, cacheline0) == 0,
1755 "Cache line marker must be 1st element in dpdk_worker_t");
1756 STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1757 "Cache line marker must be 1st element in frame_queue_trace_t");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001758
Ed Warnickecb9cada2015-12-08 15:45:58 -07001759 dm->vlib_main = vm;
Damjan Marion25665672016-08-16 18:56:08 +02001760 dm->vnet_main = vnet_get_main ();
Damjan Marion64ae6692016-05-25 18:40:13 +02001761 dm->conf = &dpdk_config_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001762
1763 ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1764 if (ei == 0)
Damjan Marion25665672016-08-16 18:56:08 +02001765 return clib_error_return (0, "ethernet-input node AWOL");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001766
1767 dm->ethernet_input_node_index = ei->index;
1768
Damjan Marion64ae6692016-05-25 18:40:13 +02001769 dm->conf->nchannels = 4;
1770 dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1771 vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001772
1773 dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1774 dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1775 dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1776
1777 /* $$$ use n_thread_stacks since it's known-good at this point */
1778 vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1779
Dave Barachd81566f2016-02-15 11:34:13 -05001780 /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
Damjan Marion25665672016-08-16 18:56:08 +02001781 dm->buffer_flags_template =
Damjan Marion22766b82016-11-15 12:50:28 +01001782 (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_RTE_MBUF_VALID
Damjan Marion25665672016-08-16 18:56:08 +02001783 | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
Dave Barachd81566f2016-02-15 11:34:13 -05001784
Bud Grise02301ef2016-02-24 16:09:05 -05001785 dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1786 dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1787
Ed Warnickecb9cada2015-12-08 15:45:58 -07001788 /* init CLI */
1789 if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1790 return error;
1791
1792 return error;
1793}
1794
1795VLIB_INIT_FUNCTION (dpdk_init);
1796
Damjan Marion25665672016-08-16 18:56:08 +02001797
1798/*
1799 * fd.io coding-style-patch-verification: ON
1800 *
1801 * Local Variables:
1802 * eval: (c-set-style "gnu")
1803 * End:
1804 */