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:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/error.h>
18 #include <vppinfra/format.h>
19 #include <vppinfra/bitmap.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <dpdk/device/dpdk.h>
23 #include <vlib/unix/physmem.h>
24 #include <vlib/pci/pci.h>
30 #include <sys/mount.h>
34 #include <dpdk/device/dpdk_priv.h>
36 dpdk_main_t dpdk_main;
38 #include <vlibapi/api.h>
39 #include <vlibmemory/api.h>
41 /* define message IDs */
42 #include <dpdk/api/dpdk_msg_enum.h>
44 #define vl_typedefs /* define message structures */
45 #include <dpdk/api/dpdk_all_api_h.h>
48 #define vl_endianfun /* define message structures */
49 #include <dpdk/api/dpdk_all_api_h.h>
52 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
54 /* Get the API version number. */
55 #define vl_api_version(n,v) static u32 api_version=(v);
56 #include <dpdk/api/dpdk_all_api_h.h>
59 /* Macro to finish up custom dump fns */
62 vl_print (handle, (char *)s); \
66 #include <vlibapi/api_helper_macros.h>
69 vl_api_sw_interface_set_dpdk_hqos_pipe_t_handler
70 (vl_api_sw_interface_set_dpdk_hqos_pipe_t * mp)
72 vl_api_sw_interface_set_dpdk_hqos_pipe_reply_t *rmp;
75 dpdk_main_t *dm = &dpdk_main;
78 u32 sw_if_index = ntohl (mp->sw_if_index);
79 u32 subport = ntohl (mp->subport);
80 u32 pipe = ntohl (mp->pipe);
81 u32 profile = ntohl (mp->profile);
82 vnet_hw_interface_t *hw;
84 VALIDATE_SW_IF_INDEX (mp);
86 /* hw_if & dpdk device */
87 hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
89 xd = vec_elt_at_index (dm->devices, hw->dev_instance);
91 rv = rte_sched_pipe_config (xd->hqos_ht->hqos, subport, pipe, profile);
93 BAD_SW_IF_INDEX_LABEL;
95 REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_PIPE_REPLY);
98 static void *vl_api_sw_interface_set_dpdk_hqos_pipe_t_print
99 (vl_api_sw_interface_set_dpdk_hqos_pipe_t * mp, void *handle)
103 s = format (0, "SCRIPT: sw_interface_set_dpdk_hqos_pipe ");
105 s = format (s, "sw_if_index %u ", ntohl (mp->sw_if_index));
107 s = format (s, "subport %u pipe %u profile %u ",
108 ntohl (mp->subport), ntohl (mp->pipe), ntohl (mp->profile));
114 vl_api_sw_interface_set_dpdk_hqos_subport_t_handler
115 (vl_api_sw_interface_set_dpdk_hqos_subport_t * mp)
117 vl_api_sw_interface_set_dpdk_hqos_subport_reply_t *rmp;
120 dpdk_main_t *dm = &dpdk_main;
122 struct rte_sched_subport_params p;
124 u32 sw_if_index = ntohl (mp->sw_if_index);
125 u32 subport = ntohl (mp->subport);
126 p.tb_rate = ntohl (mp->tb_rate);
127 p.tb_size = ntohl (mp->tb_size);
128 p.tc_rate[0] = ntohl (mp->tc_rate[0]);
129 p.tc_rate[1] = ntohl (mp->tc_rate[1]);
130 p.tc_rate[2] = ntohl (mp->tc_rate[2]);
131 p.tc_rate[3] = ntohl (mp->tc_rate[3]);
132 p.tc_period = ntohl (mp->tc_period);
134 vnet_hw_interface_t *hw;
136 VALIDATE_SW_IF_INDEX (mp);
138 /* hw_if & dpdk device */
139 hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
141 xd = vec_elt_at_index (dm->devices, hw->dev_instance);
143 rv = rte_sched_subport_config (xd->hqos_ht->hqos, subport, &p);
145 BAD_SW_IF_INDEX_LABEL;
147 REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_SUBPORT_REPLY);
150 static void *vl_api_sw_interface_set_dpdk_hqos_subport_t_print
151 (vl_api_sw_interface_set_dpdk_hqos_subport_t * mp, void *handle)
155 s = format (0, "SCRIPT: sw_interface_set_dpdk_hqos_subport ");
157 s = format (s, "sw_if_index %u ", ntohl (mp->sw_if_index));
161 "subport %u rate %u bkt_size %u tc0 %u tc1 %u tc2 %u tc3 %u period %u",
162 ntohl (mp->subport), ntohl (mp->tb_rate), ntohl (mp->tb_size),
163 ntohl (mp->tc_rate[0]), ntohl (mp->tc_rate[1]),
164 ntohl (mp->tc_rate[2]), ntohl (mp->tc_rate[3]),
165 ntohl (mp->tc_period));
171 vl_api_sw_interface_set_dpdk_hqos_tctbl_t_handler
172 (vl_api_sw_interface_set_dpdk_hqos_tctbl_t * mp)
174 vl_api_sw_interface_set_dpdk_hqos_tctbl_reply_t *rmp;
177 dpdk_main_t *dm = &dpdk_main;
178 vlib_thread_main_t *tm = vlib_get_thread_main ();
181 u32 sw_if_index = ntohl (mp->sw_if_index);
182 u32 entry = ntohl (mp->entry);
183 u32 tc = ntohl (mp->tc);
184 u32 queue = ntohl (mp->queue);
187 vnet_hw_interface_t *hw;
189 VALIDATE_SW_IF_INDEX (mp);
191 /* hw_if & dpdk device */
192 hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
194 xd = vec_elt_at_index (dm->devices, hw->dev_instance);
196 if (tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
198 clib_warning ("invalid traffic class !!");
199 rv = VNET_API_ERROR_INVALID_VALUE;
202 if (queue >= RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
204 clib_warning ("invalid queue !!");
205 rv = VNET_API_ERROR_INVALID_VALUE;
209 /* Detect the set of worker threads */
210 uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
214 clib_warning ("worker thread registration AWOL !!");
215 rv = VNET_API_ERROR_INVALID_VALUE_2;
219 vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
220 int worker_thread_first = tr->first_index;
221 int worker_thread_count = tr->count;
223 val = tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
224 for (i = 0; i < worker_thread_count; i++)
225 xd->hqos_wt[worker_thread_first + i].hqos_tc_table[entry] = val;
227 BAD_SW_IF_INDEX_LABEL;
230 REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_TCTBL_REPLY);
233 static void *vl_api_sw_interface_set_dpdk_hqos_tctbl_t_print
234 (vl_api_sw_interface_set_dpdk_hqos_tctbl_t * mp, void *handle)
238 s = format (0, "SCRIPT: sw_interface_set_dpdk_hqos_tctbl ");
240 s = format (s, "sw_if_index %u ", ntohl (mp->sw_if_index));
242 s = format (s, "entry %u tc %u queue %u",
243 ntohl (mp->entry), ntohl (mp->tc), ntohl (mp->queue));
248 #define foreach_dpdk_plugin_api_msg \
249 _(SW_INTERFACE_SET_DPDK_HQOS_PIPE, sw_interface_set_dpdk_hqos_pipe) \
250 _(SW_INTERFACE_SET_DPDK_HQOS_SUBPORT, sw_interface_set_dpdk_hqos_subport) \
251 _(SW_INTERFACE_SET_DPDK_HQOS_TCTBL, sw_interface_set_dpdk_hqos_tctbl)
253 /* Set up the API message handling tables */
254 static clib_error_t *
255 dpdk_plugin_api_hookup (vlib_main_t * vm)
257 dpdk_main_t *dm __attribute__ ((unused)) = &dpdk_main;
259 vl_msg_api_set_handlers((VL_API_##N + dm->msg_id_base), \
261 vl_api_##n##_t_handler, \
263 vl_api_##n##_t_endian, \
264 vl_api_##n##_t_print, \
265 sizeof(vl_api_##n##_t), 1);
266 foreach_dpdk_plugin_api_msg;
271 #define vl_msg_name_crc_list
272 #include <dpdk/api/dpdk_all_api_h.h>
273 #undef vl_msg_name_crc_list
276 setup_message_id_table (dpdk_main_t * dm, api_main_t * am)
278 #define _(id,n,crc) \
279 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + dm->msg_id_base);
280 foreach_vl_msg_name_crc_dpdk;
286 static void plugin_custom_dump_configure (dpdk_main_t * dm)
288 #define _(n,f) dm->api_main->msg_print_handlers \
289 [VL_API_##n + dm->msg_id_base] \
290 = (void *) vl_api_##f##_t_print;
291 foreach_dpdk_plugin_api_msg;
295 /* force linker to link functions used by vlib and declared weak */
296 void *vlib_weakly_linked_functions[] = {
298 &rte_pktmbuf_pool_init,
301 #define LINK_STATE_ELOGS 0
303 #define DEFAULT_HUGE_DIR "/run/vpp/hugepages"
304 #define VPP_RUN_DIR "/run/vpp"
306 /* Port configuration, mildly modified Intel app values */
308 static struct rte_eth_conf port_conf_template = {
311 .header_split = 0, /**< Header Split disabled */
312 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
313 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
314 .hw_strip_crc = 0, /**< CRC stripped by hardware */
317 .mq_mode = ETH_MQ_TX_NONE,
322 dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd)
327 ASSERT (vlib_get_thread_index () == 0);
329 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
331 vnet_hw_interface_set_flags (dm->vnet_main, xd->hw_if_index, 0);
332 rte_eth_dev_stop (xd->device_index);
335 rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
336 xd->tx_q_used, &xd->port_conf);
339 return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
340 xd->device_index, rv);
342 /* Set up one TX-queue per worker thread */
343 for (j = 0; j < xd->tx_q_used; j++)
345 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
346 xd->cpu_socket, &xd->tx_conf);
348 /* retry with any other CPU socket */
350 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
351 SOCKET_ID_ANY, &xd->tx_conf);
357 return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
358 xd->device_index, rv);
360 for (j = 0; j < xd->rx_q_used; j++)
362 uword tidx = vnet_get_device_input_thread_index (dm->vnet_main,
364 unsigned lcore = vlib_worker_threads[tidx].lcore_id;
365 u16 socket_id = rte_lcore_to_socket_id (lcore);
367 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
369 dm->pktmbuf_pools[socket_id]);
371 /* retry with any other CPU socket */
373 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
375 dm->pktmbuf_pools[socket_id]);
377 return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
378 xd->device_index, rv);
381 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
384 rv = rte_eth_dev_start (xd->device_index);
385 if (!rv && xd->default_mac_address)
386 rv = rte_eth_dev_default_mac_addr_set (xd->device_index,
387 (struct ether_addr *)
388 xd->default_mac_address);
390 clib_warning ("rte_eth_dev_start %d returned %d",
391 xd->device_index, rv);
397 dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
399 dpdk_main_t *dm = &dpdk_main;
400 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
403 if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
405 old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
407 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
408 xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
410 xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
412 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
414 if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
415 rte_eth_promiscuous_enable (xd->device_index);
417 rte_eth_promiscuous_disable (xd->device_index);
420 else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU (flags))
424 xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
426 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
427 rte_eth_dev_stop (xd->device_index);
429 rv = rte_eth_dev_configure
430 (xd->device_index, xd->rx_q_used, xd->tx_q_used, &xd->port_conf);
433 vlib_cli_output (vlib_get_main (),
434 "rte_eth_dev_configure[%d]: err %d",
435 xd->device_index, rv);
437 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
439 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
441 int rv = rte_eth_dev_start (xd->device_index);
442 if (!rv && xd->default_mac_address)
443 rv = rte_eth_dev_default_mac_addr_set (xd->device_index,
444 (struct ether_addr *)
445 xd->default_mac_address);
447 clib_warning ("rte_eth_dev_start %d returned %d",
448 xd->device_index, rv);
456 dpdk_device_lock_init (dpdk_device_t * xd)
459 vec_validate (xd->lockp, xd->tx_q_used - 1);
460 for (q = 0; q < xd->tx_q_used; q++)
462 xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
463 CLIB_CACHE_LINE_BYTES);
464 memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
469 dpdk_device_lock_free (dpdk_device_t * xd)
473 for (q = 0; q < vec_len (xd->lockp); q++)
474 clib_mem_free ((void *) xd->lockp[q]);
475 vec_free (xd->lockp);
479 static clib_error_t *
480 dpdk_lib_init (dpdk_main_t * dm)
486 vlib_main_t *vm = vlib_get_main ();
487 vlib_thread_main_t *tm = vlib_get_thread_main ();
488 vnet_device_main_t *vdm = &vnet_device_main;
489 vnet_sw_interface_t *sw;
490 vnet_hw_interface_t *hi;
492 vlib_pci_addr_t last_pci_addr;
493 u32 last_pci_addr_port = 0;
494 vlib_thread_registration_t *tr_hqos;
497 u32 next_hqos_cpu = 0;
498 u8 af_packet_port_id = 0;
499 u8 bond_ether_port_id = 0;
500 last_pci_addr.as_u32 = ~0;
502 dm->hqos_cpu_first_index = 0;
503 dm->hqos_cpu_count = 0;
505 /* find out which cpus will be used for I/O TX */
506 p_hqos = hash_get_mem (tm->thread_registrations_by_name, "hqos-threads");
507 tr_hqos = p_hqos ? (vlib_thread_registration_t *) p_hqos[0] : 0;
509 if (tr_hqos && tr_hqos->count > 0)
511 dm->hqos_cpu_first_index = tr_hqos->first_index;
512 dm->hqos_cpu_count = tr_hqos->count;
515 vec_validate_aligned (dm->devices_by_hqos_cpu, tm->n_vlib_mains - 1,
516 CLIB_CACHE_LINE_BYTES);
518 nports = rte_eth_dev_count ();
521 clib_warning ("DPDK drivers found no ports...");
525 clib_warning ("DPDK drivers found %d ports...", nports);
528 * All buffers are all allocated from the same rte_mempool.
529 * Thus they all have the same number of data bytes.
531 dm->vlib_buffer_free_list_index =
532 vlib_buffer_get_or_create_free_list (vm,
533 VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
536 if (dm->conf->enable_tcp_udp_checksum)
537 dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
538 | IP_BUFFER_L4_CHECKSUM_COMPUTED);
540 /* vlib_buffer_t template */
541 vec_validate_aligned (dm->buffer_templates, tm->n_vlib_mains - 1,
542 CLIB_CACHE_LINE_BYTES);
543 for (i = 0; i < tm->n_vlib_mains; i++)
545 vlib_buffer_free_list_t *fl;
546 vlib_buffer_t *bt = vec_elt_at_index (dm->buffer_templates, i);
547 fl = vlib_buffer_get_free_list (vm,
548 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
549 vlib_buffer_init_for_free_list (bt, fl);
550 bt->flags = dm->buffer_flags_template;
551 bt->current_data = -RTE_PKTMBUF_HEADROOM;
552 vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0;
555 for (i = 0; i < nports; i++)
560 struct rte_eth_dev_info dev_info;
562 struct rte_eth_link l;
563 dpdk_device_config_t *devconf = 0;
564 vlib_pci_addr_t pci_addr;
567 rte_eth_dev_info_get (i, &dev_info);
568 if (dev_info.pci_dev) /* bonded interface has no pci info */
570 pci_addr.domain = dev_info.pci_dev->addr.domain;
571 pci_addr.bus = dev_info.pci_dev->addr.bus;
572 pci_addr.slot = dev_info.pci_dev->addr.devid;
573 pci_addr.function = dev_info.pci_dev->addr.function;
575 hash_get (dm->conf->device_config_index_by_pci_addr,
580 devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
582 devconf = &dm->conf->default_devconf;
584 /* Create vnet interface */
585 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
586 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
587 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
588 xd->cpu_socket = (i8) rte_eth_dev_socket_id (i);
590 /* Handle interface naming for devices with multiple ports sharing same PCI ID */
591 if (dev_info.pci_dev)
593 struct rte_eth_dev_info di = { 0 };
594 rte_eth_dev_info_get (i + 1, &di);
595 if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
596 memcmp (&dev_info.pci_dev->addr, &di.pci_dev->addr,
597 sizeof (struct rte_pci_addr)) == 0)
599 xd->interface_name_suffix = format (0, "0");
600 last_pci_addr.as_u32 = pci_addr.as_u32;
601 last_pci_addr_port = i;
603 else if (pci_addr.as_u32 == last_pci_addr.as_u32)
605 xd->interface_name_suffix =
606 format (0, "%u", i - last_pci_addr_port);
610 last_pci_addr.as_u32 = ~0;
614 last_pci_addr.as_u32 = ~0;
616 clib_memcpy (&xd->tx_conf, &dev_info.default_txconf,
617 sizeof (struct rte_eth_txconf));
618 if (dm->conf->no_multi_seg)
620 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
621 port_conf_template.rxmode.jumbo_frame = 0;
622 port_conf_template.rxmode.enable_scatter = 0;
626 xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
627 port_conf_template.rxmode.jumbo_frame = 1;
628 port_conf_template.rxmode.enable_scatter = 1;
629 xd->flags |= DPDK_DEVICE_FLAG_MAYBE_MULTISEG;
632 clib_memcpy (&xd->port_conf, &port_conf_template,
633 sizeof (struct rte_eth_conf));
635 xd->tx_q_used = clib_min (dev_info.max_tx_queues, tm->n_vlib_mains);
637 if (devconf->num_tx_queues > 0
638 && devconf->num_tx_queues < xd->tx_q_used)
639 xd->tx_q_used = clib_min (xd->tx_q_used, devconf->num_tx_queues);
641 if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
646 if (devconf->num_rx_queues > 1
647 && dev_info.max_rx_queues >= devconf->num_rx_queues)
649 xd->rx_q_used = devconf->num_rx_queues;
650 xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
651 if (devconf->rss_fn == 0)
652 xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
653 ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
655 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
660 xd->flags |= DPDK_DEVICE_FLAG_PMD;
662 /* workaround for drivers not setting driver_name */
663 if ((!dev_info.driver_name) && (dev_info.pci_dev))
664 dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
666 ASSERT (dev_info.driver_name);
672 #define _(s,f) else if (dev_info.driver_name && \
673 !strcmp(dev_info.driver_name, s)) \
674 xd->pmd = VNET_DPDK_PMD_##f;
680 xd->pmd = VNET_DPDK_PMD_UNKNOWN;
682 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
683 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
684 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
689 case VNET_DPDK_PMD_E1000EM:
690 case VNET_DPDK_PMD_IGB:
691 case VNET_DPDK_PMD_IGBVF:
692 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
696 case VNET_DPDK_PMD_IXGBE:
697 case VNET_DPDK_PMD_IXGBEVF:
698 case VNET_DPDK_PMD_THUNDERX:
699 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
701 case VNET_DPDK_PMD_DPAA2:
702 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
706 case VNET_DPDK_PMD_ENIC:
707 rte_eth_link_get_nowait (i, &l);
708 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
709 if (l.link_speed == 40000)
710 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
712 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
715 /* Intel Fortville */
716 case VNET_DPDK_PMD_I40E:
717 case VNET_DPDK_PMD_I40EVF:
718 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
719 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
721 switch (dev_info.pci_dev->id.device_id)
723 case I40E_DEV_ID_10G_BASE_T:
724 case I40E_DEV_ID_SFP_XL710:
725 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
727 case I40E_DEV_ID_QSFP_A:
728 case I40E_DEV_ID_QSFP_B:
729 case I40E_DEV_ID_QSFP_C:
730 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
733 rte_eth_link_get_nowait (i, &l);
734 xd->port_type = l.link_speed == 10000 ?
735 VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
738 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
742 case VNET_DPDK_PMD_CXGBE:
743 switch (dev_info.pci_dev->id.device_id)
745 case 0x540d: /* T580-CR */
746 case 0x5410: /* T580-LP-cr */
747 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
749 case 0x5403: /* T540-CR */
750 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
753 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
757 case VNET_DPDK_PMD_MLX5:
759 char *pn_100g[] = { "MCX415A-CCAT", "MCX416A-CCAT",
760 "MCX556A-ECAT", "MCX556A-EDAT", "MCX555A-ECAT",
761 "MCX515A-CCAT", "MCX516A-CCAT", "MCX516A-CDAT", 0
763 char *pn_40g[] = { "MCX413A-BCAT", "MCX414A-BCAT",
764 "MCX415A-BCAT", "MCX416A-BCAT", "MCX4131A-BCAT", 0
766 char *pn_10g[] = { "MCX4111A-XCAT", "MCX4121A-XCAT", 0 };
768 vlib_pci_device_t *pd = vlib_get_pci_device (&pci_addr);
772 pn = format (0, "%U%c",
773 format_vlib_pci_vpd, pd->vpd_r, "PN", 0);
779 while (!found && c[0])
781 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
783 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_100G;
790 while (!found && c[0])
792 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
794 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
801 while (!found && c[0])
803 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
805 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
815 /* Intel Red Rock Canyon */
816 case VNET_DPDK_PMD_FM10K:
817 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
821 case VNET_DPDK_PMD_VIRTIO:
822 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
823 xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
824 xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
828 case VNET_DPDK_PMD_VMXNET3:
829 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
830 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
833 case VNET_DPDK_PMD_AF_PACKET:
834 xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
835 xd->port_id = af_packet_port_id++;
838 case VNET_DPDK_PMD_BOND:
839 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
840 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
841 xd->port_id = bond_ether_port_id++;
845 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
848 if (devconf->num_rx_desc)
849 xd->nb_rx_desc = devconf->num_rx_desc;
851 if (devconf->num_tx_desc)
852 xd->nb_tx_desc = devconf->num_tx_desc;
856 * Ensure default mtu is not > the mtu read from the hardware.
857 * Otherwise rte_eth_dev_configure() will fail and the port will
860 if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
863 * This device does not support the platforms's max frame
864 * size. Use it's advertised mru instead.
866 xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
870 xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
873 * Some platforms do not account for Ethernet FCS (4 bytes) in
874 * MTU calculations. To interop with them increase mru but only
875 * if the device's settings can support it.
877 if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
878 xd->port_conf.rxmode.hw_strip_crc)
881 * Allow additional 4 bytes (for Ethernet FCS). These bytes are
882 * stripped by h/w and so will not consume any buffer memory.
884 xd->port_conf.rxmode.max_rx_pkt_len += 4;
888 if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
890 f64 now = vlib_time_now (vm);
892 rnd = (u32) (now * 1e6);
893 rnd = random_u32 (&rnd);
894 clib_memcpy (addr + 2, &rnd, sizeof (rnd));
899 rte_eth_macaddr_get (i, (struct ether_addr *) addr);
901 if (xd->tx_q_used < tm->n_vlib_mains)
902 dpdk_device_lock_init (xd);
904 xd->device_index = xd - dm->devices;
905 ASSERT (i == xd->device_index);
906 xd->per_interface_next_index = ~0;
908 /* assign interface to input thread */
909 dpdk_device_and_queue_t *dq;
912 if (devconf->hqos_enabled)
914 xd->flags |= DPDK_DEVICE_FLAG_HQOS;
916 if (devconf->hqos.hqos_thread_valid)
918 int cpu = dm->hqos_cpu_first_index + devconf->hqos.hqos_thread;
920 if (devconf->hqos.hqos_thread >= dm->hqos_cpu_count)
921 return clib_error_return (0, "invalid HQoS thread index");
923 vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
924 dq->device = xd->device_index;
929 int cpu = dm->hqos_cpu_first_index + next_hqos_cpu;
931 if (dm->hqos_cpu_count == 0)
932 return clib_error_return (0, "no HQoS threads available");
934 vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
935 dq->device = xd->device_index;
939 if (next_hqos_cpu == dm->hqos_cpu_count)
942 devconf->hqos.hqos_thread_valid = 1;
943 devconf->hqos.hqos_thread = cpu;
947 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
948 CLIB_CACHE_LINE_BYTES);
949 for (j = 0; j < tm->n_vlib_mains; j++)
951 vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
952 sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
953 vec_reset_length (xd->tx_vectors[j]);
956 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
957 CLIB_CACHE_LINE_BYTES);
958 for (j = 0; j < xd->rx_q_used; j++)
960 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
961 CLIB_CACHE_LINE_BYTES);
962 vec_reset_length (xd->rx_vectors[j]);
965 vec_validate_aligned (xd->d_trace_buffers, tm->n_vlib_mains,
966 CLIB_CACHE_LINE_BYTES);
969 /* count the number of descriptors used for this device */
970 nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
972 error = ethernet_register_interface
973 (dm->vnet_main, dpdk_device_class.index, xd->device_index,
974 /* ethernet address */ addr,
975 &xd->hw_if_index, dpdk_flag_change);
979 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->hw_if_index);
980 xd->vlib_sw_if_index = sw->sw_if_index;
981 vnet_set_device_input_node (dm->vnet_main, xd->hw_if_index,
982 dpdk_input_node.index);
984 if (devconf->workers)
989 clib_bitmap_foreach (i, devconf->workers, ({
990 vnet_device_input_assign_thread (dm->vnet_main, xd->hw_if_index, q++,
991 vdm->first_worker_thread_index + i);
996 for (q = 0; q < xd->rx_q_used; q++)
998 vnet_device_input_assign_thread (dm->vnet_main, xd->hw_if_index, q, /* any */
1002 hi = vnet_get_hw_interface (dm->vnet_main, xd->hw_if_index);
1004 rv = dpdk_port_setup (dm, xd);
1009 if (devconf->hqos_enabled)
1011 rv = dpdk_port_setup_hqos (xd, &devconf->hqos);
1017 * For cisco VIC vNIC, set default to VLAN strip enabled, unless
1018 * specified otherwise in the startup config.
1019 * For other NICs default to VLAN strip disabled, unless specified
1020 * otherwis in the startup config.
1022 if (xd->pmd == VNET_DPDK_PMD_ENIC)
1024 if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
1025 vlan_strip = 1; /* remove vlan tag from VIC port by default */
1027 clib_warning ("VLAN strip disabled for interface\n");
1029 else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
1035 vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
1036 vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
1037 xd->port_conf.rxmode.hw_vlan_strip = vlan_off;
1038 if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
1039 clib_warning ("VLAN strip enabled for interface\n");
1041 clib_warning ("VLAN strip cannot be supported by interface\n");
1044 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
1045 xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
1047 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
1050 if (nb_desc > dm->conf->num_mbufs)
1051 clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
1052 dm->conf->num_mbufs, nb_desc);
1058 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
1060 vlib_pci_main_t *pm = &pci_main;
1061 clib_error_t *error;
1062 vlib_pci_device_t *d;
1064 int num_whitelisted = vec_len (conf->dev_confs);
1067 pool_foreach (d, pm->pci_devs, ({
1068 dpdk_device_config_t * devconf = 0;
1069 vec_reset_length (pci_addr);
1070 pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
1072 if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
1075 if (num_whitelisted)
1077 uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
1082 devconf = pool_elt_at_index (conf->dev_confs, p[0]);
1086 if (d->vendor_id == 0x1af4 && d->device_id == 0x1000)
1089 else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
1091 /* all Intel network devices */
1092 else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_NETWORK_ETHERNET)
1094 /* all Intel QAT devices VFs */
1095 else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_PROCESSOR_CO &&
1096 (d->device_id == 0x0443 || d->device_id == 0x37c9 || d->device_id == 0x19e3))
1099 else if (d->vendor_id == 0x1137 && d->device_id == 0x0043)
1102 else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
1106 clib_warning ("Unsupported PCI device 0x%04x:0x%04x found "
1107 "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
1112 error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
1118 pool_get (conf->dev_confs, devconf);
1119 hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
1120 devconf - conf->dev_confs);
1121 devconf->pci_addr.as_u32 = d->bus_address.as_u32;
1123 devconf->is_blacklisted = 1;
1124 clib_error_report (error);
1128 vec_free (pci_addr);
1131 static clib_error_t *
1132 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
1133 unformat_input_t * input, u8 is_default)
1135 clib_error_t *error = 0;
1137 dpdk_device_config_t *devconf;
1138 unformat_input_t sub_input;
1142 devconf = &conf->default_devconf;
1146 p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
1150 pool_get (conf->dev_confs, devconf);
1151 hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
1152 devconf - conf->dev_confs);
1155 return clib_error_return (0,
1156 "duplicate configuration for PCI address %U",
1157 format_vlib_pci_addr, &pci_addr);
1160 devconf->pci_addr.as_u32 = pci_addr.as_u32;
1161 devconf->hqos_enabled = 0;
1162 dpdk_device_config_hqos_default (&devconf->hqos);
1167 unformat_skip_white_space (input);
1168 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1170 if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
1172 else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
1174 else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
1176 else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
1178 else if (unformat (input, "workers %U", unformat_bitmap_list,
1183 (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
1185 error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
1189 else if (unformat (input, "vlan-strip-offload off"))
1190 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
1191 else if (unformat (input, "vlan-strip-offload on"))
1192 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
1195 (input, "hqos %U", unformat_vlib_cli_sub_input, &sub_input))
1197 devconf->hqos_enabled = 1;
1198 error = unformat_hqos (&sub_input, &devconf->hqos);
1202 else if (unformat (input, "hqos"))
1204 devconf->hqos_enabled = 1;
1208 error = clib_error_return (0, "unknown input `%U'",
1209 format_unformat_error, input);
1217 if (devconf->workers && devconf->num_rx_queues == 0)
1218 devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
1219 else if (devconf->workers &&
1220 clib_bitmap_count_set_bits (devconf->workers) !=
1221 devconf->num_rx_queues)
1223 clib_error_return (0,
1224 "%U: number of worker threadds must be "
1225 "equal to number of rx queues", format_vlib_pci_addr,
1231 static clib_error_t *
1232 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
1234 clib_error_t *error = 0;
1235 dpdk_main_t *dm = &dpdk_main;
1236 dpdk_config_main_t *conf = &dpdk_config_main;
1237 vlib_thread_main_t *tm = vlib_get_thread_main ();
1238 dpdk_device_config_t *devconf;
1239 vlib_pci_addr_t pci_addr;
1240 unformat_input_t sub_input;
1242 u8 *rte_cmd = 0, *ethname = 0;
1245 int num_whitelisted = 0;
1252 conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
1254 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1256 /* Prime the pump */
1257 if (unformat (input, "no-hugetlb"))
1259 vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
1263 else if (unformat (input, "enable-tcp-udp-checksum"))
1264 conf->enable_tcp_udp_checksum = 1;
1266 else if (unformat (input, "decimal-interface-names"))
1267 conf->interface_name_format_decimal = 1;
1269 else if (unformat (input, "no-multi-seg"))
1270 conf->no_multi_seg = 1;
1272 else if (unformat (input, "enable-cryptodev"))
1273 conf->cryptodev = 1;
1275 else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1279 dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
1287 (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1288 unformat_vlib_cli_sub_input, &sub_input))
1290 error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1297 else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1299 error = dpdk_device_config (conf, pci_addr, 0, 0);
1306 else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1308 else if (unformat (input, "kni %d", &conf->num_kni))
1310 else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1312 else if (unformat (input, "socket-mem %s", &socket_mem))
1314 else if (unformat (input, "no-pci"))
1317 tmp = format (0, "--no-pci%c", 0);
1318 vec_add1 (conf->eal_init_args, tmp);
1320 else if (unformat (input, "poll-sleep %d", &dm->poll_sleep_usec))
1324 else if (unformat(input, #a)) \
1326 tmp = format (0, "--%s%c", #a, 0); \
1327 vec_add1 (conf->eal_init_args, tmp); \
1329 foreach_eal_double_hyphen_predicate_arg
1332 else if (unformat(input, #a " %s", &s)) \
1334 if (!strncmp(#a, "huge-dir", 8)) \
1336 else if (!strncmp(#a, "file-prefix", 11)) \
1338 tmp = format (0, "--%s%c", #a, 0); \
1339 vec_add1 (conf->eal_init_args, tmp); \
1341 if (!strncmp(#a, "vdev", 4)) \
1342 if (strstr((char*)s, "af_packet")) \
1343 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1344 vec_add1 (conf->eal_init_args, s); \
1346 foreach_eal_double_hyphen_arg
1349 else if (unformat(input, #a " %s", &s)) \
1351 tmp = format (0, "-%s%c", #b, 0); \
1352 vec_add1 (conf->eal_init_args, tmp); \
1354 vec_add1 (conf->eal_init_args, s); \
1356 foreach_eal_single_hyphen_arg
1359 else if (unformat(input, #a " %s", &s)) \
1361 tmp = format (0, "-%s%c", #b, 0); \
1362 vec_add1 (conf->eal_init_args, tmp); \
1364 vec_add1 (conf->eal_init_args, s); \
1365 conf->a##_set_manually = 1; \
1367 foreach_eal_single_hyphen_mandatory_arg
1369 else if (unformat (input, "default"))
1372 else if (unformat_skip_white_space (input))
1376 error = clib_error_return (0, "unknown input `%U'",
1377 format_unformat_error, input);
1382 if (!conf->uio_driver_name)
1383 conf->uio_driver_name = format (0, "uio_pci_generic%c", 0);
1386 * Use 1G huge pages if available.
1388 if (!no_huge && !huge_dir)
1390 u32 x, *mem_by_socket = 0;
1394 u8 less_than_1g = 1;
1397 umount (DEFAULT_HUGE_DIR);
1399 /* Process "socket-mem" parameter value */
1400 if (vec_len (socket_mem))
1402 unformat_input_t in;
1403 unformat_init_vector (&in, socket_mem);
1404 while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1406 if (unformat (&in, "%u,", &x))
1408 else if (unformat (&in, "%u", &x))
1410 else if (unformat (&in, ","))
1415 vec_add1 (mem_by_socket, x);
1420 /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1421 unformat_free (&in);
1427 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1429 vec_validate(mem_by_socket, c);
1430 mem_by_socket[c] = 256; /* default per-socket mem */
1436 /* check if available enough 1GB pages for each socket */
1438 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1440 int pages_avail, page_size, mem;
1442 vec_validate(mem_by_socket, c);
1443 mem = mem_by_socket[c];
1446 pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1448 if (pages_avail < 0 || page_size * pages_avail < mem)
1452 pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1454 if (pages_avail < 0 || page_size * pages_avail < mem)
1459 if (mem_by_socket == 0)
1461 error = clib_error_return (0, "mem_by_socket NULL");
1464 _vec_len (mem_by_socket) = c + 1;
1466 /* regenerate socket_mem string */
1467 vec_foreach_index (x, mem_by_socket)
1468 socket_mem = format (socket_mem, "%s%u",
1469 socket_mem ? "," : "", mem_by_socket[x]);
1470 socket_mem = format (socket_mem, "%c", 0);
1472 vec_free (mem_by_socket);
1474 rv = mkdir (VPP_RUN_DIR, 0755);
1475 if (rv && errno != EEXIST)
1477 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1478 VPP_RUN_DIR, errno);
1482 rv = mkdir (DEFAULT_HUGE_DIR, 0755);
1483 if (rv && errno != EEXIST)
1485 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1486 DEFAULT_HUGE_DIR, errno);
1490 if (use_1g && !(less_than_1g && use_2m))
1493 mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1497 rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1501 return clib_error_return (0, "not enough free huge pages");
1506 error = clib_error_return (0, "mount failed %d", errno);
1510 tmp = format (0, "--huge-dir%c", 0);
1511 vec_add1 (conf->eal_init_args, tmp);
1512 tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1513 vec_add1 (conf->eal_init_args, tmp);
1516 tmp = format (0, "--file-prefix%c", 0);
1517 vec_add1 (conf->eal_init_args, tmp);
1518 tmp = format (0, "vpp%c", 0);
1519 vec_add1 (conf->eal_init_args, tmp);
1529 /* I'll bet that -c and -n must be the first and second args... */
1530 if (!conf->coremask_set_manually)
1532 vlib_thread_registration_t *tr;
1533 uword *coremask = 0;
1536 /* main thread core */
1537 coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1539 for (i = 0; i < vec_len (tm->registrations); i++)
1541 tr = tm->registrations[i];
1542 coremask = clib_bitmap_or (coremask, tr->coremask);
1545 vec_insert (conf->eal_init_args, 2, 1);
1546 conf->eal_init_args[1] = (u8 *) "-c";
1547 tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1548 conf->eal_init_args[2] = tmp;
1549 clib_bitmap_free (coremask);
1552 if (!conf->nchannels_set_manually)
1554 vec_insert (conf->eal_init_args, 2, 3);
1555 conf->eal_init_args[3] = (u8 *) "-n";
1556 tmp = format (0, "%d", conf->nchannels);
1557 conf->eal_init_args[4] = tmp;
1560 if (no_pci == 0 && geteuid () == 0)
1561 dpdk_bind_devices_to_uio (conf);
1564 if (devconf->x == 0 && conf->default_devconf.x > 0) \
1565 devconf->x = conf->default_devconf.x ;
1568 pool_foreach (devconf, conf->dev_confs, ({
1570 /* default per-device config items */
1571 foreach_dpdk_device_config_item
1573 /* add DPDK EAL whitelist/blacklist entry */
1574 if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1576 tmp = format (0, "-w%c", 0);
1577 vec_add1 (conf->eal_init_args, tmp);
1578 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1579 vec_add1 (conf->eal_init_args, tmp);
1581 else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1583 tmp = format (0, "-b%c", 0);
1584 vec_add1 (conf->eal_init_args, tmp);
1585 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1586 vec_add1 (conf->eal_init_args, tmp);
1593 /* set master-lcore */
1594 tmp = format (0, "--master-lcore%c", 0);
1595 vec_add1 (conf->eal_init_args, tmp);
1596 tmp = format (0, "%u%c", tm->main_lcore, 0);
1597 vec_add1 (conf->eal_init_args, tmp);
1599 /* set socket-mem */
1600 tmp = format (0, "--socket-mem%c", 0);
1601 vec_add1 (conf->eal_init_args, tmp);
1602 tmp = format (0, "%s%c", socket_mem, 0);
1603 vec_add1 (conf->eal_init_args, tmp);
1605 /* NULL terminate the "argv" vector, in case of stupidity */
1606 vec_add1 (conf->eal_init_args, 0);
1607 _vec_len (conf->eal_init_args) -= 1;
1609 /* Set up DPDK eal and packet mbuf pool early. */
1611 log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1613 rte_set_log_level (log_level);
1615 vm = vlib_get_main ();
1617 /* make copy of args as rte_eal_init tends to mess up with arg array */
1618 for (i = 1; i < vec_len (conf->eal_init_args); i++)
1619 conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1620 conf->eal_init_args[i]);
1623 rte_eal_init (vec_len (conf->eal_init_args),
1624 (char **) conf->eal_init_args);
1626 /* lazy umount hugepages */
1627 umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
1630 return clib_error_return (0, "rte_eal_init returned %d", ret);
1632 /* Dump the physical memory layout prior to creating the mbuf_pool */
1633 fprintf (stdout, "DPDK physical memory layout:\n");
1634 rte_dump_physmem_layout (stdout);
1636 /* main thread 1st */
1637 error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
1641 for (i = 0; i < RTE_MAX_LCORE; i++)
1643 error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1644 rte_lcore_to_socket_id (i));
1653 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1656 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1658 vnet_main_t *vnm = vnet_get_main ();
1659 struct rte_eth_link prev_link = xd->link;
1661 u8 hw_flags_chg = 0;
1663 /* only update link state for PMD interfaces */
1664 if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1667 xd->time_last_link_update = now ? now : xd->time_last_link_update;
1668 memset (&xd->link, 0, sizeof (xd->link));
1669 rte_eth_link_get_nowait (xd->device_index, &xd->link);
1671 if (LINK_STATE_ELOGS)
1673 vlib_main_t *vm = vlib_get_main ();
1674 ELOG_TYPE_DECLARE (e) =
1677 "update-link-state: sw_if_index %d, admin_up %d,"
1678 "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1687 ed = ELOG_DATA (&vm->elog_main, e);
1688 ed->sw_if_index = xd->vlib_sw_if_index;
1689 ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1690 ed->old_link_state = (u8)
1691 vnet_hw_interface_is_link_up (vnm, xd->hw_if_index);
1692 ed->new_link_state = (u8) xd->link.link_status;
1695 if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
1696 ((xd->link.link_status != 0) ^
1697 vnet_hw_interface_is_link_up (vnm, xd->hw_if_index)))
1700 hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1703 if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1706 switch (xd->link.link_duplex)
1708 case ETH_LINK_HALF_DUPLEX:
1709 hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1711 case ETH_LINK_FULL_DUPLEX:
1712 hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1718 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1721 switch (xd->link.link_speed)
1723 case ETH_SPEED_NUM_10M:
1724 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1726 case ETH_SPEED_NUM_100M:
1727 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1729 case ETH_SPEED_NUM_1G:
1730 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1732 case ETH_SPEED_NUM_10G:
1733 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1735 case ETH_SPEED_NUM_40G:
1736 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1741 clib_warning ("unknown link speed %d", xd->link.link_speed);
1747 if (LINK_STATE_ELOGS)
1749 vlib_main_t *vm = vlib_get_main ();
1751 ELOG_TYPE_DECLARE (e) =
1754 "update-link-state: sw_if_index %d, new flags %d",.format_args
1762 ed = ELOG_DATA (&vm->elog_main, e);
1763 ed->sw_if_index = xd->vlib_sw_if_index;
1764 ed->flags = hw_flags;
1766 vnet_hw_interface_set_flags (vnm, xd->hw_if_index, hw_flags);
1771 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1773 clib_error_t *error;
1774 vnet_main_t *vnm = vnet_get_main ();
1775 dpdk_main_t *dm = &dpdk_main;
1776 ethernet_main_t *em = ðernet_main;
1778 vlib_thread_main_t *tm = vlib_get_thread_main ();
1781 error = dpdk_lib_init (dm);
1784 clib_error_report (error);
1786 tm->worker_thread_release = 1;
1788 f64 now = vlib_time_now (vm);
1789 vec_foreach (xd, dm->devices)
1791 dpdk_update_link_state (xd, now);
1796 * Extra set up for bond interfaces:
1797 * 1. Setup MACs for bond interfaces and their slave links which was set
1798 * in dpdk_port_setup() but needs to be done again here to take effect.
1799 * 2. Set up info for bond interface related CLI support.
1801 int nports = rte_eth_dev_count ();
1804 for (i = 0; i < nports; i++)
1806 xd = &dm->devices[i];
1807 ASSERT (i == xd->device_index);
1808 if (xd->pmd == VNET_DPDK_PMD_BOND)
1812 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1815 vnet_hw_interface_t *bhi;
1816 ethernet_interface_t *bei;
1819 /* Get MAC of 1st slave link */
1821 (slink[0], (struct ether_addr *) addr);
1823 /* Set MAC of bounded interface to that of 1st slave link */
1824 clib_warning ("Set MAC for bond dev# %d", i);
1825 rv = rte_eth_bond_mac_address_set
1826 (i, (struct ether_addr *) addr);
1828 clib_warning ("Set MAC addr failure rv=%d", rv);
1830 /* Populate MAC of bonded interface in VPP hw tables */
1831 bhi = vnet_get_hw_interface
1832 (vnm, dm->devices[i].hw_if_index);
1833 bei = pool_elt_at_index
1834 (em->interfaces, bhi->hw_instance);
1835 clib_memcpy (bhi->hw_address, addr, 6);
1836 clib_memcpy (bei->address, addr, 6);
1838 /* Init l3 packet size allowed on bonded interface */
1839 bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
1840 bhi->max_l3_packet_bytes[VLIB_RX] =
1841 bhi->max_l3_packet_bytes[VLIB_TX] =
1842 ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1844 { /* for all slave links */
1845 int slave = slink[--nlink];
1846 dpdk_device_t *sdev = &dm->devices[slave];
1847 vnet_hw_interface_t *shi;
1848 vnet_sw_interface_t *ssi;
1849 ethernet_interface_t *sei;
1850 /* Add MAC to all slave links except the first one */
1853 clib_warning ("Add MAC for slave dev# %d", slave);
1854 rv = rte_eth_dev_mac_addr_add
1855 (slave, (struct ether_addr *) addr, 0);
1857 clib_warning ("Add MAC addr failure rv=%d", rv);
1859 /* Set slaves bitmap for bonded interface */
1860 bhi->bond_info = clib_bitmap_set
1861 (bhi->bond_info, sdev->hw_if_index, 1);
1862 /* Set slave link flags on slave interface */
1863 shi = vnet_get_hw_interface (vnm, sdev->hw_if_index);
1864 ssi = vnet_get_sw_interface
1865 (vnm, sdev->vlib_sw_if_index);
1866 sei = pool_elt_at_index
1867 (em->interfaces, shi->hw_instance);
1869 shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1870 ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1871 clib_memcpy (shi->hw_address, addr, 6);
1872 clib_memcpy (sei->address, addr, 6);
1874 /* Set l3 packet size allowed as the lowest of slave */
1875 if (bhi->max_l3_packet_bytes[VLIB_RX] >
1876 shi->max_l3_packet_bytes[VLIB_RX])
1877 bhi->max_l3_packet_bytes[VLIB_RX] =
1878 bhi->max_l3_packet_bytes[VLIB_TX] =
1879 shi->max_l3_packet_bytes[VLIB_RX];
1881 /* Set max packet size allowed as the lowest of slave */
1882 if (bhi->max_packet_bytes > shi->max_packet_bytes)
1883 bhi->max_packet_bytes = shi->max_packet_bytes;
1894 * check each time through the loop in case intervals are changed
1896 f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1897 dm->link_state_poll_interval : dm->stat_poll_interval;
1899 vlib_process_wait_for_event_or_clock (vm, min_wait);
1901 if (dm->admin_up_down_in_progress)
1902 /* skip the poll if an admin up down is in progress (on any interface) */
1905 vec_foreach (xd, dm->devices)
1907 f64 now = vlib_time_now (vm);
1908 if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1909 dpdk_update_counters (xd, now);
1910 if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1911 dpdk_update_link_state (xd, now);
1920 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1921 .function = dpdk_process,
1922 .type = VLIB_NODE_TYPE_PROCESS,
1923 .name = "dpdk-process",
1924 .process_log2_n_stack_bytes = 17,
1929 dpdk_set_stat_poll_interval (f64 interval)
1931 if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1932 return (VNET_API_ERROR_INVALID_VALUE);
1934 dpdk_main.stat_poll_interval = interval;
1940 dpdk_set_link_state_poll_interval (f64 interval)
1942 if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1943 return (VNET_API_ERROR_INVALID_VALUE);
1945 dpdk_main.link_state_poll_interval = interval;
1951 dpdk_init (vlib_main_t * vm)
1953 dpdk_main_t *dm = &dpdk_main;
1955 clib_error_t *error = 0;
1956 vlib_thread_main_t *tm = vlib_get_thread_main ();
1958 /* verify that structs are cacheline aligned */
1959 STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1960 "Cache line marker must be 1st element in dpdk_device_t");
1961 STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1962 CLIB_CACHE_LINE_BYTES,
1963 "Data in cache line 0 is bigger than cache line size");
1964 STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1965 "Cache line marker must be 1st element in frame_queue_trace_t");
1968 name = format (0, "dpdk_%08x%c", api_version, 0);
1970 /* Ask for a correctly-sized block of API message decode slots */
1971 dm->msg_id_base = vl_msg_api_get_msg_ids
1972 ((char *) name, VL_MSG_FIRST_AVAILABLE);
1976 dm->vnet_main = vnet_get_main ();
1977 dm->conf = &dpdk_config_main;
1979 error = dpdk_plugin_api_hookup (vm);
1981 /* Add our API messages to the global name_crc hash table */
1982 setup_message_id_table (dm, &api_main);
1985 // plugin_custom_dump_configure (dm);
1987 ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1989 return clib_error_return (0, "ethernet-input node AWOL");
1991 dm->ethernet_input_node_index = ei->index;
1993 dm->conf->nchannels = 4;
1994 dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1995 vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1997 dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1998 dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1999 dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
2001 /* $$$ use n_thread_stacks since it's known-good at this point */
2002 vec_validate (dm->recycle, tm->n_thread_stacks - 1);
2004 /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
2005 dm->buffer_flags_template =
2006 (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_EXT_HDR_VALID
2007 | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
2009 dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
2010 dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
2013 if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
2019 VLIB_INIT_FUNCTION (dpdk_init);
2023 * fd.io coding-style-patch-verification: ON
2026 * eval: (c-set-style "gnu")