blob: 693ca98513004efd003b7bb3bd931960fede2671 [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{
Damjan Marion25665672016-08-16 18:56:08 +020067 vlib_main_t *vm = vlib_get_main ();
68 vlib_buffer_main_t *bm = vm->buffer_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 int rv;
70 int j;
71
Damjan Marion25665672016-08-16 18:56:08 +020072 ASSERT (os_get_cpu_number () == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
Damjan Marionb28e4982016-08-22 22:34:38 +020074 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Damjan Marion25665672016-08-16 18:56:08 +020075 {
76 vnet_hw_interface_set_flags (dm->vnet_main, xd->vlib_hw_if_index, 0);
77 rte_eth_dev_stop (xd->device_index);
78 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
80 rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
Damjan Marion25665672016-08-16 18:56:08 +020081 xd->tx_q_used, &xd->port_conf);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 if (rv < 0)
84 return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
Damjan Marion25665672016-08-16 18:56:08 +020085 xd->device_index, rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
87 /* Set up one TX-queue per worker thread */
88 for (j = 0; j < xd->tx_q_used; j++)
89 {
Damjan Marion25665672016-08-16 18:56:08 +020090 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
91 xd->cpu_socket, &xd->tx_conf);
Damjan Marionae605262016-04-21 21:42:40 +020092
93 /* retry with any other CPU socket */
94 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +020095 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
96 SOCKET_ID_ANY, &xd->tx_conf);
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +020098 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 }
100
Damjan Marion25665672016-08-16 18:56:08 +0200101 if (rv < 0)
102 return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
103 xd->device_index, rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
105 for (j = 0; j < xd->rx_q_used; j++)
106 {
107
Damjan Marion25665672016-08-16 18:56:08 +0200108 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
109 xd->cpu_socket, 0,
110 bm->
111 pktmbuf_pools[xd->cpu_socket_id_by_queue
112 [j]]);
Damjan Marionae605262016-04-21 21:42:40 +0200113
114 /* retry with any other CPU socket */
115 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +0200116 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
117 SOCKET_ID_ANY, 0,
118 bm->
119 pktmbuf_pools[xd->cpu_socket_id_by_queue
120 [j]]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +0200122 return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
123 xd->device_index, rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 }
125
Damjan Marionb28e4982016-08-22 22:34:38 +0200126 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Damjan Marion25665672016-08-16 18:56:08 +0200127 {
Dave Barachdd522cb2016-08-10 16:56:16 -0400128 int rv;
129 rv = rte_eth_dev_start (xd->device_index);
130 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +0200131 clib_warning ("rte_eth_dev_start %d returned %d",
132 xd->device_index, rv);
133 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 return 0;
135}
136
Damjan Marion25665672016-08-16 18:56:08 +0200137static u32
138dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139{
Damjan Marion25665672016-08-16 18:56:08 +0200140 dpdk_main_t *dm = &dpdk_main;
141 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 u32 old = 0;
143
Damjan Marion25665672016-08-16 18:56:08 +0200144 if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 {
Damjan Marionb28e4982016-08-22 22:34:38 +0200146 old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
Damjan Marion25665672016-08-16 18:56:08 +0200147
Damjan Marionb28e4982016-08-22 22:34:38 +0200148 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
149 xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
150 else
151 xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
152
153 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 {
Damjan Marionb28e4982016-08-22 22:34:38 +0200155 if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
Damjan Marion25665672016-08-16 18:56:08 +0200156 rte_eth_promiscuous_enable (xd->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 else
Damjan Marion25665672016-08-16 18:56:08 +0200158 rte_eth_promiscuous_disable (xd->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 }
160 }
Damjan Marion25665672016-08-16 18:56:08 +0200161 else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU (flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 {
163 /*
164 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
Damjan Marion25665672016-08-16 18:56:08 +0200165 * driver to dynamically change the mtu. If/when the
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 * VIC firmware gets fixed, then this should be removed.
167 */
Damjan Marion3a944752016-07-06 23:13:39 +0200168 if (xd->pmd == VNET_DPDK_PMD_ENIC)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 {
170 struct rte_eth_dev_info dev_info;
171
172 /*
173 * Restore mtu to what has been set by CIMC in the firmware cfg.
174 */
Damjan Marion25665672016-08-16 18:56:08 +0200175 rte_eth_dev_info_get (xd->device_index, &dev_info);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 hi->max_packet_bytes = dev_info.max_rx_pktlen;
177
Damjan Marion25665672016-08-16 18:56:08 +0200178 vlib_cli_output (vlib_get_main (),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 "Cisco VIC mtu can only be changed "
180 "using CIMC then rebooting the server!");
181 }
182 else
183 {
184 int rv;
Damjan Marion25665672016-08-16 18:56:08 +0200185
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
187
Damjan Marionb28e4982016-08-22 22:34:38 +0200188 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 rte_eth_dev_stop (xd->device_index);
190
Damjan Marion25665672016-08-16 18:56:08 +0200191 rv = rte_eth_dev_configure
192 (xd->device_index, xd->rx_q_used, xd->tx_q_used, &xd->port_conf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
194 if (rv < 0)
Damjan Marion25665672016-08-16 18:56:08 +0200195 vlib_cli_output (vlib_get_main (),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 "rte_eth_dev_configure[%d]: err %d",
197 xd->device_index, rv);
198
Damjan Marion25665672016-08-16 18:56:08 +0200199 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
Damjan Marionb28e4982016-08-22 22:34:38 +0200201 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
Damjan Marion25665672016-08-16 18:56:08 +0200202 {
203 int rv = rte_eth_dev_start (xd->device_index);
204 if (rv < 0)
205 clib_warning ("rte_eth_dev_start %d returned %d",
206 xd->device_index, rv);
207 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 }
209 }
210 return old;
211}
212
Damjan Marion85cdbd02016-02-12 18:00:23 +0100213void
Damjan Marion25665672016-08-16 18:56:08 +0200214dpdk_device_lock_init (dpdk_device_t * xd)
Damjan Marion85cdbd02016-02-12 18:00:23 +0100215{
216 int q;
Damjan Marion25665672016-08-16 18:56:08 +0200217 vec_validate (xd->lockp, xd->tx_q_used - 1);
Damjan Marion85cdbd02016-02-12 18:00:23 +0100218 for (q = 0; q < xd->tx_q_used; q++)
219 {
220 xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
Damjan Marion25665672016-08-16 18:56:08 +0200221 CLIB_CACHE_LINE_BYTES);
Damjan Marion85cdbd02016-02-12 18:00:23 +0100222 memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion25665672016-08-16 18:56:08 +0200223 }
Damjan Marion85cdbd02016-02-12 18:00:23 +0100224}
225
226void
Damjan Marion25665672016-08-16 18:56:08 +0200227dpdk_device_lock_free (dpdk_device_t * xd)
Damjan Marion85cdbd02016-02-12 18:00:23 +0100228{
229 int q;
230
Damjan Marion25665672016-08-16 18:56:08 +0200231 for (q = 0; q < vec_len (xd->lockp); q++)
232 clib_mem_free ((void *) xd->lockp[q]);
233 vec_free (xd->lockp);
Damjan Marion85cdbd02016-02-12 18:00:23 +0100234 xd->lockp = 0;
235}
236
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237static clib_error_t *
238dpdk_lib_init (dpdk_main_t * dm)
239{
240 u32 nports;
241 u32 nb_desc = 0;
242 int i;
Damjan Marion25665672016-08-16 18:56:08 +0200243 clib_error_t *error;
244 vlib_main_t *vm = vlib_get_main ();
245 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion25665672016-08-16 18:56:08 +0200246 vnet_sw_interface_t *sw;
247 vnet_hw_interface_t *hi;
248 dpdk_device_t *xd;
Damjan Marion32f7bc12016-07-21 03:42:37 -0700249 vlib_pci_addr_t last_pci_addr;
250 u32 last_pci_addr_port = 0;
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100251 vlib_thread_registration_t *tr, *tr_hqos;
252 uword *p, *p_hqos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100254 u32 next_cpu = 0, next_hqos_cpu = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 u8 af_packet_port_id = 0;
Damjan Marion32f7bc12016-07-21 03:42:37 -0700256 last_pci_addr.as_u32 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
258 dm->input_cpu_first_index = 0;
259 dm->input_cpu_count = 1;
260
261 /* find out which cpus will be used for input */
Damjan Marionbf741472016-06-13 22:49:44 +0200262 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
264
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265 if (tr && tr->count > 0)
266 {
267 dm->input_cpu_first_index = tr->first_index;
268 dm->input_cpu_count = tr->count;
269 }
270
271 vec_validate_aligned (dm->devices_by_cpu, tm->n_vlib_mains - 1,
Damjan Marion25665672016-08-16 18:56:08 +0200272 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
274 vec_validate_aligned (dm->workers, tm->n_vlib_mains - 1,
Damjan Marion25665672016-08-16 18:56:08 +0200275 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100277 dm->hqos_cpu_first_index = 0;
278 dm->hqos_cpu_count = 0;
279
280 /* find out which cpus will be used for I/O TX */
281 p_hqos = hash_get_mem (tm->thread_registrations_by_name, "hqos-threads");
282 tr_hqos = p_hqos ? (vlib_thread_registration_t *) p_hqos[0] : 0;
283
284 if (tr_hqos && tr_hqos->count > 0)
285 {
286 dm->hqos_cpu_first_index = tr_hqos->first_index;
287 dm->hqos_cpu_count = tr_hqos->count;
288 }
289
290 vec_validate_aligned (dm->devices_by_hqos_cpu, tm->n_vlib_mains - 1,
291 CLIB_CACHE_LINE_BYTES);
292
293 vec_validate_aligned (dm->hqos_threads, tm->n_vlib_mains - 1,
294 CLIB_CACHE_LINE_BYTES);
295
Damjan Marion25665672016-08-16 18:56:08 +0200296 nports = rte_eth_dev_count ();
297 if (nports < 1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 {
299 clib_warning ("DPDK drivers found no ports...");
300 }
301
302 if (CLIB_DEBUG > 0)
303 clib_warning ("DPDK drivers found %d ports...", nports);
304
Damjan Marion25665672016-08-16 18:56:08 +0200305 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 * All buffers are all allocated from the same rte_mempool.
307 * Thus they all have the same number of data bytes.
308 */
Damjan Marion25665672016-08-16 18:56:08 +0200309 dm->vlib_buffer_free_list_index =
310 vlib_buffer_get_or_create_free_list (vm,
311 VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
312 "dpdk rx");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
Damjan Marion64ae6692016-05-25 18:40:13 +0200314 if (dm->conf->enable_tcp_udp_checksum)
315 dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
316 | IP_BUFFER_L4_CHECKSUM_COMPUTED);
317
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 for (i = 0; i < nports; i++)
319 {
320 u8 addr[6];
John Loce3e9712016-07-07 13:54:44 -0400321 u8 vlan_strip = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 int j;
323 struct rte_eth_dev_info dev_info;
Damjan Marion25665672016-08-16 18:56:08 +0200324 clib_error_t *rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 struct rte_eth_link l;
Damjan Marion25665672016-08-16 18:56:08 +0200326 dpdk_device_config_t *devconf = 0;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200327 vlib_pci_addr_t pci_addr;
Damjan Marion25665672016-08-16 18:56:08 +0200328 uword *p = 0;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200329
Damjan Marion25665672016-08-16 18:56:08 +0200330 rte_eth_dev_info_get (i, &dev_info);
331 if (dev_info.pci_dev) /* bonded interface has no pci info */
332 {
John Lo55ef1b12016-06-08 23:21:35 -0400333 pci_addr.domain = dev_info.pci_dev->addr.domain;
334 pci_addr.bus = dev_info.pci_dev->addr.bus;
335 pci_addr.slot = dev_info.pci_dev->addr.devid;
336 pci_addr.function = dev_info.pci_dev->addr.function;
Damjan Marion25665672016-08-16 18:56:08 +0200337 p =
338 hash_get (dm->conf->device_config_index_by_pci_addr,
339 pci_addr.as_u32);
340 }
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200341
342 if (p)
343 devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
344 else
345 devconf = &dm->conf->default_devconf;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
347 /* Create vnet interface */
348 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
349 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
350 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
Damjan Marion25665672016-08-16 18:56:08 +0200351 xd->cpu_socket = (i8) rte_eth_dev_socket_id (i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Damjan Marion32f7bc12016-07-21 03:42:37 -0700353 /* Handle interface naming for devices with multiple ports sharing same PCI ID */
354 if (dev_info.pci_dev)
355 {
Damjan Marion25665672016-08-16 18:56:08 +0200356 struct rte_eth_dev_info di = { 0 };
Damjan Marion32f7bc12016-07-21 03:42:37 -0700357 rte_eth_dev_info_get (i + 1, &di);
358 if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
Damjan Marion25665672016-08-16 18:56:08 +0200359 memcmp (&dev_info.pci_dev->addr, &di.pci_dev->addr,
360 sizeof (struct rte_pci_addr)) == 0)
Damjan Marion32f7bc12016-07-21 03:42:37 -0700361 {
362 xd->interface_name_suffix = format (0, "0");
363 last_pci_addr.as_u32 = pci_addr.as_u32;
364 last_pci_addr_port = i;
365 }
366 else if (pci_addr.as_u32 == last_pci_addr.as_u32)
367 {
Damjan Marion25665672016-08-16 18:56:08 +0200368 xd->interface_name_suffix =
369 format (0, "%u", i - last_pci_addr_port);
Damjan Marion32f7bc12016-07-21 03:42:37 -0700370 }
371 else
372 {
373 last_pci_addr.as_u32 = ~0;
374 }
375 }
376 else
377 last_pci_addr.as_u32 = ~0;
378
Damjan Marion25665672016-08-16 18:56:08 +0200379 clib_memcpy (&xd->tx_conf, &dev_info.default_txconf,
380 sizeof (struct rte_eth_txconf));
Damjan Marion64ae6692016-05-25 18:40:13 +0200381 if (dm->conf->no_multi_seg)
Damjan Marion25665672016-08-16 18:56:08 +0200382 {
383 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
384 port_conf_template.rxmode.jumbo_frame = 0;
385 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 else
Damjan Marion25665672016-08-16 18:56:08 +0200387 {
388 xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
389 port_conf_template.rxmode.jumbo_frame = 1;
Damjan Marion7dc41462016-11-15 19:47:58 +0100390 xd->flags |= DPDK_DEVICE_FLAG_MAYBE_MULTISEG;
Damjan Marion25665672016-08-16 18:56:08 +0200391 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Damjan Marion25665672016-08-16 18:56:08 +0200393 clib_memcpy (&xd->port_conf, &port_conf_template,
394 sizeof (struct rte_eth_conf));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395
Damjan Marion25665672016-08-16 18:56:08 +0200396 xd->tx_q_used = clib_min (dev_info.max_tx_queues, tm->n_vlib_mains);
Damjan Marion85cdbd02016-02-12 18:00:23 +0100397
Damjan Marion25665672016-08-16 18:56:08 +0200398 if (devconf->num_tx_queues > 0
399 && devconf->num_tx_queues < xd->tx_q_used)
400 xd->tx_q_used = clib_min (xd->tx_q_used, devconf->num_tx_queues);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200402 if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
403 {
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200404 dm->use_rss = 1;
405 }
406
Damjan Marion25665672016-08-16 18:56:08 +0200407 if (devconf->num_rx_queues > 1
408 && dev_info.max_rx_queues >= devconf->num_rx_queues)
409 {
410 xd->rx_q_used = devconf->num_rx_queues;
411 xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
412 if (devconf->rss_fn == 0)
413 xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
414 ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
415 else
416 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
417 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 else
Damjan Marion25665672016-08-16 18:56:08 +0200419 xd->rx_q_used = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
Damjan Marion56431702016-09-19 13:18:09 +0200421 xd->flags |= DPDK_DEVICE_FLAG_PMD;
Damjan Marion2068e982016-01-27 16:59:04 +0100422
423 /* workaround for drivers not setting driver_name */
John Lo55ef1b12016-06-08 23:21:35 -0400424 if ((!dev_info.driver_name) && (dev_info.pci_dev))
Damjan Marion39162272016-10-25 14:54:38 +0200425#if RTE_VERSION < RTE_VERSION_NUM(16, 11, 0, 0)
Damjan Marion25665672016-08-16 18:56:08 +0200426 dev_info.driver_name = dev_info.pci_dev->driver->name;
Damjan Marion39162272016-10-25 14:54:38 +0200427#else
428 dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
429#endif
Damjan Marion25665672016-08-16 18:56:08 +0200430 ASSERT (dev_info.driver_name);
Damjan Marion2068e982016-01-27 16:59:04 +0100431
Damjan Marion25665672016-08-16 18:56:08 +0200432 if (!xd->pmd)
433 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
435
Dave Barachdd522cb2016-08-10 16:56:16 -0400436#define _(s,f) else if (dev_info.driver_name && \
437 !strcmp(dev_info.driver_name, s)) \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 xd->pmd = VNET_DPDK_PMD_##f;
Damjan Marion25665672016-08-16 18:56:08 +0200439 if (0)
440 ;
441 foreach_dpdk_pmd
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442#undef _
Damjan Marion25665672016-08-16 18:56:08 +0200443 else
444 xd->pmd = VNET_DPDK_PMD_UNKNOWN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
Damjan Marion696f1ad2016-12-23 22:42:41 +0100446 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
447 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
448 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449
Damjan Marion25665672016-08-16 18:56:08 +0200450 switch (xd->pmd)
451 {
452 /* 1G adapters */
453 case VNET_DPDK_PMD_E1000EM:
454 case VNET_DPDK_PMD_IGB:
455 case VNET_DPDK_PMD_IGBVF:
456 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
457 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458
Damjan Marion25665672016-08-16 18:56:08 +0200459 /* 10G adapters */
460 case VNET_DPDK_PMD_IXGBE:
461 case VNET_DPDK_PMD_IXGBEVF:
462 case VNET_DPDK_PMD_THUNDERX:
463 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
Damjan Marion25665672016-08-16 18:56:08 +0200464 break;
465 case VNET_DPDK_PMD_DPAA2:
466 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
467 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Damjan Marion25665672016-08-16 18:56:08 +0200469 /* Cisco VIC */
470 case VNET_DPDK_PMD_ENIC:
471 rte_eth_link_get_nowait (i, &l);
John Loa60d4cb2016-12-17 03:09:58 -0500472 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
Damjan Marion25665672016-08-16 18:56:08 +0200473 if (l.link_speed == 40000)
Damjan Marion696f1ad2016-12-23 22:42:41 +0100474 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
Damjan Marion25665672016-08-16 18:56:08 +0200475 else
Damjan Marion696f1ad2016-12-23 22:42:41 +0100476 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
Damjan Marion25665672016-08-16 18:56:08 +0200477 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
Damjan Marion25665672016-08-16 18:56:08 +0200479 /* Intel Fortville */
480 case VNET_DPDK_PMD_I40E:
481 case VNET_DPDK_PMD_I40EVF:
John Loa60d4cb2016-12-17 03:09:58 -0500482 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
Damjan Marion25665672016-08-16 18:56:08 +0200483 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484
Damjan Marion25665672016-08-16 18:56:08 +0200485 switch (dev_info.pci_dev->id.device_id)
486 {
487 case I40E_DEV_ID_10G_BASE_T:
488 case I40E_DEV_ID_SFP_XL710:
489 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
490 break;
491 case I40E_DEV_ID_QSFP_A:
492 case I40E_DEV_ID_QSFP_B:
493 case I40E_DEV_ID_QSFP_C:
494 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
495 break;
496 case I40E_DEV_ID_VF:
497 rte_eth_link_get_nowait (i, &l);
498 xd->port_type = l.link_speed == 10000 ?
499 VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
500 break;
501 default:
502 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
503 }
504 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
Damjan Marion25665672016-08-16 18:56:08 +0200506 case VNET_DPDK_PMD_CXGBE:
507 switch (dev_info.pci_dev->id.device_id)
508 {
509 case 0x540d: /* T580-CR */
510 case 0x5410: /* T580-LP-cr */
Damjan Marion25665672016-08-16 18:56:08 +0200511 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
512 break;
513 case 0x5403: /* T540-CR */
Damjan Marion25665672016-08-16 18:56:08 +0200514 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
515 break;
516 default:
Damjan Marion25665672016-08-16 18:56:08 +0200517 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
518 }
519 break;
Damjan Marion2068e982016-01-27 16:59:04 +0100520
Damjan Marion696f1ad2016-12-23 22:42:41 +0100521 case VNET_DPDK_PMD_MLX5:
522 {
523 char *pn_100g[] = { "MCX415A-CCAT", "MCX416A-CCAT", 0 };
524 char *pn_40g[] = { "MCX413A-BCAT", "MCX414A-BCAT",
525 "MCX415A-BCAT", "MCX416A-BCAT", "MCX4131A-BCAT", 0
526 };
527 char *pn_10g[] = { "MCX4111A-XCAT", "MCX4121A-XCAT", 0 };
528
529 vlib_pci_device_t *pd = vlib_get_pci_device (&pci_addr);
530 u8 *pn = 0;
531 char **c;
532 int found = 0;
533 pn = format (0, "%U%c",
534 format_vlib_pci_vpd, pd->vpd_r, "PN", 0);
535
536 if (!pn)
537 break;
538
539 c = pn_100g;
540 while (!found && c[0])
541 {
542 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
543 {
544 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_100G;
545 break;
546 }
547 c++;
548 }
549
550 c = pn_40g;
551 while (!found && c[0])
552 {
553 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
554 {
555 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
556 break;
557 }
558 c++;
559 }
560
561 c = pn_10g;
562 while (!found && c[0])
563 {
564 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
565 {
566 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
567 break;
568 }
569 c++;
570 }
571
572 vec_free (pn);
573 }
574
575 break;
Damjan Marion25665672016-08-16 18:56:08 +0200576 /* Intel Red Rock Canyon */
577 case VNET_DPDK_PMD_FM10K:
578 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
Damjan Marion25665672016-08-16 18:56:08 +0200579 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Damjan Marion25665672016-08-16 18:56:08 +0200581 /* virtio */
582 case VNET_DPDK_PMD_VIRTIO:
583 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
584 xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
585 xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
586 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Damjan Marion25665672016-08-16 18:56:08 +0200588 /* vmxnet3 */
589 case VNET_DPDK_PMD_VMXNET3:
590 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
591 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
592 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Damjan Marion25665672016-08-16 18:56:08 +0200594 case VNET_DPDK_PMD_AF_PACKET:
595 xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
596 xd->af_packet_port_id = af_packet_port_id++;
597 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598
Damjan Marion25665672016-08-16 18:56:08 +0200599 case VNET_DPDK_PMD_BOND:
John Loa60d4cb2016-12-17 03:09:58 -0500600 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
Damjan Marion25665672016-08-16 18:56:08 +0200601 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
602 break;
John Lod9bf9ab2016-02-25 11:17:55 -0500603
Damjan Marion25665672016-08-16 18:56:08 +0200604 default:
605 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
606 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
Damjan Marion25665672016-08-16 18:56:08 +0200608 if (devconf->num_rx_desc)
609 xd->nb_rx_desc = devconf->num_rx_desc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610
Damjan Marion25665672016-08-16 18:56:08 +0200611 if (devconf->num_tx_desc)
612 xd->nb_tx_desc = devconf->num_tx_desc;
613 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
615 /*
616 * Ensure default mtu is not > the mtu read from the hardware.
617 * Otherwise rte_eth_dev_configure() will fail and the port will
618 * not be available.
619 */
Jurek Matuszewskia69318b2016-03-28 15:15:15 -0400620 if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
Damjan Marion25665672016-08-16 18:56:08 +0200621 {
622 /*
623 * This device does not support the platforms's max frame
624 * size. Use it's advertised mru instead.
625 */
626 xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
627 }
Jurek Matuszewskia69318b2016-03-28 15:15:15 -0400628 else
Damjan Marion25665672016-08-16 18:56:08 +0200629 {
630 xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
Jurek Matuszewskia69318b2016-03-28 15:15:15 -0400631
Damjan Marion25665672016-08-16 18:56:08 +0200632 /*
633 * Some platforms do not account for Ethernet FCS (4 bytes) in
634 * MTU calculations. To interop with them increase mru but only
635 * if the device's settings can support it.
636 */
637 if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
638 xd->port_conf.rxmode.hw_strip_crc)
639 {
640 /*
641 * Allow additional 4 bytes (for Ethernet FCS). These bytes are
642 * stripped by h/w and so will not consume any buffer memory.
643 */
644 xd->port_conf.rxmode.max_rx_pkt_len += 4;
645 }
646 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648 if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
Damjan Marion25665672016-08-16 18:56:08 +0200649 {
650 f64 now = vlib_time_now (vm);
651 u32 rnd;
652 rnd = (u32) (now * 1e6);
653 rnd = random_u32 (&rnd);
654 clib_memcpy (addr + 2, &rnd, sizeof (rnd));
655 addr[0] = 2;
656 addr[1] = 0xfe;
657 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658 else
Damjan Marion25665672016-08-16 18:56:08 +0200659 rte_eth_macaddr_get (i, (struct ether_addr *) addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660
661 if (xd->tx_q_used < tm->n_vlib_mains)
Damjan Marion25665672016-08-16 18:56:08 +0200662 dpdk_device_lock_init (xd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
664 xd->device_index = xd - dm->devices;
Damjan Marion25665672016-08-16 18:56:08 +0200665 ASSERT (i == xd->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 xd->per_interface_next_index = ~0;
667
668 /* assign interface to input thread */
Damjan Marion25665672016-08-16 18:56:08 +0200669 dpdk_device_and_queue_t *dq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670 int q;
671
Damjan Marion0b140722016-06-14 00:36:09 +0200672 if (devconf->workers)
673 {
674 int i;
675 q = 0;
Damjan Marion25665672016-08-16 18:56:08 +0200676 /* *INDENT-OFF* */
Damjan Marion0b140722016-06-14 00:36:09 +0200677 clib_bitmap_foreach (i, devconf->workers, ({
678 int cpu = dm->input_cpu_first_index + i;
Pavel Kotucek98765202016-10-07 08:37:28 +0200679 unsigned lcore = vlib_worker_threads[cpu].lcore_id;
Damjan Marion0b140722016-06-14 00:36:09 +0200680 vec_validate(xd->cpu_socket_id_by_queue, q);
681 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
682 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
683 dq->device = xd->device_index;
684 dq->queue_id = q++;
685 }));
Damjan Marion25665672016-08-16 18:56:08 +0200686 /* *INDENT-ON* */
Damjan Marion0b140722016-06-14 00:36:09 +0200687 }
688 else
689 for (q = 0; q < xd->rx_q_used; q++)
690 {
691 int cpu = dm->input_cpu_first_index + next_cpu;
Pavel Kotucek98765202016-10-07 08:37:28 +0200692 unsigned lcore = vlib_worker_threads[cpu].lcore_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693
Damjan Marion0b140722016-06-14 00:36:09 +0200694 /*
695 * numa node for worker thread handling this queue
696 * needed for taking buffers from the right mempool
697 */
Damjan Marion25665672016-08-16 18:56:08 +0200698 vec_validate (xd->cpu_socket_id_by_queue, q);
699 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id (lcore);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
Damjan Marion0b140722016-06-14 00:36:09 +0200701 /*
702 * construct vector of (device,queue) pairs for each worker thread
703 */
Damjan Marion25665672016-08-16 18:56:08 +0200704 vec_add2 (dm->devices_by_cpu[cpu], dq, 1);
Damjan Marion0b140722016-06-14 00:36:09 +0200705 dq->device = xd->device_index;
706 dq->queue_id = q;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
Damjan Marion0b140722016-06-14 00:36:09 +0200708 next_cpu++;
709 if (next_cpu == dm->input_cpu_count)
710 next_cpu = 0;
711 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100713
714 if (devconf->hqos_enabled)
715 {
716 xd->flags |= DPDK_DEVICE_FLAG_HQOS;
717
718 if (devconf->hqos.hqos_thread_valid)
719 {
720 int cpu = dm->hqos_cpu_first_index + devconf->hqos.hqos_thread;
721
722 if (devconf->hqos.hqos_thread >= dm->hqos_cpu_count)
723 return clib_error_return (0, "invalid HQoS thread index");
724
725 vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
726 dq->device = xd->device_index;
727 dq->queue_id = 0;
728 }
729 else
730 {
731 int cpu = dm->hqos_cpu_first_index + next_hqos_cpu;
732
733 if (dm->hqos_cpu_count == 0)
734 return clib_error_return (0, "no HQoS threads available");
735
736 vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
737 dq->device = xd->device_index;
738 dq->queue_id = 0;
739
740 next_hqos_cpu++;
741 if (next_hqos_cpu == dm->hqos_cpu_count)
742 next_hqos_cpu = 0;
743
744 devconf->hqos.hqos_thread_valid = 1;
745 devconf->hqos.hqos_thread = cpu;
746 }
747 }
748
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
Damjan Marion25665672016-08-16 18:56:08 +0200750 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 for (j = 0; j < tm->n_vlib_mains; j++)
Damjan Marion25665672016-08-16 18:56:08 +0200752 {
Damjan Marionb58598b2016-09-19 13:44:37 +0200753 vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
Damjan Marion25665672016-08-16 18:56:08 +0200754 sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
755 vec_reset_length (xd->tx_vectors[j]);
756 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757
758 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
Damjan Marion25665672016-08-16 18:56:08 +0200759 CLIB_CACHE_LINE_BYTES);
760 for (j = 0; j < xd->rx_q_used; j++)
761 {
762 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
763 CLIB_CACHE_LINE_BYTES);
764 vec_reset_length (xd->rx_vectors[j]);
765 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
Damjan Marion4015b192016-11-18 11:41:37 +0100767 vec_validate_aligned (xd->d_trace_buffers, tm->n_vlib_mains,
768 CLIB_CACHE_LINE_BYTES);
769
Damjan Marion25665672016-08-16 18:56:08 +0200770 rv = dpdk_port_setup (dm, xd);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771
Dave Barachdd522cb2016-08-10 16:56:16 -0400772 if (rv)
Damjan Marion25665672016-08-16 18:56:08 +0200773 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100775 if (devconf->hqos_enabled)
776 {
777 rv = dpdk_port_setup_hqos (xd, &devconf->hqos);
Dave Barach8d2e0e02016-11-09 11:28:36 -0500778 if (rv)
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100779 return rv;
780 }
781
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 /* count the number of descriptors used for this device */
783 nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
784
785 error = ethernet_register_interface
Damjan Marion25665672016-08-16 18:56:08 +0200786 (dm->vnet_main, dpdk_device_class.index, xd->device_index,
787 /* ethernet address */ addr,
788 &xd->vlib_hw_if_index, dpdk_flag_change);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 if (error)
Damjan Marion25665672016-08-16 18:56:08 +0200790 return error;
791
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
793 xd->vlib_sw_if_index = sw->sw_if_index;
794 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
795
796 /*
797 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
Damjan Marion25665672016-08-16 18:56:08 +0200798 * driver to dynamically change the mtu. If/when the
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 * VIC firmware gets fixed, then this should be removed.
800 */
Damjan Marion3a944752016-07-06 23:13:39 +0200801 if (xd->pmd == VNET_DPDK_PMD_ENIC)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 {
803 /*
804 * Initialize mtu to what has been set by CIMC in the firmware cfg.
805 */
806 hi->max_packet_bytes = dev_info.max_rx_pktlen;
John Loce3e9712016-07-07 13:54:44 -0400807 if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
Damjan Marion25665672016-08-16 18:56:08 +0200808 vlan_strip = 1; /* remove vlan tag from VIC port by default */
John Loce3e9712016-07-07 13:54:44 -0400809 else
Damjan Marion25665672016-08-16 18:56:08 +0200810 clib_warning ("VLAN strip disabled for interface\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811 }
John Loce3e9712016-07-07 13:54:44 -0400812 else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
813 vlan_strip = 1;
814
815 if (vlan_strip)
Damjan Marion25665672016-08-16 18:56:08 +0200816 {
John Loce3e9712016-07-07 13:54:44 -0400817 int vlan_off;
Damjan Marion25665672016-08-16 18:56:08 +0200818 vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
John Loce3e9712016-07-07 13:54:44 -0400819 vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
jerryian5165cbc2016-11-18 11:20:19 +0800820 xd->port_conf.rxmode.hw_vlan_strip = vlan_off;
Damjan Marion25665672016-08-16 18:56:08 +0200821 if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
822 clib_warning ("VLAN strip enabled for interface\n");
John Loce3e9712016-07-07 13:54:44 -0400823 else
Damjan Marion25665672016-08-16 18:56:08 +0200824 clib_warning ("VLAN strip cannot be supported by interface\n");
825 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826
Damjan Marion25665672016-08-16 18:56:08 +0200827 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
828 xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829
Damjan Marion25665672016-08-16 18:56:08 +0200830 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831 }
832
Damjan Marion25665672016-08-16 18:56:08 +0200833 if (nb_desc > dm->conf->num_mbufs)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834 clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
Damjan Marion25665672016-08-16 18:56:08 +0200835 dm->conf->num_mbufs, nb_desc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837 return 0;
838}
839
Damjan Mariona42cd342016-04-13 18:03:20 +0200840static void
Damjan Marion64ae6692016-05-25 18:40:13 +0200841dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842{
Damjan Marion25665672016-08-16 18:56:08 +0200843 vlib_pci_main_t *pm = &pci_main;
844 clib_error_t *error;
845 vlib_pci_device_t *d;
Damjan Marion25665672016-08-16 18:56:08 +0200846 u8 *pci_addr = 0;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200847 int num_whitelisted = vec_len (conf->dev_confs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848
Damjan Marion25665672016-08-16 18:56:08 +0200849 /* *INDENT-OFF* */
Damjan Mariona42cd342016-04-13 18:03:20 +0200850 pool_foreach (d, pm->pci_devs, ({
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200851 dpdk_device_config_t * devconf = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200852 vec_reset_length (pci_addr);
853 pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854
Damjan Marionb1b1a142016-08-23 00:53:22 +0200855 if (d->device_class != PCI_CLASS_NETWORK_ETHERNET)
Damjan Mariona42cd342016-04-13 18:03:20 +0200856 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200858 if (num_whitelisted)
859 {
860 uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
861
862 if (!p)
863 continue;
864
865 devconf = pool_elt_at_index (conf->dev_confs, p[0]);
866 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
Damjan Mariona42cd342016-04-13 18:03:20 +0200868 /* virtio */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200869 if (d->vendor_id == 0x1af4 && d->device_id == 0x1000)
Damjan Mariona42cd342016-04-13 18:03:20 +0200870 ;
871 /* vmxnet3 */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200872 else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
Damjan Mariona42cd342016-04-13 18:03:20 +0200873 ;
874 /* all Intel devices */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200875 else if (d->vendor_id == 0x8086)
Damjan Mariona42cd342016-04-13 18:03:20 +0200876 ;
877 /* Cisco VIC */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200878 else if (d->vendor_id == 0x1137 && d->device_id == 0x0043)
Damjan Mariona42cd342016-04-13 18:03:20 +0200879 ;
880 /* Chelsio T4/T5 */
Damjan Marionb1b1a142016-08-23 00:53:22 +0200881 else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
Damjan Mariona42cd342016-04-13 18:03:20 +0200882 ;
883 else
884 {
885 clib_warning ("Unsupported Ethernet PCI device 0x%04x:0x%04x found "
Damjan Marionb1b1a142016-08-23 00:53:22 +0200886 "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
Damjan Mariona42cd342016-04-13 18:03:20 +0200887 pci_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888 continue;
Damjan Mariona42cd342016-04-13 18:03:20 +0200889 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890
Damjan Marion64ae6692016-05-25 18:40:13 +0200891 error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
Damjan Mariona42cd342016-04-13 18:03:20 +0200893 if (error)
894 {
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200895 if (devconf == 0)
896 {
897 pool_get (conf->dev_confs, devconf);
898 hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
899 devconf - conf->dev_confs);
900 devconf->pci_addr.as_u32 = d->bus_address.as_u32;
901 }
902 devconf->is_blacklisted = 1;
Damjan Mariona42cd342016-04-13 18:03:20 +0200903 clib_error_report (error);
904 }
905 }));
Damjan Marion25665672016-08-16 18:56:08 +0200906 /* *INDENT-ON* */
Damjan Mariona42cd342016-04-13 18:03:20 +0200907 vec_free (pci_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908}
909
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910static clib_error_t *
Damjan Marion25665672016-08-16 18:56:08 +0200911dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
912 unformat_input_t * input, u8 is_default)
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200913{
Damjan Marion25665672016-08-16 18:56:08 +0200914 clib_error_t *error = 0;
915 uword *p;
916 dpdk_device_config_t *devconf;
Srivatsa Sangli820e3632016-06-14 13:10:55 -0700917 unformat_input_t sub_input;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200918
919 if (is_default)
920 {
921 devconf = &conf->default_devconf;
922 }
923 else
924 {
925 p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
926
927 if (!p)
928 {
929 pool_get (conf->dev_confs, devconf);
Damjan Marion25665672016-08-16 18:56:08 +0200930 hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
931 devconf - conf->dev_confs);
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200932 }
933 else
Damjan Marion25665672016-08-16 18:56:08 +0200934 return clib_error_return (0,
935 "duplicate configuration for PCI address %U",
936 format_vlib_pci_addr, &pci_addr);
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200937 }
938
939 devconf->pci_addr.as_u32 = pci_addr.as_u32;
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100940 devconf->hqos_enabled = 0;
941 dpdk_device_config_hqos_default (&devconf->hqos);
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200942
943 if (!input)
944 return 0;
945
Pierre Pfister446e1f52016-11-02 10:06:49 +0000946 unformat_skip_white_space (input);
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200947 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
948 {
949 if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
950 ;
Damjan Marion39da6212016-06-06 13:21:04 +0200951 else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
952 ;
953 else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
954 ;
955 else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200956 ;
Damjan Marion0b140722016-06-14 00:36:09 +0200957 else if (unformat (input, "workers %U", unformat_bitmap_list,
958 &devconf->workers))
959 ;
Damjan Marion25665672016-08-16 18:56:08 +0200960 else
961 if (unformat
962 (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
963 {
964 error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
965 if (error)
966 break;
967 }
John Loce3e9712016-07-07 13:54:44 -0400968 else if (unformat (input, "vlan-strip-offload off"))
Damjan Marion25665672016-08-16 18:56:08 +0200969 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
John Loce3e9712016-07-07 13:54:44 -0400970 else if (unformat (input, "vlan-strip-offload on"))
Damjan Marion25665672016-08-16 18:56:08 +0200971 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200972 else
Jasvinder Singh85ecc812016-07-21 17:02:19 +0100973 if (unformat
974 (input, "hqos %U", unformat_vlib_cli_sub_input, &sub_input))
975 {
976 devconf->hqos_enabled = 1;
977 error = unformat_hqos (&sub_input, &devconf->hqos);
978 if (error)
979 break;
980 }
981 else if (unformat (input, "hqos"))
982 {
983 devconf->hqos_enabled = 1;
984 }
985 else
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200986 {
987 error = clib_error_return (0, "unknown input `%U'",
988 format_unformat_error, input);
989 break;
990 }
991 }
Damjan Marion0b140722016-06-14 00:36:09 +0200992
993 if (error)
994 return error;
995
996 if (devconf->workers && devconf->num_rx_queues == 0)
Damjan Marion25665672016-08-16 18:56:08 +0200997 devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
Damjan Marion0b140722016-06-14 00:36:09 +0200998 else if (devconf->workers &&
Damjan Marion25665672016-08-16 18:56:08 +0200999 clib_bitmap_count_set_bits (devconf->workers) !=
1000 devconf->num_rx_queues)
1001 error =
1002 clib_error_return (0,
1003 "%U: number of worker threadds must be "
1004 "equal to number of rx queues", format_vlib_pci_addr,
1005 &pci_addr);
Damjan Marion0b140722016-06-14 00:36:09 +02001006
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001007 return error;
1008}
1009
1010static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011dpdk_config (vlib_main_t * vm, unformat_input_t * input)
1012{
Damjan Marion25665672016-08-16 18:56:08 +02001013 clib_error_t *error = 0;
1014 dpdk_main_t *dm = &dpdk_main;
1015 dpdk_config_main_t *conf = &dpdk_config_main;
1016 vlib_thread_main_t *tm = vlib_get_thread_main ();
1017 dpdk_device_config_t *devconf;
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001018 vlib_pci_addr_t pci_addr;
1019 unformat_input_t sub_input;
Damjan Marion25665672016-08-16 18:56:08 +02001020 u8 *s, *tmp = 0;
1021 u8 *rte_cmd = 0, *ethname = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022 u32 log_level;
1023 int ret, i;
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001024 int num_whitelisted = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 u8 no_pci = 0;
1026 u8 no_huge = 0;
1027 u8 huge_dir = 0;
1028 u8 file_prefix = 0;
Damjan Marion25665672016-08-16 18:56:08 +02001029 u8 *socket_mem = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001031 conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
1032
Damjan Marion25665672016-08-16 18:56:08 +02001033 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034 {
1035 /* Prime the pump */
1036 if (unformat (input, "no-hugetlb"))
Damjan Marion25665672016-08-16 18:56:08 +02001037 {
1038 vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
1039 no_huge = 1;
1040 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041
Dave Barachd81566f2016-02-15 11:34:13 -05001042 else if (unformat (input, "enable-tcp-udp-checksum"))
Damjan Marion64ae6692016-05-25 18:40:13 +02001043 conf->enable_tcp_udp_checksum = 1;
Dave Barachd81566f2016-02-15 11:34:13 -05001044
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045 else if (unformat (input, "decimal-interface-names"))
Damjan Marion25665672016-08-16 18:56:08 +02001046 conf->interface_name_format_decimal = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047
1048 else if (unformat (input, "no-multi-seg"))
Damjan Marion25665672016-08-16 18:56:08 +02001049 conf->no_multi_seg = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001051 else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1052 &sub_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053 {
Damjan Marion25665672016-08-16 18:56:08 +02001054 error =
1055 dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
1056 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001058 if (error)
1059 return error;
1060 }
Damjan Marion25665672016-08-16 18:56:08 +02001061 else
1062 if (unformat
1063 (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1064 unformat_vlib_cli_sub_input, &sub_input))
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001065 {
1066 error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1067
1068 if (error)
1069 return error;
1070
1071 num_whitelisted++;
1072 }
1073 else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1074 {
1075 error = dpdk_device_config (conf, pci_addr, 0, 0);
1076
1077 if (error)
1078 return error;
1079
1080 num_whitelisted++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081 }
Damjan Marion64ae6692016-05-25 18:40:13 +02001082 else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
Damjan Marion25665672016-08-16 18:56:08 +02001083 ;
Damjan Marion64ae6692016-05-25 18:40:13 +02001084 else if (unformat (input, "kni %d", &conf->num_kni))
Damjan Marion25665672016-08-16 18:56:08 +02001085 ;
Damjan Marion64ae6692016-05-25 18:40:13 +02001086 else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087 ;
Damjan Marion7a2a3782016-04-15 20:24:55 +02001088 else if (unformat (input, "socket-mem %s", &socket_mem))
1089 ;
Damjan Marionb022cd12016-08-16 13:25:59 +02001090 else if (unformat (input, "no-pci"))
1091 {
1092 no_pci = 1;
1093 tmp = format (0, "--no-pci%c", 0);
1094 vec_add1 (conf->eal_init_args, tmp);
1095 }
Dave Barach08602d12016-06-04 14:10:59 -04001096 else if (unformat (input, "poll-sleep %d", &dm->poll_sleep))
Damjan Marion25665672016-08-16 18:56:08 +02001097 ;
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001098
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099#define _(a) \
1100 else if (unformat(input, #a)) \
1101 { \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 tmp = format (0, "--%s%c", #a, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001103 vec_add1 (conf->eal_init_args, tmp); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104 }
1105 foreach_eal_double_hyphen_predicate_arg
1106#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107#define _(a) \
1108 else if (unformat(input, #a " %s", &s)) \
1109 { \
1110 if (!strncmp(#a, "huge-dir", 8)) \
1111 huge_dir = 1; \
1112 else if (!strncmp(#a, "file-prefix", 11)) \
1113 file_prefix = 1; \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114 tmp = format (0, "--%s%c", #a, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001115 vec_add1 (conf->eal_init_args, tmp); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 vec_add1 (s, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001117 vec_add1 (conf->eal_init_args, s); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 }
1119 foreach_eal_double_hyphen_arg
1120#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121#define _(a,b) \
1122 else if (unformat(input, #a " %s", &s)) \
1123 { \
1124 tmp = format (0, "-%s%c", #b, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001125 vec_add1 (conf->eal_init_args, tmp); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 vec_add1 (s, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001127 vec_add1 (conf->eal_init_args, s); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128 }
Damjan Marion25665672016-08-16 18:56:08 +02001129 foreach_eal_single_hyphen_arg
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131#define _(a,b) \
1132 else if (unformat(input, #a " %s", &s)) \
1133 { \
1134 tmp = format (0, "-%s%c", #b, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001135 vec_add1 (conf->eal_init_args, tmp); \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136 vec_add1 (s, 0); \
Damjan Marion64ae6692016-05-25 18:40:13 +02001137 vec_add1 (conf->eal_init_args, s); \
1138 conf->a##_set_manually = 1; \
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139 }
Damjan Marion25665672016-08-16 18:56:08 +02001140 foreach_eal_single_hyphen_mandatory_arg
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141#undef _
Damjan Marion25665672016-08-16 18:56:08 +02001142 else if (unformat (input, "default"))
1143 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144
Damjan Marion4c20e712016-10-05 09:50:38 +02001145 else if (unformat_skip_white_space (input))
Damjan Marion61448022016-09-13 10:47:35 +02001146 ;
Damjan Marion25665672016-08-16 18:56:08 +02001147 else
1148 {
1149 error = clib_error_return (0, "unknown input `%U'",
1150 format_unformat_error, input);
1151 goto done;
1152 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153 }
1154
Damjan Marion64ae6692016-05-25 18:40:13 +02001155 if (!conf->uio_driver_name)
1156 conf->uio_driver_name = format (0, "igb_uio%c", 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157
1158 /*
1159 * Use 1G huge pages if available.
1160 */
1161 if (!no_huge && !huge_dir)
1162 {
Damjan Marion25665672016-08-16 18:56:08 +02001163 u32 x, *mem_by_socket = 0;
Damjan Marion7a2a3782016-04-15 20:24:55 +02001164 uword c = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 u8 use_1g = 1;
1166 u8 use_2m = 1;
Damjan Marion7a2a3782016-04-15 20:24:55 +02001167 u8 less_than_1g = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168 int rv;
1169
Damjan Marion25665672016-08-16 18:56:08 +02001170 umount (DEFAULT_HUGE_DIR);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171
1172 /* Process "socket-mem" parameter value */
1173 if (vec_len (socket_mem))
Damjan Marion7a2a3782016-04-15 20:24:55 +02001174 {
1175 unformat_input_t in;
Damjan Marion25665672016-08-16 18:56:08 +02001176 unformat_init_vector (&in, socket_mem);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001177 while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1178 {
1179 if (unformat (&in, "%u,", &x))
1180 ;
1181 else if (unformat (&in, "%u", &x))
1182 ;
1183 else if (unformat (&in, ","))
1184 x = 0;
1185 else
1186 break;
1187
Damjan Marion25665672016-08-16 18:56:08 +02001188 vec_add1 (mem_by_socket, x);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001189
1190 if (x > 1023)
1191 less_than_1g = 0;
1192 }
Damjan Marion25665672016-08-16 18:56:08 +02001193 /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1194 unformat_free (&in);
1195 socket_mem = 0;
Damjan Marion7a2a3782016-04-15 20:24:55 +02001196 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197 else
Damjan Marion7a2a3782016-04-15 20:24:55 +02001198 {
Damjan Marion25665672016-08-16 18:56:08 +02001199 /* *INDENT-OFF* */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001200 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1201 {
1202 vec_validate(mem_by_socket, c);
Damjan Mariona06dfb32016-06-26 20:16:57 +02001203 mem_by_socket[c] = 256; /* default per-socket mem */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001204 }
1205 ));
Damjan Marion25665672016-08-16 18:56:08 +02001206 /* *INDENT-ON* */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001207 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
1209 /* check if available enough 1GB pages for each socket */
Damjan Marion25665672016-08-16 18:56:08 +02001210 /* *INDENT-OFF* */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001211 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1212 {
Damjan Marion20f64412016-09-27 17:51:13 +02001213 int pages_avail, page_size, mem;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214
Damjan Marion7a2a3782016-04-15 20:24:55 +02001215 vec_validate(mem_by_socket, c);
1216 mem = mem_by_socket[c];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001217
Damjan Marion7a2a3782016-04-15 20:24:55 +02001218 page_size = 1024;
Damjan Marion20f64412016-09-27 17:51:13 +02001219 pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001220
Damjan Marion20f64412016-09-27 17:51:13 +02001221 if (pages_avail < 0 || page_size * pages_avail < mem)
Damjan Marion7a2a3782016-04-15 20:24:55 +02001222 use_1g = 0;
1223
1224 page_size = 2;
Damjan Marion20f64412016-09-27 17:51:13 +02001225 pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001226
Damjan Marion20f64412016-09-27 17:51:13 +02001227 if (pages_avail < 0 || page_size * pages_avail < mem)
Damjan Marion7a2a3782016-04-15 20:24:55 +02001228 use_2m = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229 }));
Damjan Marion25665672016-08-16 18:56:08 +02001230 /* *INDENT-ON* */
Dave Barachdd522cb2016-08-10 16:56:16 -04001231
1232 if (mem_by_socket == 0)
Damjan Marion25665672016-08-16 18:56:08 +02001233 {
1234 error = clib_error_return (0, "mem_by_socket NULL");
1235 goto done;
1236 }
Damjan Marion7a2a3782016-04-15 20:24:55 +02001237 _vec_len (mem_by_socket) = c + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238
Damjan Marion7a2a3782016-04-15 20:24:55 +02001239 /* regenerate socket_mem string */
Damjan Marion7a2a3782016-04-15 20:24:55 +02001240 vec_foreach_index (x, mem_by_socket)
1241 socket_mem = format (socket_mem, "%s%u",
Damjan Marion25665672016-08-16 18:56:08 +02001242 socket_mem ? "," : "", mem_by_socket[x]);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001243 socket_mem = format (socket_mem, "%c", 0);
1244
1245 vec_free (mem_by_socket);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246
Damjan Marion25665672016-08-16 18:56:08 +02001247 rv = mkdir (VPP_RUN_DIR, 0755);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248 if (rv && errno != EEXIST)
Damjan Marion25665672016-08-16 18:56:08 +02001249 {
1250 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1251 VPP_RUN_DIR, errno);
1252 goto done;
1253 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001254
Damjan Marion25665672016-08-16 18:56:08 +02001255 rv = mkdir (DEFAULT_HUGE_DIR, 0755);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256 if (rv && errno != EEXIST)
Damjan Marion25665672016-08-16 18:56:08 +02001257 {
1258 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1259 DEFAULT_HUGE_DIR, errno);
1260 goto done;
1261 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262
Damjan Marion7a2a3782016-04-15 20:24:55 +02001263 if (use_1g && !(less_than_1g && use_2m))
Damjan Marion25665672016-08-16 18:56:08 +02001264 {
1265 rv =
1266 mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1267 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001268 else if (use_2m)
Damjan Marion25665672016-08-16 18:56:08 +02001269 {
1270 rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1271 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001272 else
Damjan Marion25665672016-08-16 18:56:08 +02001273 {
1274 return clib_error_return (0, "not enough free huge pages");
1275 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276
1277 if (rv)
Damjan Marion25665672016-08-16 18:56:08 +02001278 {
1279 error = clib_error_return (0, "mount failed %d", errno);
1280 goto done;
1281 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282
1283 tmp = format (0, "--huge-dir%c", 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001284 vec_add1 (conf->eal_init_args, tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285 tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001286 vec_add1 (conf->eal_init_args, tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287 if (!file_prefix)
Damjan Marion25665672016-08-16 18:56:08 +02001288 {
1289 tmp = format (0, "--file-prefix%c", 0);
1290 vec_add1 (conf->eal_init_args, tmp);
1291 tmp = format (0, "vpp%c", 0);
1292 vec_add1 (conf->eal_init_args, tmp);
1293 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294 }
1295
Ed Warnickecb9cada2015-12-08 15:45:58 -07001296 vec_free (rte_cmd);
1297 vec_free (ethname);
1298
1299 if (error)
1300 return error;
1301
1302 /* I'll bet that -c and -n must be the first and second args... */
Damjan Marion64ae6692016-05-25 18:40:13 +02001303 if (!conf->coremask_set_manually)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304 {
Damjan Marion25665672016-08-16 18:56:08 +02001305 vlib_thread_registration_t *tr;
1306 uword *coremask = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001307 int i;
1308
1309 /* main thread core */
Damjan Marion25665672016-08-16 18:56:08 +02001310 coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001311
1312 for (i = 0; i < vec_len (tm->registrations); i++)
Damjan Marion25665672016-08-16 18:56:08 +02001313 {
1314 tr = tm->registrations[i];
1315 coremask = clib_bitmap_or (coremask, tr->coremask);
1316 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001317
Damjan Marion64ae6692016-05-25 18:40:13 +02001318 vec_insert (conf->eal_init_args, 2, 1);
1319 conf->eal_init_args[1] = (u8 *) "-c";
Damjan Marion14a44d32016-02-05 23:33:21 +01001320 tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001321 conf->eal_init_args[2] = tmp;
Damjan Marion25665672016-08-16 18:56:08 +02001322 clib_bitmap_free (coremask);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323 }
1324
Damjan Marion64ae6692016-05-25 18:40:13 +02001325 if (!conf->nchannels_set_manually)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001326 {
Damjan Marion64ae6692016-05-25 18:40:13 +02001327 vec_insert (conf->eal_init_args, 2, 3);
1328 conf->eal_init_args[3] = (u8 *) "-n";
1329 tmp = format (0, "%d", conf->nchannels);
1330 conf->eal_init_args[4] = tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001331 }
1332
Damjan Marion25665672016-08-16 18:56:08 +02001333 if (no_pci == 0 && geteuid () == 0)
1334 dpdk_bind_devices_to_uio (conf);
Damjan Mariona42cd342016-04-13 18:03:20 +02001335
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001336#define _(x) \
1337 if (devconf->x == 0 && conf->default_devconf.x > 0) \
1338 devconf->x = conf->default_devconf.x ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001339
Damjan Marion25665672016-08-16 18:56:08 +02001340 /* *INDENT-OFF* */
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001341 pool_foreach (devconf, conf->dev_confs, ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07001342
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001343 /* default per-device config items */
1344 foreach_dpdk_device_config_item
1345
1346 /* add DPDK EAL whitelist/blacklist entry */
1347 if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1348 {
1349 tmp = format (0, "-w%c", 0);
1350 vec_add1 (conf->eal_init_args, tmp);
Damjan Marionc883fa72016-06-07 21:49:56 +02001351 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001352 vec_add1 (conf->eal_init_args, tmp);
1353 }
1354 else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1355 {
1356 tmp = format (0, "-b%c", 0);
1357 vec_add1 (conf->eal_init_args, tmp);
Damjan Marionc883fa72016-06-07 21:49:56 +02001358 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001359 vec_add1 (conf->eal_init_args, tmp);
1360 }
1361 }));
Damjan Marion25665672016-08-16 18:56:08 +02001362 /* *INDENT-ON* */
Damjan Marionc68b4cb2016-05-25 20:11:33 +02001363
1364#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366 /* set master-lcore */
1367 tmp = format (0, "--master-lcore%c", 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001368 vec_add1 (conf->eal_init_args, tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001369 tmp = format (0, "%u%c", tm->main_lcore, 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001370 vec_add1 (conf->eal_init_args, tmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371
Damjan Marion7a2a3782016-04-15 20:24:55 +02001372 /* set socket-mem */
1373 tmp = format (0, "--socket-mem%c", 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001374 vec_add1 (conf->eal_init_args, tmp);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001375 tmp = format (0, "%s%c", socket_mem, 0);
Damjan Marion64ae6692016-05-25 18:40:13 +02001376 vec_add1 (conf->eal_init_args, tmp);
Damjan Marion7a2a3782016-04-15 20:24:55 +02001377
Ed Warnickecb9cada2015-12-08 15:45:58 -07001378 /* NULL terminate the "argv" vector, in case of stupidity */
Damjan Marion64ae6692016-05-25 18:40:13 +02001379 vec_add1 (conf->eal_init_args, 0);
Damjan Marion25665672016-08-16 18:56:08 +02001380 _vec_len (conf->eal_init_args) -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001381
1382 /* Set up DPDK eal and packet mbuf pool early. */
1383
1384 log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1385
1386 rte_set_log_level (log_level);
1387
Damjan Marion64ae6692016-05-25 18:40:13 +02001388 vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001389
Damjan Mariond8ff0e12016-02-16 12:29:57 +01001390 /* make copy of args as rte_eal_init tends to mess up with arg array */
Damjan Marion25665672016-08-16 18:56:08 +02001391 for (i = 1; i < vec_len (conf->eal_init_args); i++)
1392 conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1393 conf->eal_init_args[i]);
Damjan Mariond8ff0e12016-02-16 12:29:57 +01001394
Damjan Marion25665672016-08-16 18:56:08 +02001395 ret =
1396 rte_eal_init (vec_len (conf->eal_init_args),
1397 (char **) conf->eal_init_args);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001398
1399 /* lazy umount hugepages */
Damjan Marion25665672016-08-16 18:56:08 +02001400 umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401
1402 if (ret < 0)
1403 return clib_error_return (0, "rte_eal_init returned %d", ret);
1404
Sean Hope98efd022016-02-22 15:21:31 -05001405 /* Dump the physical memory layout prior to creating the mbuf_pool */
Damjan Marion25665672016-08-16 18:56:08 +02001406 fprintf (stdout, "DPDK physical memory layout:\n");
1407 rte_dump_physmem_layout (stdout);
Sean Hope98efd022016-02-22 15:21:31 -05001408
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409 /* main thread 1st */
Damjan Marion25665672016-08-16 18:56:08 +02001410 error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001411 if (error)
1412 return error;
1413
1414 for (i = 0; i < RTE_MAX_LCORE; i++)
1415 {
Damjan Marion25665672016-08-16 18:56:08 +02001416 error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1417 rte_lcore_to_socket_id (i));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001418 if (error)
Damjan Marion25665672016-08-16 18:56:08 +02001419 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001420 }
1421
Damjan Marion25665672016-08-16 18:56:08 +02001422done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001423 return error;
1424}
1425
1426VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1427
Damjan Marion25665672016-08-16 18:56:08 +02001428void
1429dpdk_update_link_state (dpdk_device_t * xd, f64 now)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001430{
Damjan Marion25665672016-08-16 18:56:08 +02001431 vnet_main_t *vnm = vnet_get_main ();
1432 struct rte_eth_link prev_link = xd->link;
1433 u32 hw_flags = 0;
1434 u8 hw_flags_chg = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001435
Damjan Marion25665672016-08-16 18:56:08 +02001436 /* only update link state for PMD interfaces */
Damjan Marion56431702016-09-19 13:18:09 +02001437 if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
Damjan Marion25665672016-08-16 18:56:08 +02001438 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439
Damjan Marion25665672016-08-16 18:56:08 +02001440 xd->time_last_link_update = now ? now : xd->time_last_link_update;
1441 memset (&xd->link, 0, sizeof (xd->link));
1442 rte_eth_link_get_nowait (xd->device_index, &xd->link);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001443
Damjan Marion25665672016-08-16 18:56:08 +02001444 if (LINK_STATE_ELOGS)
1445 {
1446 vlib_main_t *vm = vlib_get_main ();
1447 ELOG_TYPE_DECLARE (e) =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448 {
Damjan Marion25665672016-08-16 18:56:08 +02001449 .format =
1450 "update-link-state: sw_if_index %d, admin_up %d,"
1451 "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452
Damjan Marion25665672016-08-16 18:56:08 +02001453 struct
Ed Warnickecb9cada2015-12-08 15:45:58 -07001454 {
Damjan Marion25665672016-08-16 18:56:08 +02001455 u32 sw_if_index;
1456 u8 admin_up;
1457 u8 old_link_state;
1458 u8 new_link_state;
1459 } *ed;
1460 ed = ELOG_DATA (&vm->elog_main, e);
1461 ed->sw_if_index = xd->vlib_sw_if_index;
Damjan Marionb28e4982016-08-22 22:34:38 +02001462 ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
Damjan Marion25665672016-08-16 18:56:08 +02001463 ed->old_link_state = (u8)
1464 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1465 ed->new_link_state = (u8) xd->link.link_status;
1466 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001467
Damjan Marionb28e4982016-08-22 22:34:38 +02001468 if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
Damjan Marion25665672016-08-16 18:56:08 +02001469 ((xd->link.link_status != 0) ^
1470 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1471 {
1472 hw_flags_chg = 1;
1473 hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1474 }
1475
1476 if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1477 {
1478 hw_flags_chg = 1;
1479 switch (xd->link.link_duplex)
1480 {
1481 case ETH_LINK_HALF_DUPLEX:
1482 hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1483 break;
1484 case ETH_LINK_FULL_DUPLEX:
1485 hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1486 break;
1487 default:
1488 break;
1489 }
1490 }
Damjan Marion25665672016-08-16 18:56:08 +02001491 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1492 {
1493 hw_flags_chg = 1;
1494 switch (xd->link.link_speed)
1495 {
1496 case ETH_SPEED_NUM_10M:
1497 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1498 break;
1499 case ETH_SPEED_NUM_100M:
1500 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1501 break;
1502 case ETH_SPEED_NUM_1G:
1503 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1504 break;
1505 case ETH_SPEED_NUM_10G:
1506 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1507 break;
1508 case ETH_SPEED_NUM_40G:
1509 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1510 break;
1511 case 0:
1512 break;
1513 default:
1514 clib_warning ("unknown link speed %d", xd->link.link_speed);
1515 break;
1516 }
1517 }
Damjan Marion25665672016-08-16 18:56:08 +02001518 if (hw_flags_chg)
1519 {
1520 if (LINK_STATE_ELOGS)
1521 {
1522 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001523
Damjan Marion25665672016-08-16 18:56:08 +02001524 ELOG_TYPE_DECLARE (e) =
1525 {
1526 .format =
1527 "update-link-state: sw_if_index %d, new flags %d",.format_args
1528 = "i4i4",};
Ed Warnickecb9cada2015-12-08 15:45:58 -07001529
Damjan Marion25665672016-08-16 18:56:08 +02001530 struct
1531 {
1532 u32 sw_if_index;
1533 u32 flags;
1534 } *ed;
1535 ed = ELOG_DATA (&vm->elog_main, e);
1536 ed->sw_if_index = xd->vlib_sw_if_index;
1537 ed->flags = hw_flags;
1538 }
1539 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1540 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001541}
1542
1543static uword
Damjan Marion25665672016-08-16 18:56:08 +02001544dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001545{
Damjan Marion25665672016-08-16 18:56:08 +02001546 clib_error_t *error;
1547 vnet_main_t *vnm = vnet_get_main ();
1548 dpdk_main_t *dm = &dpdk_main;
1549 ethernet_main_t *em = &ethernet_main;
1550 dpdk_device_t *xd;
1551 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001552 int i;
1553
1554 error = dpdk_lib_init (dm);
1555
Damjan Marion25665672016-08-16 18:56:08 +02001556 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001557 * Turn on the input node if we found some devices to drive
1558 * and we're not running worker threads or i/o threads
1559 */
1560
Damjan Marion25665672016-08-16 18:56:08 +02001561 if (error == 0 && vec_len (dm->devices) > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001562 {
Damjan Marion25665672016-08-16 18:56:08 +02001563 if (tm->n_vlib_mains == 1)
1564 vlib_node_set_state (vm, dpdk_input_node.index,
1565 VLIB_NODE_STATE_POLLING);
1566 else
1567 for (i = 0; i < tm->n_vlib_mains; i++)
1568 if (vec_len (dm->devices_by_cpu[i]) > 0)
1569 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1570 VLIB_NODE_STATE_POLLING);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001571 }
1572
1573 if (error)
1574 clib_error_report (error);
1575
Damjan Marion0f8ecf02016-06-27 08:30:30 +02001576 tm->worker_thread_release = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001577
1578 f64 now = vlib_time_now (vm);
1579 vec_foreach (xd, dm->devices)
Damjan Marion25665672016-08-16 18:56:08 +02001580 {
1581 dpdk_update_link_state (xd, now);
1582 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001583
Damjan Marion25665672016-08-16 18:56:08 +02001584 {
1585 /*
1586 * Extra set up for bond interfaces:
1587 * 1. Setup MACs for bond interfaces and their slave links which was set
1588 * in dpdk_port_setup() but needs to be done again here to take effect.
1589 * 2. Set up info for bond interface related CLI support.
1590 */
1591 int nports = rte_eth_dev_count ();
1592 if (nports > 0)
1593 {
1594 for (i = 0; i < nports; i++)
1595 {
1596 struct rte_eth_dev_info dev_info;
1597 rte_eth_dev_info_get (i, &dev_info);
1598 if (!dev_info.driver_name)
Damjan Marion39162272016-10-25 14:54:38 +02001599#if RTE_VERSION < RTE_VERSION_NUM(16, 11, 0, 0)
John Lod9bf9ab2016-02-25 11:17:55 -05001600 dev_info.driver_name = dev_info.pci_dev->driver->name;
Damjan Marion39162272016-10-25 14:54:38 +02001601#else
1602 dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
1603#endif
Damjan Marion25665672016-08-16 18:56:08 +02001604 ASSERT (dev_info.driver_name);
1605 if (strncmp (dev_info.driver_name, "rte_bond_pmd", 12) == 0)
1606 {
1607 u8 addr[6];
1608 u8 slink[16];
1609 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1610 if (nlink > 0)
1611 {
1612 vnet_hw_interface_t *bhi;
1613 ethernet_interface_t *bei;
1614 int rv;
Damjan Marionb022cd12016-08-16 13:25:59 +02001615
Damjan Marion25665672016-08-16 18:56:08 +02001616 /* Get MAC of 1st slave link */
1617 rte_eth_macaddr_get (slink[0],
1618 (struct ether_addr *) addr);
1619 /* Set MAC of bounded interface to that of 1st slave link */
1620 rv =
1621 rte_eth_bond_mac_address_set (i,
1622 (struct ether_addr *)
1623 addr);
1624 if (rv < 0)
1625 clib_warning ("Failed to set MAC address");
Damjan Marionb022cd12016-08-16 13:25:59 +02001626
Damjan Marion25665672016-08-16 18:56:08 +02001627 /* Populate MAC of bonded interface in VPP hw tables */
1628 bhi =
1629 vnet_get_hw_interface (vnm,
1630 dm->devices[i].vlib_hw_if_index);
1631 bei =
1632 pool_elt_at_index (em->interfaces, bhi->hw_instance);
1633 clib_memcpy (bhi->hw_address, addr, 6);
1634 clib_memcpy (bei->address, addr, 6);
1635 /* Init l3 packet size allowed on bonded interface */
Steve Shinbf1fa7f2016-09-07 08:25:04 -07001636 bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
Damjan Marion25665672016-08-16 18:56:08 +02001637 bhi->max_l3_packet_bytes[VLIB_RX] =
1638 bhi->max_l3_packet_bytes[VLIB_TX] =
1639 ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1640 while (nlink >= 1)
1641 { /* for all slave links */
1642 int slave = slink[--nlink];
1643 dpdk_device_t *sdev = &dm->devices[slave];
1644 vnet_hw_interface_t *shi;
1645 vnet_sw_interface_t *ssi;
1646 /* Add MAC to all slave links except the first one */
1647 if (nlink)
1648 rte_eth_dev_mac_addr_add (slave,
1649 (struct ether_addr *)
1650 addr, 0);
1651 /* Set slaves bitmap for bonded interface */
1652 bhi->bond_info =
1653 clib_bitmap_set (bhi->bond_info,
1654 sdev->vlib_hw_if_index, 1);
1655 /* Set slave link flags on slave interface */
1656 shi =
1657 vnet_get_hw_interface (vnm, sdev->vlib_hw_if_index);
1658 ssi =
1659 vnet_get_sw_interface (vnm, sdev->vlib_sw_if_index);
1660 shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1661 ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
Steve Shinbf1fa7f2016-09-07 08:25:04 -07001662
1663 /* Set l3 packet size allowed as the lowest of slave */
1664 if (bhi->max_l3_packet_bytes[VLIB_RX] >
1665 shi->max_l3_packet_bytes[VLIB_RX])
1666 bhi->max_l3_packet_bytes[VLIB_RX] =
1667 bhi->max_l3_packet_bytes[VLIB_TX] =
1668 shi->max_l3_packet_bytes[VLIB_RX];
1669
1670 /* Set max packet size allowed as the lowest of slave */
1671 if (bhi->max_packet_bytes > shi->max_packet_bytes)
1672 bhi->max_packet_bytes = shi->max_packet_bytes;
Damjan Marion25665672016-08-16 18:56:08 +02001673 }
John Lod9bf9ab2016-02-25 11:17:55 -05001674 }
1675 }
1676 }
1677 }
1678 }
John Lod9bf9ab2016-02-25 11:17:55 -05001679
Ed Warnickecb9cada2015-12-08 15:45:58 -07001680 while (1)
1681 {
Bud Grise02301ef2016-02-24 16:09:05 -05001682 /*
1683 * check each time through the loop in case intervals are changed
1684 */
1685 f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
Damjan Marion25665672016-08-16 18:56:08 +02001686 dm->link_state_poll_interval : dm->stat_poll_interval;
Bud Grise02301ef2016-02-24 16:09:05 -05001687
1688 vlib_process_wait_for_event_or_clock (vm, min_wait);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001689
Damjan Marion4ee3a652016-11-15 19:47:58 +01001690 if (dm->admin_up_down_in_progress)
Damjan Marion25665672016-08-16 18:56:08 +02001691 /* skip the poll if an admin up down is in progress (on any interface) */
1692 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001693
1694 vec_foreach (xd, dm->devices)
Damjan Marion25665672016-08-16 18:56:08 +02001695 {
1696 f64 now = vlib_time_now (vm);
1697 if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1698 dpdk_update_counters (xd, now);
1699 if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1700 dpdk_update_link_state (xd, now);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001701
Damjan Marion25665672016-08-16 18:56:08 +02001702 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001703 }
1704
Damjan Marion25665672016-08-16 18:56:08 +02001705 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001706}
1707
Damjan Marion25665672016-08-16 18:56:08 +02001708/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001709VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1710 .function = dpdk_process,
1711 .type = VLIB_NODE_TYPE_PROCESS,
1712 .name = "dpdk-process",
1713 .process_log2_n_stack_bytes = 17,
1714};
Damjan Marion25665672016-08-16 18:56:08 +02001715/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001716
Damjan Marion25665672016-08-16 18:56:08 +02001717int
1718dpdk_set_stat_poll_interval (f64 interval)
Bud Grise02301ef2016-02-24 16:09:05 -05001719{
1720 if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
Damjan Marion25665672016-08-16 18:56:08 +02001721 return (VNET_API_ERROR_INVALID_VALUE);
Bud Grise02301ef2016-02-24 16:09:05 -05001722
1723 dpdk_main.stat_poll_interval = interval;
1724
1725 return 0;
1726}
1727
Damjan Marion25665672016-08-16 18:56:08 +02001728int
1729dpdk_set_link_state_poll_interval (f64 interval)
Bud Grise02301ef2016-02-24 16:09:05 -05001730{
1731 if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
Damjan Marion25665672016-08-16 18:56:08 +02001732 return (VNET_API_ERROR_INVALID_VALUE);
Bud Grise02301ef2016-02-24 16:09:05 -05001733
1734 dpdk_main.link_state_poll_interval = interval;
1735
1736 return 0;
1737}
1738
Ed Warnickecb9cada2015-12-08 15:45:58 -07001739clib_error_t *
1740dpdk_init (vlib_main_t * vm)
1741{
Damjan Marion25665672016-08-16 18:56:08 +02001742 dpdk_main_t *dm = &dpdk_main;
1743 vlib_node_t *ei;
1744 clib_error_t *error = 0;
1745 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001746
1747 /* verify that structs are cacheline aligned */
Damjan Marion3b906b02016-11-01 15:16:30 +01001748 STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1749 "Cache line marker must be 1st element in dpdk_device_t");
1750 STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1751 CLIB_CACHE_LINE_BYTES,
1752 "Data in cache line 0 is bigger than cache line size");
1753 STATIC_ASSERT (offsetof (dpdk_worker_t, cacheline0) == 0,
1754 "Cache line marker must be 1st element in dpdk_worker_t");
1755 STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1756 "Cache line marker must be 1st element in frame_queue_trace_t");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001757
Ed Warnickecb9cada2015-12-08 15:45:58 -07001758 dm->vlib_main = vm;
Damjan Marion25665672016-08-16 18:56:08 +02001759 dm->vnet_main = vnet_get_main ();
Damjan Marion64ae6692016-05-25 18:40:13 +02001760 dm->conf = &dpdk_config_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001761
1762 ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1763 if (ei == 0)
Damjan Marion25665672016-08-16 18:56:08 +02001764 return clib_error_return (0, "ethernet-input node AWOL");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765
1766 dm->ethernet_input_node_index = ei->index;
1767
Damjan Marion64ae6692016-05-25 18:40:13 +02001768 dm->conf->nchannels = 4;
1769 dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1770 vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001771
1772 dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1773 dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1774 dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1775
1776 /* $$$ use n_thread_stacks since it's known-good at this point */
1777 vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1778
Dave Barachd81566f2016-02-15 11:34:13 -05001779 /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
Damjan Marion25665672016-08-16 18:56:08 +02001780 dm->buffer_flags_template =
Damjan Marion22766b82016-11-15 12:50:28 +01001781 (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_RTE_MBUF_VALID
Damjan Marion25665672016-08-16 18:56:08 +02001782 | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
Dave Barachd81566f2016-02-15 11:34:13 -05001783
Bud Grise02301ef2016-02-24 16:09:05 -05001784 dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1785 dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1786
Ed Warnickecb9cada2015-12-08 15:45:58 -07001787 /* init CLI */
1788 if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1789 return error;
1790
1791 return error;
1792}
1793
1794VLIB_INIT_FUNCTION (dpdk_init);
1795
Damjan Marion25665672016-08-16 18:56:08 +02001796
1797/*
1798 * fd.io coding-style-patch-verification: ON
1799 *
1800 * Local Variables:
1801 * eval: (c-set-style "gnu")
1802 * End:
1803 */