dpdk: refactor setup, start, stop code
[vpp.git] / src / plugins / dpdk / device / init.c
1 /*
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>
19 #include <vppinfra/bitmap.h>
20
21 #include <vnet/ethernet/ethernet.h>
22 #include <dpdk/device/dpdk.h>
23 #include <vlib/unix/physmem.h>
24 #include <vlib/pci/pci.h>
25
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/device/dpdk_priv.h>
35
36 dpdk_main_t dpdk_main;
37
38 #define LINK_STATE_ELOGS        0
39
40 #define DEFAULT_HUGE_DIR "/run/vpp/hugepages"
41 #define VPP_RUN_DIR "/run/vpp"
42
43 /* Port configuration, mildly modified Intel app values */
44
45 static struct rte_eth_conf port_conf_template = {
46   .rxmode = {
47              .split_hdr_size = 0,
48              .header_split = 0,         /**< Header Split disabled */
49              .hw_ip_checksum = 0,       /**< IP checksum offload disabled */
50              .hw_vlan_filter = 0,       /**< VLAN filtering disabled */
51              .hw_strip_crc = 0,         /**< CRC stripped by hardware */
52              },
53   .txmode = {
54              .mq_mode = ETH_MQ_TX_NONE,
55              },
56 };
57
58
59 static u32
60 dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
61 {
62   dpdk_main_t *dm = &dpdk_main;
63   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
64   u32 old = 0;
65
66   if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
67     {
68       old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
69
70       if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
71         xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
72       else
73         xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
74
75       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
76         {
77           if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
78             rte_eth_promiscuous_enable (xd->device_index);
79           else
80             rte_eth_promiscuous_disable (xd->device_index);
81         }
82     }
83   else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU (flags))
84     {
85       int rv;
86
87       xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
88
89       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
90         dpdk_device_stop (xd);
91
92       rv = rte_eth_dev_configure
93         (xd->device_index, xd->rx_q_used, xd->tx_q_used, &xd->port_conf);
94
95       if (rv < 0)
96         vlib_cli_output (vlib_get_main (),
97                          "rte_eth_dev_configure[%d]: err %d",
98                          xd->device_index, rv);
99
100       rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
101
102       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
103         {
104           clib_error_t *error;
105           error = dpdk_device_start (xd);
106           clib_error_report (error);
107         }
108
109     }
110   return old;
111 }
112
113 static void
114 dpdk_device_lock_init (dpdk_device_t * xd)
115 {
116   int q;
117   vec_validate (xd->lockp, xd->tx_q_used - 1);
118   for (q = 0; q < xd->tx_q_used; q++)
119     {
120       xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
121                                              CLIB_CACHE_LINE_BYTES);
122       memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
123     }
124 }
125
126 static clib_error_t *
127 dpdk_lib_init (dpdk_main_t * dm)
128 {
129   u32 nports;
130   u32 nb_desc = 0;
131   int i;
132   clib_error_t *error;
133   vlib_main_t *vm = vlib_get_main ();
134   vlib_thread_main_t *tm = vlib_get_thread_main ();
135   vnet_device_main_t *vdm = &vnet_device_main;
136   vnet_sw_interface_t *sw;
137   vnet_hw_interface_t *hi;
138   dpdk_device_t *xd;
139   vlib_pci_addr_t last_pci_addr;
140   u32 last_pci_addr_port = 0;
141   vlib_thread_registration_t *tr_hqos;
142   uword *p_hqos;
143
144   u32 next_hqos_cpu = 0;
145   u8 af_packet_port_id = 0;
146   u8 bond_ether_port_id = 0;
147   last_pci_addr.as_u32 = ~0;
148
149   dm->hqos_cpu_first_index = 0;
150   dm->hqos_cpu_count = 0;
151
152   /* find out which cpus will be used for I/O TX */
153   p_hqos = hash_get_mem (tm->thread_registrations_by_name, "hqos-threads");
154   tr_hqos = p_hqos ? (vlib_thread_registration_t *) p_hqos[0] : 0;
155
156   if (tr_hqos && tr_hqos->count > 0)
157     {
158       dm->hqos_cpu_first_index = tr_hqos->first_index;
159       dm->hqos_cpu_count = tr_hqos->count;
160     }
161
162   vec_validate_aligned (dm->devices_by_hqos_cpu, tm->n_vlib_mains - 1,
163                         CLIB_CACHE_LINE_BYTES);
164
165   nports = rte_eth_dev_count ();
166   if (nports < 1)
167     {
168       clib_warning ("DPDK drivers found no ports...");
169     }
170
171   if (CLIB_DEBUG > 0)
172     clib_warning ("DPDK drivers found %d ports...", nports);
173
174   /*
175    * All buffers are all allocated from the same rte_mempool.
176    * Thus they all have the same number of data bytes.
177    */
178   dm->vlib_buffer_free_list_index =
179     vlib_buffer_get_or_create_free_list (vm,
180                                          VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
181                                          "dpdk rx");
182
183   if (dm->conf->enable_tcp_udp_checksum)
184     dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
185                                    | IP_BUFFER_L4_CHECKSUM_COMPUTED);
186
187   /* vlib_buffer_t template */
188   vec_validate_aligned (dm->buffer_templates, tm->n_vlib_mains - 1,
189                         CLIB_CACHE_LINE_BYTES);
190   for (i = 0; i < tm->n_vlib_mains; i++)
191     {
192       vlib_buffer_free_list_t *fl;
193       vlib_buffer_t *bt = vec_elt_at_index (dm->buffer_templates, i);
194       fl = vlib_buffer_get_free_list (vm,
195                                       VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
196       vlib_buffer_init_for_free_list (bt, fl);
197       bt->flags = dm->buffer_flags_template;
198       bt->current_data = -RTE_PKTMBUF_HEADROOM;
199       vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0;
200     }
201
202   for (i = 0; i < nports; i++)
203     {
204       u8 addr[6];
205       u8 vlan_strip = 0;
206       int j;
207       struct rte_eth_dev_info dev_info;
208       clib_error_t *rv;
209       struct rte_eth_link l;
210       dpdk_device_config_t *devconf = 0;
211       vlib_pci_addr_t pci_addr;
212       uword *p = 0;
213
214       rte_eth_dev_info_get (i, &dev_info);
215       if (dev_info.pci_dev)     /* bonded interface has no pci info */
216         {
217           pci_addr.domain = dev_info.pci_dev->addr.domain;
218           pci_addr.bus = dev_info.pci_dev->addr.bus;
219           pci_addr.slot = dev_info.pci_dev->addr.devid;
220           pci_addr.function = dev_info.pci_dev->addr.function;
221           p =
222             hash_get (dm->conf->device_config_index_by_pci_addr,
223                       pci_addr.as_u32);
224         }
225
226       if (p)
227         devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
228       else
229         devconf = &dm->conf->default_devconf;
230
231       /* Create vnet interface */
232       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
233       xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
234       xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
235       xd->cpu_socket = (i8) rte_eth_dev_socket_id (i);
236
237       /* Handle interface naming for devices with multiple ports sharing same PCI ID */
238       if (dev_info.pci_dev)
239         {
240           struct rte_eth_dev_info di = { 0 };
241           rte_eth_dev_info_get (i + 1, &di);
242           if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
243               memcmp (&dev_info.pci_dev->addr, &di.pci_dev->addr,
244                       sizeof (struct rte_pci_addr)) == 0)
245             {
246               xd->interface_name_suffix = format (0, "0");
247               last_pci_addr.as_u32 = pci_addr.as_u32;
248               last_pci_addr_port = i;
249             }
250           else if (pci_addr.as_u32 == last_pci_addr.as_u32)
251             {
252               xd->interface_name_suffix =
253                 format (0, "%u", i - last_pci_addr_port);
254             }
255           else
256             {
257               last_pci_addr.as_u32 = ~0;
258             }
259         }
260       else
261         last_pci_addr.as_u32 = ~0;
262
263       clib_memcpy (&xd->tx_conf, &dev_info.default_txconf,
264                    sizeof (struct rte_eth_txconf));
265       if (dm->conf->no_multi_seg)
266         {
267           xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
268           port_conf_template.rxmode.jumbo_frame = 0;
269           port_conf_template.rxmode.enable_scatter = 0;
270         }
271       else
272         {
273           xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
274           port_conf_template.rxmode.jumbo_frame = 1;
275           port_conf_template.rxmode.enable_scatter = 1;
276           xd->flags |= DPDK_DEVICE_FLAG_MAYBE_MULTISEG;
277         }
278
279       clib_memcpy (&xd->port_conf, &port_conf_template,
280                    sizeof (struct rte_eth_conf));
281
282       xd->tx_q_used = clib_min (dev_info.max_tx_queues, tm->n_vlib_mains);
283
284       if (devconf->num_tx_queues > 0
285           && devconf->num_tx_queues < xd->tx_q_used)
286         xd->tx_q_used = clib_min (xd->tx_q_used, devconf->num_tx_queues);
287
288       if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
289         {
290           dm->use_rss = 1;
291         }
292
293       if (devconf->num_rx_queues > 1
294           && dev_info.max_rx_queues >= devconf->num_rx_queues)
295         {
296           xd->rx_q_used = devconf->num_rx_queues;
297           xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
298           if (devconf->rss_fn == 0)
299             xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
300               ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
301           else
302             xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
303         }
304       else
305         xd->rx_q_used = 1;
306
307       xd->flags |= DPDK_DEVICE_FLAG_PMD;
308
309       /* workaround for drivers not setting driver_name */
310       if ((!dev_info.driver_name) && (dev_info.pci_dev))
311         dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
312
313       ASSERT (dev_info.driver_name);
314
315       if (!xd->pmd)
316         {
317
318
319 #define _(s,f) else if (dev_info.driver_name &&                 \
320                         !strcmp(dev_info.driver_name, s))       \
321                  xd->pmd = VNET_DPDK_PMD_##f;
322           if (0)
323             ;
324           foreach_dpdk_pmd
325 #undef _
326             else
327             xd->pmd = VNET_DPDK_PMD_UNKNOWN;
328
329           xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
330           xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
331           xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
332
333           switch (xd->pmd)
334             {
335               /* 1G adapters */
336             case VNET_DPDK_PMD_E1000EM:
337             case VNET_DPDK_PMD_IGB:
338             case VNET_DPDK_PMD_IGBVF:
339               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
340               break;
341
342               /* 10G adapters */
343             case VNET_DPDK_PMD_IXGBE:
344             case VNET_DPDK_PMD_IXGBEVF:
345             case VNET_DPDK_PMD_THUNDERX:
346               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
347               break;
348             case VNET_DPDK_PMD_DPAA2:
349               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
350               break;
351
352               /* Cisco VIC */
353             case VNET_DPDK_PMD_ENIC:
354               rte_eth_link_get_nowait (i, &l);
355               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
356               if (l.link_speed == 40000)
357                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
358               else
359                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
360               break;
361
362               /* Intel Fortville */
363             case VNET_DPDK_PMD_I40E:
364             case VNET_DPDK_PMD_I40EVF:
365               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
366               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
367
368               if (dev_info.speed_capa & ETH_LINK_SPEED_40G)
369                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
370               else if (dev_info.speed_capa & ETH_LINK_SPEED_25G)
371                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_25G;
372               else if (dev_info.speed_capa & ETH_LINK_SPEED_10G)
373                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
374               else
375                 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
376               break;
377
378             case VNET_DPDK_PMD_CXGBE:
379               switch (dev_info.pci_dev->id.device_id)
380                 {
381                 case 0x540d:    /* T580-CR */
382                 case 0x5410:    /* T580-LP-cr */
383                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
384                   break;
385                 case 0x5403:    /* T540-CR */
386                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
387                   break;
388                 default:
389                   xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
390                 }
391               break;
392
393             case VNET_DPDK_PMD_MLX5:
394               {
395                 char *pn_100g[] = { "MCX415A-CCAT", "MCX416A-CCAT",
396                   "MCX556A-ECAT", "MCX556A-EDAT", "MCX555A-ECAT",
397                   "MCX515A-CCAT", "MCX516A-CCAT", "MCX516A-CDAT", 0
398                 };
399                 char *pn_40g[] = { "MCX413A-BCAT", "MCX414A-BCAT",
400                   "MCX415A-BCAT", "MCX416A-BCAT", "MCX4131A-BCAT", 0
401                 };
402                 char *pn_10g[] = { "MCX4111A-XCAT", "MCX4121A-XCAT", 0 };
403
404                 vlib_pci_device_t *pd = vlib_get_pci_device (&pci_addr);
405                 u8 *pn = 0;
406                 char **c;
407                 int found = 0;
408                 pn = format (0, "%U%c",
409                              format_vlib_pci_vpd, pd->vpd_r, "PN", 0);
410
411                 if (!pn)
412                   break;
413
414                 c = pn_100g;
415                 while (!found && c[0])
416                   {
417                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
418                       {
419                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_100G;
420                         break;
421                       }
422                     c++;
423                   }
424
425                 c = pn_40g;
426                 while (!found && c[0])
427                   {
428                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
429                       {
430                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
431                         break;
432                       }
433                     c++;
434                   }
435
436                 c = pn_10g;
437                 while (!found && c[0])
438                   {
439                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
440                       {
441                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
442                         break;
443                       }
444                     c++;
445                   }
446
447                 vec_free (pn);
448               }
449
450               break;
451               /* Intel Red Rock Canyon */
452             case VNET_DPDK_PMD_FM10K:
453               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
454               break;
455
456               /* virtio */
457             case VNET_DPDK_PMD_VIRTIO:
458               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
459               xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
460               xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
461               break;
462
463               /* vmxnet3 */
464             case VNET_DPDK_PMD_VMXNET3:
465               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
466               xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
467               break;
468
469             case VNET_DPDK_PMD_AF_PACKET:
470               xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
471               xd->port_id = af_packet_port_id++;
472               break;
473
474             case VNET_DPDK_PMD_BOND:
475               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
476               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
477               xd->port_id = bond_ether_port_id++;
478               break;
479
480             default:
481               xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
482             }
483
484           if (devconf->num_rx_desc)
485             xd->nb_rx_desc = devconf->num_rx_desc;
486
487           if (devconf->num_tx_desc)
488             xd->nb_tx_desc = devconf->num_tx_desc;
489         }
490
491       /*
492        * Ensure default mtu is not > the mtu read from the hardware.
493        * Otherwise rte_eth_dev_configure() will fail and the port will
494        * not be available.
495        */
496       if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
497         {
498           /*
499            * This device does not support the platforms's max frame
500            * size. Use it's advertised mru instead.
501            */
502           xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
503         }
504       else
505         {
506           xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
507
508           /*
509            * Some platforms do not account for Ethernet FCS (4 bytes) in
510            * MTU calculations. To interop with them increase mru but only
511            * if the device's settings can support it.
512            */
513           if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
514               xd->port_conf.rxmode.hw_strip_crc)
515             {
516               /*
517                * Allow additional 4 bytes (for Ethernet FCS). These bytes are
518                * stripped by h/w and so will not consume any buffer memory.
519                */
520               xd->port_conf.rxmode.max_rx_pkt_len += 4;
521             }
522         }
523
524       if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
525         {
526           f64 now = vlib_time_now (vm);
527           u32 rnd;
528           rnd = (u32) (now * 1e6);
529           rnd = random_u32 (&rnd);
530           clib_memcpy (addr + 2, &rnd, sizeof (rnd));
531           addr[0] = 2;
532           addr[1] = 0xfe;
533         }
534       else
535         rte_eth_macaddr_get (i, (struct ether_addr *) addr);
536
537       if (xd->tx_q_used < tm->n_vlib_mains)
538         dpdk_device_lock_init (xd);
539
540       xd->device_index = xd - dm->devices;
541       ASSERT (i == xd->device_index);
542       xd->per_interface_next_index = ~0;
543
544       /* assign interface to input thread */
545       dpdk_device_and_queue_t *dq;
546       int q;
547
548       if (devconf->hqos_enabled)
549         {
550           xd->flags |= DPDK_DEVICE_FLAG_HQOS;
551
552           if (devconf->hqos.hqos_thread_valid)
553             {
554               int cpu = dm->hqos_cpu_first_index + devconf->hqos.hqos_thread;
555
556               if (devconf->hqos.hqos_thread >= dm->hqos_cpu_count)
557                 return clib_error_return (0, "invalid HQoS thread index");
558
559               vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
560               dq->device = xd->device_index;
561               dq->queue_id = 0;
562             }
563           else
564             {
565               int cpu = dm->hqos_cpu_first_index + next_hqos_cpu;
566
567               if (dm->hqos_cpu_count == 0)
568                 return clib_error_return (0, "no HQoS threads available");
569
570               vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
571               dq->device = xd->device_index;
572               dq->queue_id = 0;
573
574               next_hqos_cpu++;
575               if (next_hqos_cpu == dm->hqos_cpu_count)
576                 next_hqos_cpu = 0;
577
578               devconf->hqos.hqos_thread_valid = 1;
579               devconf->hqos.hqos_thread = cpu;
580             }
581         }
582
583       vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
584                             CLIB_CACHE_LINE_BYTES);
585       for (j = 0; j < tm->n_vlib_mains; j++)
586         {
587           vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
588                            sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
589           vec_reset_length (xd->tx_vectors[j]);
590         }
591
592       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
593                             CLIB_CACHE_LINE_BYTES);
594       for (j = 0; j < xd->rx_q_used; j++)
595         {
596           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
597                                 CLIB_CACHE_LINE_BYTES);
598           vec_reset_length (xd->rx_vectors[j]);
599         }
600
601       vec_validate_aligned (xd->d_trace_buffers, tm->n_vlib_mains,
602                             CLIB_CACHE_LINE_BYTES);
603
604
605       /* count the number of descriptors used for this device */
606       nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
607
608       error = ethernet_register_interface
609         (dm->vnet_main, dpdk_device_class.index, xd->device_index,
610          /* ethernet address */ addr,
611          &xd->hw_if_index, dpdk_flag_change);
612       if (error)
613         return error;
614
615       sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->hw_if_index);
616       xd->vlib_sw_if_index = sw->sw_if_index;
617       vnet_hw_interface_set_input_node (dm->vnet_main, xd->hw_if_index,
618                                         dpdk_input_node.index);
619
620       if (devconf->workers)
621         {
622           int i;
623           q = 0;
624           /* *INDENT-OFF* */
625           clib_bitmap_foreach (i, devconf->workers, ({
626             vnet_hw_interface_assign_rx_thread (dm->vnet_main, xd->hw_if_index, q++,
627                                              vdm->first_worker_thread_index + i);
628           }));
629           /* *INDENT-ON* */
630         }
631       else
632         for (q = 0; q < xd->rx_q_used; q++)
633           {
634             vnet_hw_interface_assign_rx_thread (dm->vnet_main, xd->hw_if_index, q,      /* any */
635                                                 ~1);
636           }
637
638       hi = vnet_get_hw_interface (dm->vnet_main, xd->hw_if_index);
639
640       rv = dpdk_device_setup (xd);
641
642       if (rv)
643         return rv;
644
645       if (devconf->hqos_enabled)
646         {
647           rv = dpdk_port_setup_hqos (xd, &devconf->hqos);
648           if (rv)
649             return rv;
650         }
651
652       /*
653        * For cisco VIC vNIC, set default to VLAN strip enabled, unless
654        * specified otherwise in the startup config.
655        * For other NICs default to VLAN strip disabled, unless specified
656        * otherwis in the startup config.
657        */
658       if (xd->pmd == VNET_DPDK_PMD_ENIC)
659         {
660           if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
661             vlan_strip = 1;     /* remove vlan tag from VIC port by default */
662           else
663             clib_warning ("VLAN strip disabled for interface\n");
664         }
665       else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
666         vlan_strip = 1;
667
668       if (vlan_strip)
669         {
670           int vlan_off;
671           vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
672           vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
673           xd->port_conf.rxmode.hw_vlan_strip = vlan_off;
674           if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
675             clib_warning ("VLAN strip enabled for interface\n");
676           else
677             clib_warning ("VLAN strip cannot be supported by interface\n");
678         }
679
680       hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
681         xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
682
683       rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
684     }
685
686   if (nb_desc > dm->conf->num_mbufs)
687     clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
688                   dm->conf->num_mbufs, nb_desc);
689
690   return 0;
691 }
692
693 static void
694 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
695 {
696   vlib_pci_main_t *pm = &pci_main;
697   clib_error_t *error;
698   vlib_pci_device_t *d;
699   u8 *pci_addr = 0;
700   int num_whitelisted = vec_len (conf->dev_confs);
701
702   /* *INDENT-OFF* */
703   pool_foreach (d, pm->pci_devs, ({
704     dpdk_device_config_t * devconf = 0;
705     vec_reset_length (pci_addr);
706     pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
707
708     if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
709       continue;
710
711     if (num_whitelisted)
712       {
713         uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
714
715         if (!p)
716           continue;
717
718         devconf = pool_elt_at_index (conf->dev_confs, p[0]);
719       }
720
721     /* virtio */
722     if (d->vendor_id == 0x1af4 && d->device_id == 0x1000)
723       ;
724     /* vmxnet3 */
725     else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
726       ;
727     /* all Intel network devices */
728     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_NETWORK_ETHERNET)
729       ;
730     /* all Intel QAT devices VFs */
731     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_PROCESSOR_CO &&
732         (d->device_id == 0x0443 || d->device_id == 0x37c9 || d->device_id == 0x19e3))
733       ;
734     /* Cisco VIC */
735     else if (d->vendor_id == 0x1137 && d->device_id == 0x0043)
736       ;
737     /* Chelsio T4/T5 */
738     else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
739       ;
740     else
741       {
742         clib_warning ("Unsupported PCI device 0x%04x:0x%04x found "
743                       "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
744                       pci_addr);
745         continue;
746       }
747
748     error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
749
750     if (error)
751       {
752         if (devconf == 0)
753           {
754             pool_get (conf->dev_confs, devconf);
755             hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
756                       devconf - conf->dev_confs);
757             devconf->pci_addr.as_u32 = d->bus_address.as_u32;
758           }
759         devconf->is_blacklisted = 1;
760         clib_error_report (error);
761       }
762   }));
763   /* *INDENT-ON* */
764   vec_free (pci_addr);
765 }
766
767 static clib_error_t *
768 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
769                     unformat_input_t * input, u8 is_default)
770 {
771   clib_error_t *error = 0;
772   uword *p;
773   dpdk_device_config_t *devconf;
774   unformat_input_t sub_input;
775
776   if (is_default)
777     {
778       devconf = &conf->default_devconf;
779     }
780   else
781     {
782       p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
783
784       if (!p)
785         {
786           pool_get (conf->dev_confs, devconf);
787           hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
788                     devconf - conf->dev_confs);
789         }
790       else
791         return clib_error_return (0,
792                                   "duplicate configuration for PCI address %U",
793                                   format_vlib_pci_addr, &pci_addr);
794     }
795
796   devconf->pci_addr.as_u32 = pci_addr.as_u32;
797   devconf->hqos_enabled = 0;
798   dpdk_device_config_hqos_default (&devconf->hqos);
799
800   if (!input)
801     return 0;
802
803   unformat_skip_white_space (input);
804   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
805     {
806       if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
807         ;
808       else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
809         ;
810       else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
811         ;
812       else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
813         ;
814       else if (unformat (input, "workers %U", unformat_bitmap_list,
815                          &devconf->workers))
816         ;
817       else
818         if (unformat
819             (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
820         {
821           error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
822           if (error)
823             break;
824         }
825       else if (unformat (input, "vlan-strip-offload off"))
826         devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
827       else if (unformat (input, "vlan-strip-offload on"))
828         devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
829       else
830         if (unformat
831             (input, "hqos %U", unformat_vlib_cli_sub_input, &sub_input))
832         {
833           devconf->hqos_enabled = 1;
834           error = unformat_hqos (&sub_input, &devconf->hqos);
835           if (error)
836             break;
837         }
838       else if (unformat (input, "hqos"))
839         {
840           devconf->hqos_enabled = 1;
841         }
842       else
843         {
844           error = clib_error_return (0, "unknown input `%U'",
845                                      format_unformat_error, input);
846           break;
847         }
848     }
849
850   if (error)
851     return error;
852
853   if (devconf->workers && devconf->num_rx_queues == 0)
854     devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
855   else if (devconf->workers &&
856            clib_bitmap_count_set_bits (devconf->workers) !=
857            devconf->num_rx_queues)
858     error =
859       clib_error_return (0,
860                          "%U: number of worker threadds must be "
861                          "equal to number of rx queues", format_vlib_pci_addr,
862                          &pci_addr);
863
864   return error;
865 }
866
867 static clib_error_t *
868 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
869 {
870   clib_error_t *error = 0;
871   dpdk_main_t *dm = &dpdk_main;
872   dpdk_config_main_t *conf = &dpdk_config_main;
873   vlib_thread_main_t *tm = vlib_get_thread_main ();
874   dpdk_device_config_t *devconf;
875   vlib_pci_addr_t pci_addr;
876   unformat_input_t sub_input;
877   u8 *s, *tmp = 0;
878   u8 *rte_cmd = 0, *ethname = 0;
879   u32 log_level;
880   int ret, i;
881   int num_whitelisted = 0;
882   u8 no_pci = 0;
883   u8 no_huge = 0;
884   u8 huge_dir = 0;
885   u8 file_prefix = 0;
886   u8 *socket_mem = 0;
887
888   conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
889
890   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
891     {
892       /* Prime the pump */
893       if (unformat (input, "no-hugetlb"))
894         {
895           vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
896           no_huge = 1;
897         }
898
899       else if (unformat (input, "enable-tcp-udp-checksum"))
900         conf->enable_tcp_udp_checksum = 1;
901
902       else if (unformat (input, "decimal-interface-names"))
903         conf->interface_name_format_decimal = 1;
904
905       else if (unformat (input, "no-multi-seg"))
906         conf->no_multi_seg = 1;
907
908       else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
909                          &sub_input))
910         {
911           error =
912             dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
913                                 1);
914
915           if (error)
916             return error;
917         }
918       else
919         if (unformat
920             (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
921              unformat_vlib_cli_sub_input, &sub_input))
922         {
923           error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
924
925           if (error)
926             return error;
927
928           num_whitelisted++;
929         }
930       else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
931         {
932           error = dpdk_device_config (conf, pci_addr, 0, 0);
933
934           if (error)
935             return error;
936
937           num_whitelisted++;
938         }
939       else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
940         ;
941       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
942         ;
943       else if (unformat (input, "socket-mem %s", &socket_mem))
944         ;
945       else if (unformat (input, "no-pci"))
946         {
947           no_pci = 1;
948           tmp = format (0, "--no-pci%c", 0);
949           vec_add1 (conf->eal_init_args, tmp);
950         }
951       else if (unformat (input, "poll-sleep %d", &dm->poll_sleep_usec))
952         ;
953
954 #define _(a)                                    \
955       else if (unformat(input, #a))             \
956         {                                       \
957           tmp = format (0, "--%s%c", #a, 0);    \
958           vec_add1 (conf->eal_init_args, tmp);    \
959         }
960       foreach_eal_double_hyphen_predicate_arg
961 #undef _
962 #define _(a)                                          \
963         else if (unformat(input, #a " %s", &s))       \
964           {                                           \
965             if (!strncmp(#a, "huge-dir", 8))          \
966               huge_dir = 1;                           \
967             else if (!strncmp(#a, "file-prefix", 11)) \
968               file_prefix = 1;                        \
969             tmp = format (0, "--%s%c", #a, 0);        \
970             vec_add1 (conf->eal_init_args, tmp);      \
971             vec_add1 (s, 0);                          \
972             if (!strncmp(#a, "vdev", 4))              \
973               if (strstr((char*)s, "af_packet"))      \
974                 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
975             vec_add1 (conf->eal_init_args, s);        \
976           }
977         foreach_eal_double_hyphen_arg
978 #undef _
979 #define _(a,b)                                          \
980           else if (unformat(input, #a " %s", &s))       \
981             {                                           \
982               tmp = format (0, "-%s%c", #b, 0);         \
983               vec_add1 (conf->eal_init_args, tmp);      \
984               vec_add1 (s, 0);                          \
985               vec_add1 (conf->eal_init_args, s);        \
986             }
987         foreach_eal_single_hyphen_arg
988 #undef _
989 #define _(a,b)                                          \
990             else if (unformat(input, #a " %s", &s))     \
991               {                                         \
992                 tmp = format (0, "-%s%c", #b, 0);       \
993                 vec_add1 (conf->eal_init_args, tmp);    \
994                 vec_add1 (s, 0);                        \
995                 vec_add1 (conf->eal_init_args, s);      \
996                 conf->a##_set_manually = 1;             \
997               }
998         foreach_eal_single_hyphen_mandatory_arg
999 #undef _
1000         else if (unformat (input, "default"))
1001         ;
1002
1003       else if (unformat_skip_white_space (input))
1004         ;
1005       else
1006         {
1007           error = clib_error_return (0, "unknown input `%U'",
1008                                      format_unformat_error, input);
1009           goto done;
1010         }
1011     }
1012
1013   if (!conf->uio_driver_name)
1014     conf->uio_driver_name = format (0, "uio_pci_generic%c", 0);
1015
1016   /*
1017    * Use 1G huge pages if available.
1018    */
1019   if (!no_huge && !huge_dir)
1020     {
1021       u32 x, *mem_by_socket = 0;
1022       uword c = 0;
1023       u8 use_1g = 1;
1024       u8 use_2m = 1;
1025       u8 less_than_1g = 1;
1026       int rv;
1027
1028       umount (DEFAULT_HUGE_DIR);
1029
1030       /* Process "socket-mem" parameter value */
1031       if (vec_len (socket_mem))
1032         {
1033           unformat_input_t in;
1034           unformat_init_vector (&in, socket_mem);
1035           while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1036             {
1037               if (unformat (&in, "%u,", &x))
1038                 ;
1039               else if (unformat (&in, "%u", &x))
1040                 ;
1041               else if (unformat (&in, ","))
1042                 x = 0;
1043               else
1044                 break;
1045
1046               vec_add1 (mem_by_socket, x);
1047
1048               if (x > 1023)
1049                 less_than_1g = 0;
1050             }
1051           /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1052           unformat_free (&in);
1053           socket_mem = 0;
1054         }
1055       else
1056         {
1057           /* *INDENT-OFF* */
1058           clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1059             {
1060               vec_validate(mem_by_socket, c);
1061               mem_by_socket[c] = 256; /* default per-socket mem */
1062             }
1063           ));
1064           /* *INDENT-ON* */
1065         }
1066
1067       /* check if available enough 1GB pages for each socket */
1068       /* *INDENT-OFF* */
1069       clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1070         {
1071           int pages_avail, page_size, mem;
1072
1073           vec_validate(mem_by_socket, c);
1074           mem = mem_by_socket[c];
1075
1076           page_size = 1024;
1077           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1078
1079           if (pages_avail < 0 || page_size * pages_avail < mem)
1080             use_1g = 0;
1081
1082           page_size = 2;
1083           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1084
1085           if (pages_avail < 0 || page_size * pages_avail < mem)
1086             use_2m = 0;
1087       }));
1088       /* *INDENT-ON* */
1089
1090       if (mem_by_socket == 0)
1091         {
1092           error = clib_error_return (0, "mem_by_socket NULL");
1093           goto done;
1094         }
1095       _vec_len (mem_by_socket) = c + 1;
1096
1097       /* regenerate socket_mem string */
1098       vec_foreach_index (x, mem_by_socket)
1099         socket_mem = format (socket_mem, "%s%u",
1100                              socket_mem ? "," : "", mem_by_socket[x]);
1101       socket_mem = format (socket_mem, "%c", 0);
1102
1103       vec_free (mem_by_socket);
1104
1105       rv = mkdir (VPP_RUN_DIR, 0755);
1106       if (rv && errno != EEXIST)
1107         {
1108           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1109                                      VPP_RUN_DIR, errno);
1110           goto done;
1111         }
1112
1113       rv = mkdir (DEFAULT_HUGE_DIR, 0755);
1114       if (rv && errno != EEXIST)
1115         {
1116           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1117                                      DEFAULT_HUGE_DIR, errno);
1118           goto done;
1119         }
1120
1121       if (use_1g && !(less_than_1g && use_2m))
1122         {
1123           rv =
1124             mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1125         }
1126       else if (use_2m)
1127         {
1128           rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1129         }
1130       else
1131         {
1132           return clib_error_return (0, "not enough free huge pages");
1133         }
1134
1135       if (rv)
1136         {
1137           error = clib_error_return (0, "mount failed %d", errno);
1138           goto done;
1139         }
1140
1141       tmp = format (0, "--huge-dir%c", 0);
1142       vec_add1 (conf->eal_init_args, tmp);
1143       tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1144       vec_add1 (conf->eal_init_args, tmp);
1145       if (!file_prefix)
1146         {
1147           tmp = format (0, "--file-prefix%c", 0);
1148           vec_add1 (conf->eal_init_args, tmp);
1149           tmp = format (0, "vpp%c", 0);
1150           vec_add1 (conf->eal_init_args, tmp);
1151         }
1152     }
1153
1154   vec_free (rte_cmd);
1155   vec_free (ethname);
1156
1157   if (error)
1158     return error;
1159
1160   /* I'll bet that -c and -n must be the first and second args... */
1161   if (!conf->coremask_set_manually)
1162     {
1163       vlib_thread_registration_t *tr;
1164       uword *coremask = 0;
1165       int i;
1166
1167       /* main thread core */
1168       coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1169
1170       for (i = 0; i < vec_len (tm->registrations); i++)
1171         {
1172           tr = tm->registrations[i];
1173           coremask = clib_bitmap_or (coremask, tr->coremask);
1174         }
1175
1176       vec_insert (conf->eal_init_args, 2, 1);
1177       conf->eal_init_args[1] = (u8 *) "-c";
1178       tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1179       conf->eal_init_args[2] = tmp;
1180       clib_bitmap_free (coremask);
1181     }
1182
1183   if (!conf->nchannels_set_manually)
1184     {
1185       vec_insert (conf->eal_init_args, 2, 3);
1186       conf->eal_init_args[3] = (u8 *) "-n";
1187       tmp = format (0, "%d", conf->nchannels);
1188       conf->eal_init_args[4] = tmp;
1189     }
1190
1191   if (no_pci == 0 && geteuid () == 0)
1192     dpdk_bind_devices_to_uio (conf);
1193
1194 #define _(x) \
1195     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1196       devconf->x = conf->default_devconf.x ;
1197
1198   /* *INDENT-OFF* */
1199   pool_foreach (devconf, conf->dev_confs, ({
1200
1201     /* default per-device config items */
1202     foreach_dpdk_device_config_item
1203
1204     /* add DPDK EAL whitelist/blacklist entry */
1205     if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1206       {
1207         tmp = format (0, "-w%c", 0);
1208         vec_add1 (conf->eal_init_args, tmp);
1209         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1210         vec_add1 (conf->eal_init_args, tmp);
1211       }
1212     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1213       {
1214         tmp = format (0, "-b%c", 0);
1215         vec_add1 (conf->eal_init_args, tmp);
1216         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1217         vec_add1 (conf->eal_init_args, tmp);
1218       }
1219   }));
1220   /* *INDENT-ON* */
1221
1222 #undef _
1223
1224   /* set master-lcore */
1225   tmp = format (0, "--master-lcore%c", 0);
1226   vec_add1 (conf->eal_init_args, tmp);
1227   tmp = format (0, "%u%c", tm->main_lcore, 0);
1228   vec_add1 (conf->eal_init_args, tmp);
1229
1230   /* set socket-mem */
1231   tmp = format (0, "--socket-mem%c", 0);
1232   vec_add1 (conf->eal_init_args, tmp);
1233   tmp = format (0, "%s%c", socket_mem, 0);
1234   vec_add1 (conf->eal_init_args, tmp);
1235
1236   /* NULL terminate the "argv" vector, in case of stupidity */
1237   vec_add1 (conf->eal_init_args, 0);
1238   _vec_len (conf->eal_init_args) -= 1;
1239
1240   /* Set up DPDK eal and packet mbuf pool early. */
1241
1242   log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1243
1244   rte_set_log_level (log_level);
1245
1246   vm = vlib_get_main ();
1247
1248   /* make copy of args as rte_eal_init tends to mess up with arg array */
1249   for (i = 1; i < vec_len (conf->eal_init_args); i++)
1250     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1251                                       conf->eal_init_args[i]);
1252
1253   ret =
1254     rte_eal_init (vec_len (conf->eal_init_args),
1255                   (char **) conf->eal_init_args);
1256
1257   /* lazy umount hugepages */
1258   umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
1259
1260   if (ret < 0)
1261     return clib_error_return (0, "rte_eal_init returned %d", ret);
1262
1263   /* Dump the physical memory layout prior to creating the mbuf_pool */
1264   fprintf (stdout, "DPDK physical memory layout:\n");
1265   rte_dump_physmem_layout (stdout);
1266
1267   /* main thread 1st */
1268   error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
1269   if (error)
1270     return error;
1271
1272   for (i = 0; i < RTE_MAX_LCORE; i++)
1273     {
1274       error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1275                                        rte_lcore_to_socket_id (i));
1276       if (error)
1277         return error;
1278     }
1279
1280 done:
1281   return error;
1282 }
1283
1284 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1285
1286 void
1287 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1288 {
1289   vnet_main_t *vnm = vnet_get_main ();
1290   struct rte_eth_link prev_link = xd->link;
1291   u32 hw_flags = 0;
1292   u8 hw_flags_chg = 0;
1293
1294   /* only update link state for PMD interfaces */
1295   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1296     return;
1297
1298   xd->time_last_link_update = now ? now : xd->time_last_link_update;
1299   memset (&xd->link, 0, sizeof (xd->link));
1300   rte_eth_link_get_nowait (xd->device_index, &xd->link);
1301
1302   if (LINK_STATE_ELOGS)
1303     {
1304       vlib_main_t *vm = vlib_get_main ();
1305       ELOG_TYPE_DECLARE (e) =
1306       {
1307       .format =
1308           "update-link-state: sw_if_index %d, admin_up %d,"
1309           "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1310
1311       struct
1312       {
1313         u32 sw_if_index;
1314         u8 admin_up;
1315         u8 old_link_state;
1316         u8 new_link_state;
1317       } *ed;
1318       ed = ELOG_DATA (&vm->elog_main, e);
1319       ed->sw_if_index = xd->vlib_sw_if_index;
1320       ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1321       ed->old_link_state = (u8)
1322         vnet_hw_interface_is_link_up (vnm, xd->hw_if_index);
1323       ed->new_link_state = (u8) xd->link.link_status;
1324     }
1325
1326   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
1327       ((xd->link.link_status != 0) ^
1328        vnet_hw_interface_is_link_up (vnm, xd->hw_if_index)))
1329     {
1330       hw_flags_chg = 1;
1331       hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1332     }
1333
1334   if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1335     {
1336       hw_flags_chg = 1;
1337       switch (xd->link.link_duplex)
1338         {
1339         case ETH_LINK_HALF_DUPLEX:
1340           hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1341           break;
1342         case ETH_LINK_FULL_DUPLEX:
1343           hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1344           break;
1345         default:
1346           break;
1347         }
1348     }
1349   if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1350     {
1351       hw_flags_chg = 1;
1352       switch (xd->link.link_speed)
1353         {
1354         case ETH_SPEED_NUM_10M:
1355           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1356           break;
1357         case ETH_SPEED_NUM_100M:
1358           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1359           break;
1360         case ETH_SPEED_NUM_1G:
1361           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1362           break;
1363         case ETH_SPEED_NUM_10G:
1364           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1365           break;
1366         case ETH_SPEED_NUM_40G:
1367           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1368           break;
1369         case 0:
1370           break;
1371         default:
1372           clib_warning ("unknown link speed %d", xd->link.link_speed);
1373           break;
1374         }
1375     }
1376   if (hw_flags_chg)
1377     {
1378       if (LINK_STATE_ELOGS)
1379         {
1380           vlib_main_t *vm = vlib_get_main ();
1381
1382           ELOG_TYPE_DECLARE (e) =
1383           {
1384           .format =
1385               "update-link-state: sw_if_index %d, new flags %d",.format_args
1386               = "i4i4",};
1387
1388           struct
1389           {
1390             u32 sw_if_index;
1391             u32 flags;
1392           } *ed;
1393           ed = ELOG_DATA (&vm->elog_main, e);
1394           ed->sw_if_index = xd->vlib_sw_if_index;
1395           ed->flags = hw_flags;
1396         }
1397       vnet_hw_interface_set_flags (vnm, xd->hw_if_index, hw_flags);
1398     }
1399 }
1400
1401 static uword
1402 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1403 {
1404   clib_error_t *error;
1405   vnet_main_t *vnm = vnet_get_main ();
1406   dpdk_main_t *dm = &dpdk_main;
1407   ethernet_main_t *em = &ethernet_main;
1408   dpdk_device_t *xd;
1409   vlib_thread_main_t *tm = vlib_get_thread_main ();
1410   int i;
1411
1412   error = dpdk_lib_init (dm);
1413
1414   if (error)
1415     clib_error_report (error);
1416
1417   tm->worker_thread_release = 1;
1418
1419   f64 now = vlib_time_now (vm);
1420   vec_foreach (xd, dm->devices)
1421   {
1422     dpdk_update_link_state (xd, now);
1423   }
1424
1425   {
1426     /*
1427      * Extra set up for bond interfaces:
1428      *  1. Setup MACs for bond interfaces and their slave links which was set
1429      *     in dpdk_device_setup() but needs to be done again here to take effect.
1430      *  2. Set up info for bond interface related CLI support.
1431      */
1432     int nports = rte_eth_dev_count ();
1433     if (nports > 0)
1434       {
1435         for (i = 0; i < nports; i++)
1436           {
1437             xd = &dm->devices[i];
1438             ASSERT (i == xd->device_index);
1439             if (xd->pmd == VNET_DPDK_PMD_BOND)
1440               {
1441                 u8 addr[6];
1442                 u8 slink[16];
1443                 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1444                 if (nlink > 0)
1445                   {
1446                     vnet_hw_interface_t *bhi;
1447                     ethernet_interface_t *bei;
1448                     int rv;
1449
1450                     /* Get MAC of 1st slave link */
1451                     rte_eth_macaddr_get
1452                       (slink[0], (struct ether_addr *) addr);
1453
1454                     /* Set MAC of bounded interface to that of 1st slave link */
1455                     clib_warning ("Set MAC for bond dev# %d", i);
1456                     rv = rte_eth_bond_mac_address_set
1457                       (i, (struct ether_addr *) addr);
1458                     if (rv)
1459                       clib_warning ("Set MAC addr failure rv=%d", rv);
1460
1461                     /* Populate MAC of bonded interface in VPP hw tables */
1462                     bhi = vnet_get_hw_interface
1463                       (vnm, dm->devices[i].hw_if_index);
1464                     bei = pool_elt_at_index
1465                       (em->interfaces, bhi->hw_instance);
1466                     clib_memcpy (bhi->hw_address, addr, 6);
1467                     clib_memcpy (bei->address, addr, 6);
1468
1469                     /* Init l3 packet size allowed on bonded interface */
1470                     bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
1471                     bhi->max_l3_packet_bytes[VLIB_RX] =
1472                       bhi->max_l3_packet_bytes[VLIB_TX] =
1473                       ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1474                     while (nlink >= 1)
1475                       {         /* for all slave links */
1476                         int slave = slink[--nlink];
1477                         dpdk_device_t *sdev = &dm->devices[slave];
1478                         vnet_hw_interface_t *shi;
1479                         vnet_sw_interface_t *ssi;
1480                         ethernet_interface_t *sei;
1481                         /* Add MAC to all slave links except the first one */
1482                         if (nlink)
1483                           {
1484                             clib_warning ("Add MAC for slave dev# %d", slave);
1485                             rv = rte_eth_dev_mac_addr_add
1486                               (slave, (struct ether_addr *) addr, 0);
1487                             if (rv)
1488                               clib_warning ("Add MAC addr failure rv=%d", rv);
1489                           }
1490                         /* Set slaves bitmap for bonded interface */
1491                         bhi->bond_info = clib_bitmap_set
1492                           (bhi->bond_info, sdev->hw_if_index, 1);
1493                         /* Set slave link flags on slave interface */
1494                         shi = vnet_get_hw_interface (vnm, sdev->hw_if_index);
1495                         ssi = vnet_get_sw_interface
1496                           (vnm, sdev->vlib_sw_if_index);
1497                         sei = pool_elt_at_index
1498                           (em->interfaces, shi->hw_instance);
1499
1500                         shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1501                         ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1502                         clib_memcpy (shi->hw_address, addr, 6);
1503                         clib_memcpy (sei->address, addr, 6);
1504
1505                         /* Set l3 packet size allowed as the lowest of slave */
1506                         if (bhi->max_l3_packet_bytes[VLIB_RX] >
1507                             shi->max_l3_packet_bytes[VLIB_RX])
1508                           bhi->max_l3_packet_bytes[VLIB_RX] =
1509                             bhi->max_l3_packet_bytes[VLIB_TX] =
1510                             shi->max_l3_packet_bytes[VLIB_RX];
1511
1512                         /* Set max packet size allowed as the lowest of slave */
1513                         if (bhi->max_packet_bytes > shi->max_packet_bytes)
1514                           bhi->max_packet_bytes = shi->max_packet_bytes;
1515                       }
1516                   }
1517               }
1518           }
1519       }
1520   }
1521
1522   while (1)
1523     {
1524       /*
1525        * check each time through the loop in case intervals are changed
1526        */
1527       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1528         dm->link_state_poll_interval : dm->stat_poll_interval;
1529
1530       vlib_process_wait_for_event_or_clock (vm, min_wait);
1531
1532       if (dm->admin_up_down_in_progress)
1533         /* skip the poll if an admin up down is in progress (on any interface) */
1534         continue;
1535
1536       vec_foreach (xd, dm->devices)
1537       {
1538         f64 now = vlib_time_now (vm);
1539         if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1540           dpdk_update_counters (xd, now);
1541         if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1542           dpdk_update_link_state (xd, now);
1543
1544       }
1545     }
1546
1547   return 0;
1548 }
1549
1550 /* *INDENT-OFF* */
1551 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1552     .function = dpdk_process,
1553     .type = VLIB_NODE_TYPE_PROCESS,
1554     .name = "dpdk-process",
1555     .process_log2_n_stack_bytes = 17,
1556 };
1557 /* *INDENT-ON* */
1558
1559 static clib_error_t *
1560 dpdk_init (vlib_main_t * vm)
1561 {
1562   dpdk_main_t *dm = &dpdk_main;
1563   vlib_node_t *ei;
1564   clib_error_t *error = 0;
1565   vlib_thread_main_t *tm = vlib_get_thread_main ();
1566
1567   /* verify that structs are cacheline aligned */
1568   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1569                  "Cache line marker must be 1st element in dpdk_device_t");
1570   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1571                  CLIB_CACHE_LINE_BYTES,
1572                  "Data in cache line 0 is bigger than cache line size");
1573   STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1574                  "Cache line marker must be 1st element in frame_queue_trace_t");
1575
1576   dm->vlib_main = vm;
1577   dm->vnet_main = vnet_get_main ();
1578   dm->conf = &dpdk_config_main;
1579
1580   ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1581   if (ei == 0)
1582     return clib_error_return (0, "ethernet-input node AWOL");
1583
1584   dm->ethernet_input_node_index = ei->index;
1585
1586   dm->conf->nchannels = 4;
1587   dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1588   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1589
1590   vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1591
1592   /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1593   dm->buffer_flags_template =
1594     (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_EXT_HDR_VALID
1595      | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
1596
1597   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1598   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1599
1600   /* init CLI */
1601   if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1602     return error;
1603
1604   return error;
1605 }
1606
1607 VLIB_INIT_FUNCTION (dpdk_init);
1608
1609
1610 /*
1611  * fd.io coding-style-patch-verification: ON
1612  *
1613  * Local Variables:
1614  * eval: (c-set-style "gnu")
1615  * End:
1616  */