Fix init of ENIC driver to allow receiving of jumbo packets
[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 #include <vlibapi/api.h>
39 #include <vlibmemory/api.h>
40
41 /* define message IDs */
42 #include <dpdk/api/dpdk_msg_enum.h>
43
44 #define vl_typedefs             /* define message structures */
45 #include <dpdk/api/dpdk_all_api_h.h>
46 #undef vl_typedefs
47
48 #define vl_endianfun            /* define message structures */
49 #include <dpdk/api/dpdk_all_api_h.h>
50 #undef vl_endianfun
51
52 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
53
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>
57 #undef vl_api_version
58
59 /* Macro to finish up custom dump fns */
60 #define FINISH                                  \
61     vec_add1 (s, 0);                            \
62     vl_print (handle, (char *)s);               \
63     vec_free (s);                               \
64     return handle;
65
66 #include <vlibapi/api_helper_macros.h>
67
68 static void
69   vl_api_sw_interface_set_dpdk_hqos_pipe_t_handler
70   (vl_api_sw_interface_set_dpdk_hqos_pipe_t * mp)
71 {
72   vl_api_sw_interface_set_dpdk_hqos_pipe_reply_t *rmp;
73   int rv = 0;
74
75   dpdk_main_t *dm = &dpdk_main;
76   dpdk_device_t *xd;
77
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;
83
84   VALIDATE_SW_IF_INDEX (mp);
85
86   /* hw_if & dpdk device */
87   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
88
89   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
90
91   rv = rte_sched_pipe_config (xd->hqos_ht->hqos, subport, pipe, profile);
92
93   BAD_SW_IF_INDEX_LABEL;
94
95   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_PIPE_REPLY);
96 }
97
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)
100 {
101   u8 *s;
102
103   s = format (0, "SCRIPT: sw_interface_set_dpdk_hqos_pipe ");
104
105   s = format (s, "sw_if_index %u ", ntohl (mp->sw_if_index));
106
107   s = format (s, "subport %u  pipe %u  profile %u ",
108               ntohl (mp->subport), ntohl (mp->pipe), ntohl (mp->profile));
109
110   FINISH;
111 }
112
113 static void
114   vl_api_sw_interface_set_dpdk_hqos_subport_t_handler
115   (vl_api_sw_interface_set_dpdk_hqos_subport_t * mp)
116 {
117   vl_api_sw_interface_set_dpdk_hqos_subport_reply_t *rmp;
118   int rv = 0;
119
120   dpdk_main_t *dm = &dpdk_main;
121   dpdk_device_t *xd;
122   struct rte_sched_subport_params p;
123
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);
133
134   vnet_hw_interface_t *hw;
135
136   VALIDATE_SW_IF_INDEX (mp);
137
138   /* hw_if & dpdk device */
139   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
140
141   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
142
143   rv = rte_sched_subport_config (xd->hqos_ht->hqos, subport, &p);
144
145   BAD_SW_IF_INDEX_LABEL;
146
147   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_SUBPORT_REPLY);
148 }
149
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)
152 {
153   u8 *s;
154
155   s = format (0, "SCRIPT: sw_interface_set_dpdk_hqos_subport ");
156
157   s = format (s, "sw_if_index %u ", ntohl (mp->sw_if_index));
158
159   s =
160     format (s,
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));
166
167   FINISH;
168 }
169
170 static void
171   vl_api_sw_interface_set_dpdk_hqos_tctbl_t_handler
172   (vl_api_sw_interface_set_dpdk_hqos_tctbl_t * mp)
173 {
174   vl_api_sw_interface_set_dpdk_hqos_tctbl_reply_t *rmp;
175   int rv = 0;
176
177   dpdk_main_t *dm = &dpdk_main;
178   vlib_thread_main_t *tm = vlib_get_thread_main ();
179   dpdk_device_t *xd;
180
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);
185   u32 val, i;
186
187   vnet_hw_interface_t *hw;
188
189   VALIDATE_SW_IF_INDEX (mp);
190
191   /* hw_if & dpdk device */
192   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
193
194   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
195
196   if (tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
197     {
198       clib_warning ("invalid traffic class !!");
199       rv = VNET_API_ERROR_INVALID_VALUE;
200       goto done;
201     }
202   if (queue >= RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
203     {
204       clib_warning ("invalid queue !!");
205       rv = VNET_API_ERROR_INVALID_VALUE;
206       goto done;
207     }
208
209   /* Detect the set of worker threads */
210   uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
211
212   if (p == 0)
213     {
214       clib_warning ("worker thread registration AWOL !!");
215       rv = VNET_API_ERROR_INVALID_VALUE_2;
216       goto done;
217     }
218
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;
222
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;
226
227   BAD_SW_IF_INDEX_LABEL;
228 done:
229
230   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_TCTBL_REPLY);
231 }
232
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)
235 {
236   u8 *s;
237
238   s = format (0, "SCRIPT: sw_interface_set_dpdk_hqos_tctbl ");
239
240   s = format (s, "sw_if_index %u ", ntohl (mp->sw_if_index));
241
242   s = format (s, "entry %u  tc %u  queue %u",
243               ntohl (mp->entry), ntohl (mp->tc), ntohl (mp->queue));
244
245   FINISH;
246 }
247
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)
252
253 /* Set up the API message handling tables */
254 static clib_error_t *
255 dpdk_plugin_api_hookup (vlib_main_t * vm)
256 {
257   dpdk_main_t *dm __attribute__ ((unused)) = &dpdk_main;
258 #define _(N,n)                                                  \
259     vl_msg_api_set_handlers((VL_API_##N + dm->msg_id_base),     \
260                            #n,          \
261                            vl_api_##n##_t_handler,              \
262                            vl_noop_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;
267 #undef _
268   return 0;
269 }
270
271 #define vl_msg_name_crc_list
272 #include <dpdk/api/dpdk_all_api_h.h>
273 #undef vl_msg_name_crc_list
274
275 static void
276 setup_message_id_table (dpdk_main_t * dm, api_main_t * am)
277 {
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;
281 #undef _
282 }
283
284 //  TODO
285 /*
286 static void plugin_custom_dump_configure (dpdk_main_t * dm)
287 {
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;
292 #undef _
293 }
294 */
295 /* force linker to link functions used by vlib and declared weak */
296 void *vlib_weakly_linked_functions[] = {
297   &rte_pktmbuf_init,
298   &rte_pktmbuf_pool_init,
299 };
300
301 #define LINK_STATE_ELOGS        0
302
303 #define DEFAULT_HUGE_DIR "/run/vpp/hugepages"
304 #define VPP_RUN_DIR "/run/vpp"
305
306 /* Port configuration, mildly modified Intel app values */
307
308 static struct rte_eth_conf port_conf_template = {
309   .rxmode = {
310              .split_hdr_size = 0,
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 */
315              },
316   .txmode = {
317              .mq_mode = ETH_MQ_TX_NONE,
318              },
319 };
320
321 clib_error_t *
322 dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd)
323 {
324   int rv;
325   int j;
326
327   ASSERT (vlib_get_thread_index () == 0);
328
329   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
330     {
331       vnet_hw_interface_set_flags (dm->vnet_main, xd->vlib_hw_if_index, 0);
332       rte_eth_dev_stop (xd->device_index);
333     }
334
335   rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
336                               xd->tx_q_used, &xd->port_conf);
337
338   if (rv < 0)
339     return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
340                               xd->device_index, rv);
341
342   /* Set up one TX-queue per worker thread */
343   for (j = 0; j < xd->tx_q_used; j++)
344     {
345       rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
346                                    xd->cpu_socket, &xd->tx_conf);
347
348       /* retry with any other CPU socket */
349       if (rv < 0)
350         rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
351                                      SOCKET_ID_ANY, &xd->tx_conf);
352       if (rv < 0)
353         break;
354     }
355
356   if (rv < 0)
357     return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
358                               xd->device_index, rv);
359
360   for (j = 0; j < xd->rx_q_used; j++)
361     {
362
363       rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
364                                    xd->cpu_socket, 0,
365                                    dm->
366                                    pktmbuf_pools[xd->cpu_socket_id_by_queue
367                                                  [j]]);
368
369       /* retry with any other CPU socket */
370       if (rv < 0)
371         rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
372                                      SOCKET_ID_ANY, 0,
373                                      dm->
374                                      pktmbuf_pools[xd->cpu_socket_id_by_queue
375                                                    [j]]);
376       if (rv < 0)
377         return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
378                                   xd->device_index, rv);
379     }
380
381   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
382     {
383       int rv;
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);
389       if (rv < 0)
390         clib_warning ("rte_eth_dev_start %d returned %d",
391                       xd->device_index, rv);
392     }
393   return 0;
394 }
395
396 static u32
397 dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
398 {
399   dpdk_main_t *dm = &dpdk_main;
400   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
401   u32 old = 0;
402
403   if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
404     {
405       old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
406
407       if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
408         xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
409       else
410         xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
411
412       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
413         {
414           if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
415             rte_eth_promiscuous_enable (xd->device_index);
416           else
417             rte_eth_promiscuous_disable (xd->device_index);
418         }
419     }
420   else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU (flags))
421     {
422       int rv;
423
424       xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
425
426       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
427         rte_eth_dev_stop (xd->device_index);
428
429       rv = rte_eth_dev_configure
430         (xd->device_index, xd->rx_q_used, xd->tx_q_used, &xd->port_conf);
431
432       if (rv < 0)
433         vlib_cli_output (vlib_get_main (),
434                          "rte_eth_dev_configure[%d]: err %d",
435                          xd->device_index, rv);
436
437       rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
438
439       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
440         {
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);
446           if (rv < 0)
447             clib_warning ("rte_eth_dev_start %d returned %d",
448                           xd->device_index, rv);
449         }
450
451     }
452   return old;
453 }
454
455 void
456 dpdk_device_lock_init (dpdk_device_t * xd)
457 {
458   int q;
459   vec_validate (xd->lockp, xd->tx_q_used - 1);
460   for (q = 0; q < xd->tx_q_used; q++)
461     {
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);
465     }
466 }
467
468 void
469 dpdk_device_lock_free (dpdk_device_t * xd)
470 {
471   int q;
472
473   for (q = 0; q < vec_len (xd->lockp); q++)
474     clib_mem_free ((void *) xd->lockp[q]);
475   vec_free (xd->lockp);
476   xd->lockp = 0;
477 }
478
479 static clib_error_t *
480 dpdk_lib_init (dpdk_main_t * dm)
481 {
482   u32 nports;
483   u32 nb_desc = 0;
484   int i;
485   clib_error_t *error;
486   vlib_main_t *vm = vlib_get_main ();
487   vlib_thread_main_t *tm = vlib_get_thread_main ();
488   vnet_sw_interface_t *sw;
489   vnet_hw_interface_t *hi;
490   dpdk_device_t *xd;
491   vlib_pci_addr_t last_pci_addr;
492   u32 last_pci_addr_port = 0;
493   vlib_thread_registration_t *tr, *tr_hqos;
494   uword *p, *p_hqos;
495
496   u32 next_cpu = 0, next_hqos_cpu = 0;
497   u8 af_packet_port_id = 0;
498   u8 bond_ether_port_id = 0;
499   last_pci_addr.as_u32 = ~0;
500
501   dm->input_cpu_first_index = 0;
502   dm->input_cpu_count = 1;
503
504   /* find out which cpus will be used for input */
505   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
506   tr = p ? (vlib_thread_registration_t *) p[0] : 0;
507
508   if (tr && tr->count > 0)
509     {
510       dm->input_cpu_first_index = tr->first_index;
511       dm->input_cpu_count = tr->count;
512     }
513
514   vec_validate_aligned (dm->devices_by_cpu, tm->n_vlib_mains - 1,
515                         CLIB_CACHE_LINE_BYTES);
516
517   dm->hqos_cpu_first_index = 0;
518   dm->hqos_cpu_count = 0;
519
520   /* find out which cpus will be used for I/O TX */
521   p_hqos = hash_get_mem (tm->thread_registrations_by_name, "hqos-threads");
522   tr_hqos = p_hqos ? (vlib_thread_registration_t *) p_hqos[0] : 0;
523
524   if (tr_hqos && tr_hqos->count > 0)
525     {
526       dm->hqos_cpu_first_index = tr_hqos->first_index;
527       dm->hqos_cpu_count = tr_hqos->count;
528     }
529
530   vec_validate_aligned (dm->devices_by_hqos_cpu, tm->n_vlib_mains - 1,
531                         CLIB_CACHE_LINE_BYTES);
532
533   nports = rte_eth_dev_count ();
534   if (nports < 1)
535     {
536       clib_warning ("DPDK drivers found no ports...");
537     }
538
539   if (CLIB_DEBUG > 0)
540     clib_warning ("DPDK drivers found %d ports...", nports);
541
542   /*
543    * All buffers are all allocated from the same rte_mempool.
544    * Thus they all have the same number of data bytes.
545    */
546   dm->vlib_buffer_free_list_index =
547     vlib_buffer_get_or_create_free_list (vm,
548                                          VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
549                                          "dpdk rx");
550
551   if (dm->conf->enable_tcp_udp_checksum)
552     dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
553                                    | IP_BUFFER_L4_CHECKSUM_COMPUTED);
554
555   /* vlib_buffer_t template */
556   vec_validate_aligned (dm->buffer_templates, tm->n_vlib_mains - 1,
557                         CLIB_CACHE_LINE_BYTES);
558   for (i = 0; i < tm->n_vlib_mains; i++)
559     {
560       vlib_buffer_free_list_t *fl;
561       vlib_buffer_t *bt = vec_elt_at_index (dm->buffer_templates, i);
562       fl = vlib_buffer_get_free_list (vm,
563                                       VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
564       vlib_buffer_init_for_free_list (bt, fl);
565       bt->flags = dm->buffer_flags_template;
566       bt->current_data = -RTE_PKTMBUF_HEADROOM;
567       vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0;
568     }
569
570   for (i = 0; i < nports; i++)
571     {
572       u8 addr[6];
573       u8 vlan_strip = 0;
574       int j;
575       struct rte_eth_dev_info dev_info;
576       clib_error_t *rv;
577       struct rte_eth_link l;
578       dpdk_device_config_t *devconf = 0;
579       vlib_pci_addr_t pci_addr;
580       uword *p = 0;
581
582       rte_eth_dev_info_get (i, &dev_info);
583       if (dev_info.pci_dev)     /* bonded interface has no pci info */
584         {
585           pci_addr.domain = dev_info.pci_dev->addr.domain;
586           pci_addr.bus = dev_info.pci_dev->addr.bus;
587           pci_addr.slot = dev_info.pci_dev->addr.devid;
588           pci_addr.function = dev_info.pci_dev->addr.function;
589           p =
590             hash_get (dm->conf->device_config_index_by_pci_addr,
591                       pci_addr.as_u32);
592         }
593
594       if (p)
595         devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
596       else
597         devconf = &dm->conf->default_devconf;
598
599       /* Create vnet interface */
600       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
601       xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
602       xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
603       xd->cpu_socket = (i8) rte_eth_dev_socket_id (i);
604
605       /* Handle interface naming for devices with multiple ports sharing same PCI ID */
606       if (dev_info.pci_dev)
607         {
608           struct rte_eth_dev_info di = { 0 };
609           rte_eth_dev_info_get (i + 1, &di);
610           if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
611               memcmp (&dev_info.pci_dev->addr, &di.pci_dev->addr,
612                       sizeof (struct rte_pci_addr)) == 0)
613             {
614               xd->interface_name_suffix = format (0, "0");
615               last_pci_addr.as_u32 = pci_addr.as_u32;
616               last_pci_addr_port = i;
617             }
618           else if (pci_addr.as_u32 == last_pci_addr.as_u32)
619             {
620               xd->interface_name_suffix =
621                 format (0, "%u", i - last_pci_addr_port);
622             }
623           else
624             {
625               last_pci_addr.as_u32 = ~0;
626             }
627         }
628       else
629         last_pci_addr.as_u32 = ~0;
630
631       clib_memcpy (&xd->tx_conf, &dev_info.default_txconf,
632                    sizeof (struct rte_eth_txconf));
633       if (dm->conf->no_multi_seg)
634         {
635           xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
636           port_conf_template.rxmode.jumbo_frame = 0;
637           port_conf_template.rxmode.enable_scatter = 0;
638         }
639       else
640         {
641           xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
642           port_conf_template.rxmode.jumbo_frame = 1;
643           port_conf_template.rxmode.enable_scatter = 1;
644           xd->flags |= DPDK_DEVICE_FLAG_MAYBE_MULTISEG;
645         }
646
647       clib_memcpy (&xd->port_conf, &port_conf_template,
648                    sizeof (struct rte_eth_conf));
649
650       xd->tx_q_used = clib_min (dev_info.max_tx_queues, tm->n_vlib_mains);
651
652       if (devconf->num_tx_queues > 0
653           && devconf->num_tx_queues < xd->tx_q_used)
654         xd->tx_q_used = clib_min (xd->tx_q_used, devconf->num_tx_queues);
655
656       if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
657         {
658           dm->use_rss = 1;
659         }
660
661       if (devconf->num_rx_queues > 1
662           && dev_info.max_rx_queues >= devconf->num_rx_queues)
663         {
664           xd->rx_q_used = devconf->num_rx_queues;
665           xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
666           if (devconf->rss_fn == 0)
667             xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
668               ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
669           else
670             xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
671         }
672       else
673         xd->rx_q_used = 1;
674
675       xd->flags |= DPDK_DEVICE_FLAG_PMD;
676
677       /* workaround for drivers not setting driver_name */
678       if ((!dev_info.driver_name) && (dev_info.pci_dev))
679         dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
680
681       ASSERT (dev_info.driver_name);
682
683       if (!xd->pmd)
684         {
685
686
687 #define _(s,f) else if (dev_info.driver_name &&                 \
688                         !strcmp(dev_info.driver_name, s))       \
689                  xd->pmd = VNET_DPDK_PMD_##f;
690           if (0)
691             ;
692           foreach_dpdk_pmd
693 #undef _
694             else
695             xd->pmd = VNET_DPDK_PMD_UNKNOWN;
696
697           xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
698           xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
699           xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
700
701           switch (xd->pmd)
702             {
703               /* 1G adapters */
704             case VNET_DPDK_PMD_E1000EM:
705             case VNET_DPDK_PMD_IGB:
706             case VNET_DPDK_PMD_IGBVF:
707               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
708               break;
709
710               /* 10G adapters */
711             case VNET_DPDK_PMD_IXGBE:
712             case VNET_DPDK_PMD_IXGBEVF:
713             case VNET_DPDK_PMD_THUNDERX:
714               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
715               break;
716             case VNET_DPDK_PMD_DPAA2:
717               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
718               break;
719
720               /* Cisco VIC */
721             case VNET_DPDK_PMD_ENIC:
722               rte_eth_link_get_nowait (i, &l);
723               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
724               if (l.link_speed == 40000)
725                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
726               else
727                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
728               break;
729
730               /* Intel Fortville */
731             case VNET_DPDK_PMD_I40E:
732             case VNET_DPDK_PMD_I40EVF:
733               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
734               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
735
736               switch (dev_info.pci_dev->id.device_id)
737                 {
738                 case I40E_DEV_ID_10G_BASE_T:
739                 case I40E_DEV_ID_SFP_XL710:
740                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
741                   break;
742                 case I40E_DEV_ID_QSFP_A:
743                 case I40E_DEV_ID_QSFP_B:
744                 case I40E_DEV_ID_QSFP_C:
745                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
746                   break;
747                 case I40E_DEV_ID_VF:
748                   rte_eth_link_get_nowait (i, &l);
749                   xd->port_type = l.link_speed == 10000 ?
750                     VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
751                   break;
752                 default:
753                   xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
754                 }
755               break;
756
757             case VNET_DPDK_PMD_CXGBE:
758               switch (dev_info.pci_dev->id.device_id)
759                 {
760                 case 0x540d:    /* T580-CR */
761                 case 0x5410:    /* T580-LP-cr */
762                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
763                   break;
764                 case 0x5403:    /* T540-CR */
765                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
766                   break;
767                 default:
768                   xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
769                 }
770               break;
771
772             case VNET_DPDK_PMD_MLX5:
773               {
774                 char *pn_100g[] = { "MCX415A-CCAT", "MCX416A-CCAT",
775                   "MCX556A-ECAT", "MCX556A-EDAT", "MCX555A-ECAT",
776                   "MCX515A-CCAT", "MCX516A-CCAT", "MCX516A-CDAT", 0
777                 };
778                 char *pn_40g[] = { "MCX413A-BCAT", "MCX414A-BCAT",
779                   "MCX415A-BCAT", "MCX416A-BCAT", "MCX4131A-BCAT", 0
780                 };
781                 char *pn_10g[] = { "MCX4111A-XCAT", "MCX4121A-XCAT", 0 };
782
783                 vlib_pci_device_t *pd = vlib_get_pci_device (&pci_addr);
784                 u8 *pn = 0;
785                 char **c;
786                 int found = 0;
787                 pn = format (0, "%U%c",
788                              format_vlib_pci_vpd, pd->vpd_r, "PN", 0);
789
790                 if (!pn)
791                   break;
792
793                 c = pn_100g;
794                 while (!found && c[0])
795                   {
796                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
797                       {
798                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_100G;
799                         break;
800                       }
801                     c++;
802                   }
803
804                 c = pn_40g;
805                 while (!found && c[0])
806                   {
807                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
808                       {
809                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
810                         break;
811                       }
812                     c++;
813                   }
814
815                 c = pn_10g;
816                 while (!found && c[0])
817                   {
818                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
819                       {
820                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
821                         break;
822                       }
823                     c++;
824                   }
825
826                 vec_free (pn);
827               }
828
829               break;
830               /* Intel Red Rock Canyon */
831             case VNET_DPDK_PMD_FM10K:
832               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
833               break;
834
835               /* virtio */
836             case VNET_DPDK_PMD_VIRTIO:
837               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
838               xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
839               xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
840               break;
841
842               /* vmxnet3 */
843             case VNET_DPDK_PMD_VMXNET3:
844               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
845               xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
846               break;
847
848             case VNET_DPDK_PMD_AF_PACKET:
849               xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
850               xd->port_id = af_packet_port_id++;
851               break;
852
853             case VNET_DPDK_PMD_BOND:
854               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
855               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
856               xd->port_id = bond_ether_port_id++;
857               break;
858
859             default:
860               xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
861             }
862
863           if (devconf->num_rx_desc)
864             xd->nb_rx_desc = devconf->num_rx_desc;
865
866           if (devconf->num_tx_desc)
867             xd->nb_tx_desc = devconf->num_tx_desc;
868         }
869
870       /*
871        * Ensure default mtu is not > the mtu read from the hardware.
872        * Otherwise rte_eth_dev_configure() will fail and the port will
873        * not be available.
874        */
875       if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
876         {
877           /*
878            * This device does not support the platforms's max frame
879            * size. Use it's advertised mru instead.
880            */
881           xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
882         }
883       else
884         {
885           xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
886
887           /*
888            * Some platforms do not account for Ethernet FCS (4 bytes) in
889            * MTU calculations. To interop with them increase mru but only
890            * if the device's settings can support it.
891            */
892           if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
893               xd->port_conf.rxmode.hw_strip_crc)
894             {
895               /*
896                * Allow additional 4 bytes (for Ethernet FCS). These bytes are
897                * stripped by h/w and so will not consume any buffer memory.
898                */
899               xd->port_conf.rxmode.max_rx_pkt_len += 4;
900             }
901         }
902
903       if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
904         {
905           f64 now = vlib_time_now (vm);
906           u32 rnd;
907           rnd = (u32) (now * 1e6);
908           rnd = random_u32 (&rnd);
909           clib_memcpy (addr + 2, &rnd, sizeof (rnd));
910           addr[0] = 2;
911           addr[1] = 0xfe;
912         }
913       else
914         rte_eth_macaddr_get (i, (struct ether_addr *) addr);
915
916       if (xd->tx_q_used < tm->n_vlib_mains)
917         dpdk_device_lock_init (xd);
918
919       xd->device_index = xd - dm->devices;
920       ASSERT (i == xd->device_index);
921       xd->per_interface_next_index = ~0;
922
923       /* assign interface to input thread */
924       dpdk_device_and_queue_t *dq;
925       int q;
926
927       if (devconf->workers)
928         {
929           int i;
930           q = 0;
931           /* *INDENT-OFF* */
932           clib_bitmap_foreach (i, devconf->workers, ({
933             int cpu = dm->input_cpu_first_index + i;
934             unsigned lcore = vlib_worker_threads[cpu].lcore_id;
935             vec_validate(xd->cpu_socket_id_by_queue, q);
936             xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
937             vec_add2(dm->devices_by_cpu[cpu], dq, 1);
938             dq->device = xd->device_index;
939             dq->queue_id = q++;
940           }));
941           /* *INDENT-ON* */
942         }
943       else
944         for (q = 0; q < xd->rx_q_used; q++)
945           {
946             int cpu = dm->input_cpu_first_index + next_cpu;
947             unsigned lcore = vlib_worker_threads[cpu].lcore_id;
948
949             /*
950              * numa node for worker thread handling this queue
951              * needed for taking buffers from the right mempool
952              */
953             vec_validate (xd->cpu_socket_id_by_queue, q);
954             xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id (lcore);
955
956             /*
957              * construct vector of (device,queue) pairs for each worker thread
958              */
959             vec_add2 (dm->devices_by_cpu[cpu], dq, 1);
960             dq->device = xd->device_index;
961             dq->queue_id = q;
962
963             next_cpu++;
964             if (next_cpu == dm->input_cpu_count)
965               next_cpu = 0;
966           }
967
968
969       if (devconf->hqos_enabled)
970         {
971           xd->flags |= DPDK_DEVICE_FLAG_HQOS;
972
973           if (devconf->hqos.hqos_thread_valid)
974             {
975               int cpu = dm->hqos_cpu_first_index + devconf->hqos.hqos_thread;
976
977               if (devconf->hqos.hqos_thread >= dm->hqos_cpu_count)
978                 return clib_error_return (0, "invalid HQoS thread index");
979
980               vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
981               dq->device = xd->device_index;
982               dq->queue_id = 0;
983             }
984           else
985             {
986               int cpu = dm->hqos_cpu_first_index + next_hqos_cpu;
987
988               if (dm->hqos_cpu_count == 0)
989                 return clib_error_return (0, "no HQoS threads available");
990
991               vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
992               dq->device = xd->device_index;
993               dq->queue_id = 0;
994
995               next_hqos_cpu++;
996               if (next_hqos_cpu == dm->hqos_cpu_count)
997                 next_hqos_cpu = 0;
998
999               devconf->hqos.hqos_thread_valid = 1;
1000               devconf->hqos.hqos_thread = cpu;
1001             }
1002         }
1003
1004       vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
1005                             CLIB_CACHE_LINE_BYTES);
1006       for (j = 0; j < tm->n_vlib_mains; j++)
1007         {
1008           vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
1009                            sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
1010           vec_reset_length (xd->tx_vectors[j]);
1011         }
1012
1013       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
1014                             CLIB_CACHE_LINE_BYTES);
1015       for (j = 0; j < xd->rx_q_used; j++)
1016         {
1017           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
1018                                 CLIB_CACHE_LINE_BYTES);
1019           vec_reset_length (xd->rx_vectors[j]);
1020         }
1021
1022       vec_validate_aligned (xd->d_trace_buffers, tm->n_vlib_mains,
1023                             CLIB_CACHE_LINE_BYTES);
1024
1025       rv = dpdk_port_setup (dm, xd);
1026
1027       if (rv)
1028         return rv;
1029
1030       if (devconf->hqos_enabled)
1031         {
1032           rv = dpdk_port_setup_hqos (xd, &devconf->hqos);
1033           if (rv)
1034             return rv;
1035         }
1036
1037       /* count the number of descriptors used for this device */
1038       nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
1039
1040       error = ethernet_register_interface
1041         (dm->vnet_main, dpdk_device_class.index, xd->device_index,
1042          /* ethernet address */ addr,
1043          &xd->vlib_hw_if_index, dpdk_flag_change);
1044       if (error)
1045         return error;
1046
1047       sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
1048       xd->vlib_sw_if_index = sw->sw_if_index;
1049       hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
1050
1051       /*
1052        * For cisco VIC vNIC, set default to VLAN strip enabled, unless
1053        * specified otherwise in the startup config.
1054        * For other NICs default to VLAN strip disabled, unless specified
1055        * otherwis in the startup config.
1056        */
1057       if (xd->pmd == VNET_DPDK_PMD_ENIC)
1058         {
1059           if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
1060             vlan_strip = 1;     /* remove vlan tag from VIC port by default */
1061           else
1062             clib_warning ("VLAN strip disabled for interface\n");
1063         }
1064       else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
1065         vlan_strip = 1;
1066
1067       if (vlan_strip)
1068         {
1069           int vlan_off;
1070           vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
1071           vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
1072           xd->port_conf.rxmode.hw_vlan_strip = vlan_off;
1073           if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
1074             clib_warning ("VLAN strip enabled for interface\n");
1075           else
1076             clib_warning ("VLAN strip cannot be supported by interface\n");
1077         }
1078
1079       hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
1080         xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
1081
1082       rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
1083     }
1084
1085   if (nb_desc > dm->conf->num_mbufs)
1086     clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
1087                   dm->conf->num_mbufs, nb_desc);
1088
1089   return 0;
1090 }
1091
1092 static void
1093 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
1094 {
1095   vlib_pci_main_t *pm = &pci_main;
1096   clib_error_t *error;
1097   vlib_pci_device_t *d;
1098   u8 *pci_addr = 0;
1099   int num_whitelisted = vec_len (conf->dev_confs);
1100
1101   /* *INDENT-OFF* */
1102   pool_foreach (d, pm->pci_devs, ({
1103     dpdk_device_config_t * devconf = 0;
1104     vec_reset_length (pci_addr);
1105     pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
1106
1107     if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
1108       continue;
1109
1110     if (num_whitelisted)
1111       {
1112         uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
1113
1114         if (!p)
1115           continue;
1116
1117         devconf = pool_elt_at_index (conf->dev_confs, p[0]);
1118       }
1119
1120     /* virtio */
1121     if (d->vendor_id == 0x1af4 && d->device_id == 0x1000)
1122       ;
1123     /* vmxnet3 */
1124     else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
1125       ;
1126     /* all Intel network devices */
1127     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_NETWORK_ETHERNET)
1128       ;
1129     /* all Intel QAT devices VFs */
1130     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_PROCESSOR_CO &&
1131         (d->device_id == 0x0443 || d->device_id == 0x37c9 || d->device_id == 0x19e3))
1132       ;
1133     /* Cisco VIC */
1134     else if (d->vendor_id == 0x1137 && d->device_id == 0x0043)
1135       ;
1136     /* Chelsio T4/T5 */
1137     else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
1138       ;
1139     else
1140       {
1141         clib_warning ("Unsupported PCI device 0x%04x:0x%04x found "
1142                       "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
1143                       pci_addr);
1144         continue;
1145       }
1146
1147     error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
1148
1149     if (error)
1150       {
1151         if (devconf == 0)
1152           {
1153             pool_get (conf->dev_confs, devconf);
1154             hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
1155                       devconf - conf->dev_confs);
1156             devconf->pci_addr.as_u32 = d->bus_address.as_u32;
1157           }
1158         devconf->is_blacklisted = 1;
1159         clib_error_report (error);
1160       }
1161   }));
1162   /* *INDENT-ON* */
1163   vec_free (pci_addr);
1164 }
1165
1166 static clib_error_t *
1167 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
1168                     unformat_input_t * input, u8 is_default)
1169 {
1170   clib_error_t *error = 0;
1171   uword *p;
1172   dpdk_device_config_t *devconf;
1173   unformat_input_t sub_input;
1174
1175   if (is_default)
1176     {
1177       devconf = &conf->default_devconf;
1178     }
1179   else
1180     {
1181       p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
1182
1183       if (!p)
1184         {
1185           pool_get (conf->dev_confs, devconf);
1186           hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
1187                     devconf - conf->dev_confs);
1188         }
1189       else
1190         return clib_error_return (0,
1191                                   "duplicate configuration for PCI address %U",
1192                                   format_vlib_pci_addr, &pci_addr);
1193     }
1194
1195   devconf->pci_addr.as_u32 = pci_addr.as_u32;
1196   devconf->hqos_enabled = 0;
1197   dpdk_device_config_hqos_default (&devconf->hqos);
1198
1199   if (!input)
1200     return 0;
1201
1202   unformat_skip_white_space (input);
1203   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1204     {
1205       if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
1206         ;
1207       else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
1208         ;
1209       else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
1210         ;
1211       else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
1212         ;
1213       else if (unformat (input, "workers %U", unformat_bitmap_list,
1214                          &devconf->workers))
1215         ;
1216       else
1217         if (unformat
1218             (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
1219         {
1220           error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
1221           if (error)
1222             break;
1223         }
1224       else if (unformat (input, "vlan-strip-offload off"))
1225         devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
1226       else if (unformat (input, "vlan-strip-offload on"))
1227         devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
1228       else
1229         if (unformat
1230             (input, "hqos %U", unformat_vlib_cli_sub_input, &sub_input))
1231         {
1232           devconf->hqos_enabled = 1;
1233           error = unformat_hqos (&sub_input, &devconf->hqos);
1234           if (error)
1235             break;
1236         }
1237       else if (unformat (input, "hqos"))
1238         {
1239           devconf->hqos_enabled = 1;
1240         }
1241       else
1242         {
1243           error = clib_error_return (0, "unknown input `%U'",
1244                                      format_unformat_error, input);
1245           break;
1246         }
1247     }
1248
1249   if (error)
1250     return error;
1251
1252   if (devconf->workers && devconf->num_rx_queues == 0)
1253     devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
1254   else if (devconf->workers &&
1255            clib_bitmap_count_set_bits (devconf->workers) !=
1256            devconf->num_rx_queues)
1257     error =
1258       clib_error_return (0,
1259                          "%U: number of worker threadds must be "
1260                          "equal to number of rx queues", format_vlib_pci_addr,
1261                          &pci_addr);
1262
1263   return error;
1264 }
1265
1266 static clib_error_t *
1267 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
1268 {
1269   clib_error_t *error = 0;
1270   dpdk_main_t *dm = &dpdk_main;
1271   dpdk_config_main_t *conf = &dpdk_config_main;
1272   vlib_thread_main_t *tm = vlib_get_thread_main ();
1273   dpdk_device_config_t *devconf;
1274   vlib_pci_addr_t pci_addr;
1275   unformat_input_t sub_input;
1276   u8 *s, *tmp = 0;
1277   u8 *rte_cmd = 0, *ethname = 0;
1278   u32 log_level;
1279   int ret, i;
1280   int num_whitelisted = 0;
1281   u8 no_pci = 0;
1282   u8 no_huge = 0;
1283   u8 huge_dir = 0;
1284   u8 file_prefix = 0;
1285   u8 *socket_mem = 0;
1286
1287   conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
1288
1289   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1290     {
1291       /* Prime the pump */
1292       if (unformat (input, "no-hugetlb"))
1293         {
1294           vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
1295           no_huge = 1;
1296         }
1297
1298       else if (unformat (input, "enable-tcp-udp-checksum"))
1299         conf->enable_tcp_udp_checksum = 1;
1300
1301       else if (unformat (input, "decimal-interface-names"))
1302         conf->interface_name_format_decimal = 1;
1303
1304       else if (unformat (input, "no-multi-seg"))
1305         conf->no_multi_seg = 1;
1306
1307       else if (unformat (input, "enable-cryptodev"))
1308         conf->cryptodev = 1;
1309
1310       else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1311                          &sub_input))
1312         {
1313           error =
1314             dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
1315                                 1);
1316
1317           if (error)
1318             return error;
1319         }
1320       else
1321         if (unformat
1322             (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1323              unformat_vlib_cli_sub_input, &sub_input))
1324         {
1325           error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1326
1327           if (error)
1328             return error;
1329
1330           num_whitelisted++;
1331         }
1332       else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1333         {
1334           error = dpdk_device_config (conf, pci_addr, 0, 0);
1335
1336           if (error)
1337             return error;
1338
1339           num_whitelisted++;
1340         }
1341       else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1342         ;
1343       else if (unformat (input, "kni %d", &conf->num_kni))
1344         ;
1345       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1346         ;
1347       else if (unformat (input, "socket-mem %s", &socket_mem))
1348         ;
1349       else if (unformat (input, "no-pci"))
1350         {
1351           no_pci = 1;
1352           tmp = format (0, "--no-pci%c", 0);
1353           vec_add1 (conf->eal_init_args, tmp);
1354         }
1355       else if (unformat (input, "poll-sleep %d", &dm->poll_sleep_usec))
1356         ;
1357
1358 #define _(a)                                    \
1359       else if (unformat(input, #a))             \
1360         {                                       \
1361           tmp = format (0, "--%s%c", #a, 0);    \
1362           vec_add1 (conf->eal_init_args, tmp);    \
1363         }
1364       foreach_eal_double_hyphen_predicate_arg
1365 #undef _
1366 #define _(a)                                          \
1367         else if (unformat(input, #a " %s", &s))       \
1368           {                                           \
1369             if (!strncmp(#a, "huge-dir", 8))          \
1370               huge_dir = 1;                           \
1371             else if (!strncmp(#a, "file-prefix", 11)) \
1372               file_prefix = 1;                        \
1373             tmp = format (0, "--%s%c", #a, 0);        \
1374             vec_add1 (conf->eal_init_args, tmp);      \
1375             vec_add1 (s, 0);                          \
1376             if (!strncmp(#a, "vdev", 4))              \
1377               if (strstr((char*)s, "af_packet"))      \
1378                 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1379             vec_add1 (conf->eal_init_args, s);        \
1380           }
1381         foreach_eal_double_hyphen_arg
1382 #undef _
1383 #define _(a,b)                                          \
1384           else if (unformat(input, #a " %s", &s))       \
1385             {                                           \
1386               tmp = format (0, "-%s%c", #b, 0);         \
1387               vec_add1 (conf->eal_init_args, tmp);      \
1388               vec_add1 (s, 0);                          \
1389               vec_add1 (conf->eal_init_args, s);        \
1390             }
1391         foreach_eal_single_hyphen_arg
1392 #undef _
1393 #define _(a,b)                                          \
1394             else if (unformat(input, #a " %s", &s))     \
1395               {                                         \
1396                 tmp = format (0, "-%s%c", #b, 0);       \
1397                 vec_add1 (conf->eal_init_args, tmp);    \
1398                 vec_add1 (s, 0);                        \
1399                 vec_add1 (conf->eal_init_args, s);      \
1400                 conf->a##_set_manually = 1;             \
1401               }
1402         foreach_eal_single_hyphen_mandatory_arg
1403 #undef _
1404         else if (unformat (input, "default"))
1405         ;
1406
1407       else if (unformat_skip_white_space (input))
1408         ;
1409       else
1410         {
1411           error = clib_error_return (0, "unknown input `%U'",
1412                                      format_unformat_error, input);
1413           goto done;
1414         }
1415     }
1416
1417   if (!conf->uio_driver_name)
1418     conf->uio_driver_name = format (0, "uio_pci_generic%c", 0);
1419
1420   /*
1421    * Use 1G huge pages if available.
1422    */
1423   if (!no_huge && !huge_dir)
1424     {
1425       u32 x, *mem_by_socket = 0;
1426       uword c = 0;
1427       u8 use_1g = 1;
1428       u8 use_2m = 1;
1429       u8 less_than_1g = 1;
1430       int rv;
1431
1432       umount (DEFAULT_HUGE_DIR);
1433
1434       /* Process "socket-mem" parameter value */
1435       if (vec_len (socket_mem))
1436         {
1437           unformat_input_t in;
1438           unformat_init_vector (&in, socket_mem);
1439           while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1440             {
1441               if (unformat (&in, "%u,", &x))
1442                 ;
1443               else if (unformat (&in, "%u", &x))
1444                 ;
1445               else if (unformat (&in, ","))
1446                 x = 0;
1447               else
1448                 break;
1449
1450               vec_add1 (mem_by_socket, x);
1451
1452               if (x > 1023)
1453                 less_than_1g = 0;
1454             }
1455           /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1456           unformat_free (&in);
1457           socket_mem = 0;
1458         }
1459       else
1460         {
1461           /* *INDENT-OFF* */
1462           clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1463             {
1464               vec_validate(mem_by_socket, c);
1465               mem_by_socket[c] = 256; /* default per-socket mem */
1466             }
1467           ));
1468           /* *INDENT-ON* */
1469         }
1470
1471       /* check if available enough 1GB pages for each socket */
1472       /* *INDENT-OFF* */
1473       clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1474         {
1475           int pages_avail, page_size, mem;
1476
1477           vec_validate(mem_by_socket, c);
1478           mem = mem_by_socket[c];
1479
1480           page_size = 1024;
1481           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1482
1483           if (pages_avail < 0 || page_size * pages_avail < mem)
1484             use_1g = 0;
1485
1486           page_size = 2;
1487           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1488
1489           if (pages_avail < 0 || page_size * pages_avail < mem)
1490             use_2m = 0;
1491       }));
1492       /* *INDENT-ON* */
1493
1494       if (mem_by_socket == 0)
1495         {
1496           error = clib_error_return (0, "mem_by_socket NULL");
1497           goto done;
1498         }
1499       _vec_len (mem_by_socket) = c + 1;
1500
1501       /* regenerate socket_mem string */
1502       vec_foreach_index (x, mem_by_socket)
1503         socket_mem = format (socket_mem, "%s%u",
1504                              socket_mem ? "," : "", mem_by_socket[x]);
1505       socket_mem = format (socket_mem, "%c", 0);
1506
1507       vec_free (mem_by_socket);
1508
1509       rv = mkdir (VPP_RUN_DIR, 0755);
1510       if (rv && errno != EEXIST)
1511         {
1512           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1513                                      VPP_RUN_DIR, errno);
1514           goto done;
1515         }
1516
1517       rv = mkdir (DEFAULT_HUGE_DIR, 0755);
1518       if (rv && errno != EEXIST)
1519         {
1520           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1521                                      DEFAULT_HUGE_DIR, errno);
1522           goto done;
1523         }
1524
1525       if (use_1g && !(less_than_1g && use_2m))
1526         {
1527           rv =
1528             mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1529         }
1530       else if (use_2m)
1531         {
1532           rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1533         }
1534       else
1535         {
1536           return clib_error_return (0, "not enough free huge pages");
1537         }
1538
1539       if (rv)
1540         {
1541           error = clib_error_return (0, "mount failed %d", errno);
1542           goto done;
1543         }
1544
1545       tmp = format (0, "--huge-dir%c", 0);
1546       vec_add1 (conf->eal_init_args, tmp);
1547       tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1548       vec_add1 (conf->eal_init_args, tmp);
1549       if (!file_prefix)
1550         {
1551           tmp = format (0, "--file-prefix%c", 0);
1552           vec_add1 (conf->eal_init_args, tmp);
1553           tmp = format (0, "vpp%c", 0);
1554           vec_add1 (conf->eal_init_args, tmp);
1555         }
1556     }
1557
1558   vec_free (rte_cmd);
1559   vec_free (ethname);
1560
1561   if (error)
1562     return error;
1563
1564   /* I'll bet that -c and -n must be the first and second args... */
1565   if (!conf->coremask_set_manually)
1566     {
1567       vlib_thread_registration_t *tr;
1568       uword *coremask = 0;
1569       int i;
1570
1571       /* main thread core */
1572       coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1573
1574       for (i = 0; i < vec_len (tm->registrations); i++)
1575         {
1576           tr = tm->registrations[i];
1577           coremask = clib_bitmap_or (coremask, tr->coremask);
1578         }
1579
1580       vec_insert (conf->eal_init_args, 2, 1);
1581       conf->eal_init_args[1] = (u8 *) "-c";
1582       tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1583       conf->eal_init_args[2] = tmp;
1584       clib_bitmap_free (coremask);
1585     }
1586
1587   if (!conf->nchannels_set_manually)
1588     {
1589       vec_insert (conf->eal_init_args, 2, 3);
1590       conf->eal_init_args[3] = (u8 *) "-n";
1591       tmp = format (0, "%d", conf->nchannels);
1592       conf->eal_init_args[4] = tmp;
1593     }
1594
1595   if (no_pci == 0 && geteuid () == 0)
1596     dpdk_bind_devices_to_uio (conf);
1597
1598 #define _(x) \
1599     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1600       devconf->x = conf->default_devconf.x ;
1601
1602   /* *INDENT-OFF* */
1603   pool_foreach (devconf, conf->dev_confs, ({
1604
1605     /* default per-device config items */
1606     foreach_dpdk_device_config_item
1607
1608     /* add DPDK EAL whitelist/blacklist entry */
1609     if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1610       {
1611         tmp = format (0, "-w%c", 0);
1612         vec_add1 (conf->eal_init_args, tmp);
1613         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1614         vec_add1 (conf->eal_init_args, tmp);
1615       }
1616     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1617       {
1618         tmp = format (0, "-b%c", 0);
1619         vec_add1 (conf->eal_init_args, tmp);
1620         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1621         vec_add1 (conf->eal_init_args, tmp);
1622       }
1623   }));
1624   /* *INDENT-ON* */
1625
1626 #undef _
1627
1628   /* set master-lcore */
1629   tmp = format (0, "--master-lcore%c", 0);
1630   vec_add1 (conf->eal_init_args, tmp);
1631   tmp = format (0, "%u%c", tm->main_lcore, 0);
1632   vec_add1 (conf->eal_init_args, tmp);
1633
1634   /* set socket-mem */
1635   tmp = format (0, "--socket-mem%c", 0);
1636   vec_add1 (conf->eal_init_args, tmp);
1637   tmp = format (0, "%s%c", socket_mem, 0);
1638   vec_add1 (conf->eal_init_args, tmp);
1639
1640   /* NULL terminate the "argv" vector, in case of stupidity */
1641   vec_add1 (conf->eal_init_args, 0);
1642   _vec_len (conf->eal_init_args) -= 1;
1643
1644   /* Set up DPDK eal and packet mbuf pool early. */
1645
1646   log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1647
1648   rte_set_log_level (log_level);
1649
1650   vm = vlib_get_main ();
1651
1652   /* make copy of args as rte_eal_init tends to mess up with arg array */
1653   for (i = 1; i < vec_len (conf->eal_init_args); i++)
1654     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1655                                       conf->eal_init_args[i]);
1656
1657   ret =
1658     rte_eal_init (vec_len (conf->eal_init_args),
1659                   (char **) conf->eal_init_args);
1660
1661   /* lazy umount hugepages */
1662   umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
1663
1664   if (ret < 0)
1665     return clib_error_return (0, "rte_eal_init returned %d", ret);
1666
1667   /* Dump the physical memory layout prior to creating the mbuf_pool */
1668   fprintf (stdout, "DPDK physical memory layout:\n");
1669   rte_dump_physmem_layout (stdout);
1670
1671   /* main thread 1st */
1672   error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
1673   if (error)
1674     return error;
1675
1676   for (i = 0; i < RTE_MAX_LCORE; i++)
1677     {
1678       error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1679                                        rte_lcore_to_socket_id (i));
1680       if (error)
1681         return error;
1682     }
1683
1684 done:
1685   return error;
1686 }
1687
1688 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1689
1690 void
1691 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1692 {
1693   vnet_main_t *vnm = vnet_get_main ();
1694   struct rte_eth_link prev_link = xd->link;
1695   u32 hw_flags = 0;
1696   u8 hw_flags_chg = 0;
1697
1698   /* only update link state for PMD interfaces */
1699   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1700     return;
1701
1702   xd->time_last_link_update = now ? now : xd->time_last_link_update;
1703   memset (&xd->link, 0, sizeof (xd->link));
1704   rte_eth_link_get_nowait (xd->device_index, &xd->link);
1705
1706   if (LINK_STATE_ELOGS)
1707     {
1708       vlib_main_t *vm = vlib_get_main ();
1709       ELOG_TYPE_DECLARE (e) =
1710       {
1711       .format =
1712           "update-link-state: sw_if_index %d, admin_up %d,"
1713           "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1714
1715       struct
1716       {
1717         u32 sw_if_index;
1718         u8 admin_up;
1719         u8 old_link_state;
1720         u8 new_link_state;
1721       } *ed;
1722       ed = ELOG_DATA (&vm->elog_main, e);
1723       ed->sw_if_index = xd->vlib_sw_if_index;
1724       ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1725       ed->old_link_state = (u8)
1726         vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1727       ed->new_link_state = (u8) xd->link.link_status;
1728     }
1729
1730   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
1731       ((xd->link.link_status != 0) ^
1732        vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1733     {
1734       hw_flags_chg = 1;
1735       hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1736     }
1737
1738   if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1739     {
1740       hw_flags_chg = 1;
1741       switch (xd->link.link_duplex)
1742         {
1743         case ETH_LINK_HALF_DUPLEX:
1744           hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1745           break;
1746         case ETH_LINK_FULL_DUPLEX:
1747           hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1748           break;
1749         default:
1750           break;
1751         }
1752     }
1753   if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1754     {
1755       hw_flags_chg = 1;
1756       switch (xd->link.link_speed)
1757         {
1758         case ETH_SPEED_NUM_10M:
1759           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1760           break;
1761         case ETH_SPEED_NUM_100M:
1762           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1763           break;
1764         case ETH_SPEED_NUM_1G:
1765           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1766           break;
1767         case ETH_SPEED_NUM_10G:
1768           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1769           break;
1770         case ETH_SPEED_NUM_40G:
1771           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1772           break;
1773         case 0:
1774           break;
1775         default:
1776           clib_warning ("unknown link speed %d", xd->link.link_speed);
1777           break;
1778         }
1779     }
1780   if (hw_flags_chg)
1781     {
1782       if (LINK_STATE_ELOGS)
1783         {
1784           vlib_main_t *vm = vlib_get_main ();
1785
1786           ELOG_TYPE_DECLARE (e) =
1787           {
1788           .format =
1789               "update-link-state: sw_if_index %d, new flags %d",.format_args
1790               = "i4i4",};
1791
1792           struct
1793           {
1794             u32 sw_if_index;
1795             u32 flags;
1796           } *ed;
1797           ed = ELOG_DATA (&vm->elog_main, e);
1798           ed->sw_if_index = xd->vlib_sw_if_index;
1799           ed->flags = hw_flags;
1800         }
1801       vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1802     }
1803 }
1804
1805 static uword
1806 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1807 {
1808   clib_error_t *error;
1809   vnet_main_t *vnm = vnet_get_main ();
1810   dpdk_main_t *dm = &dpdk_main;
1811   ethernet_main_t *em = &ethernet_main;
1812   dpdk_device_t *xd;
1813   vlib_thread_main_t *tm = vlib_get_thread_main ();
1814   int i;
1815
1816   error = dpdk_lib_init (dm);
1817
1818   /*
1819    * Turn on the input node if we found some devices to drive
1820    * and we're not running worker threads or i/o threads
1821    */
1822
1823   if (error == 0 && vec_len (dm->devices) > 0)
1824     {
1825       if (tm->n_vlib_mains == 1)
1826         vlib_node_set_state (vm, dpdk_input_node.index,
1827                              VLIB_NODE_STATE_POLLING);
1828       else
1829         for (i = 0; i < tm->n_vlib_mains; i++)
1830           if (vec_len (dm->devices_by_cpu[i]) > 0)
1831             vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1832                                  VLIB_NODE_STATE_POLLING);
1833     }
1834
1835   if (error)
1836     clib_error_report (error);
1837
1838   tm->worker_thread_release = 1;
1839
1840   f64 now = vlib_time_now (vm);
1841   vec_foreach (xd, dm->devices)
1842   {
1843     dpdk_update_link_state (xd, now);
1844   }
1845
1846   {
1847     /*
1848      * Extra set up for bond interfaces:
1849      *  1. Setup MACs for bond interfaces and their slave links which was set
1850      *     in dpdk_port_setup() but needs to be done again here to take effect.
1851      *  2. Set up info for bond interface related CLI support.
1852      */
1853     int nports = rte_eth_dev_count ();
1854     if (nports > 0)
1855       {
1856         for (i = 0; i < nports; i++)
1857           {
1858             xd = &dm->devices[i];
1859             ASSERT (i == xd->device_index);
1860             if (xd->pmd == VNET_DPDK_PMD_BOND)
1861               {
1862                 u8 addr[6];
1863                 u8 slink[16];
1864                 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1865                 if (nlink > 0)
1866                   {
1867                     vnet_hw_interface_t *bhi;
1868                     ethernet_interface_t *bei;
1869                     int rv;
1870
1871                     /* Get MAC of 1st slave link */
1872                     rte_eth_macaddr_get
1873                       (slink[0], (struct ether_addr *) addr);
1874
1875                     /* Set MAC of bounded interface to that of 1st slave link */
1876                     clib_warning ("Set MAC for bond dev# %d", i);
1877                     rv = rte_eth_bond_mac_address_set
1878                       (i, (struct ether_addr *) addr);
1879                     if (rv)
1880                       clib_warning ("Set MAC addr failure rv=%d", rv);
1881
1882                     /* Populate MAC of bonded interface in VPP hw tables */
1883                     bhi = vnet_get_hw_interface
1884                       (vnm, dm->devices[i].vlib_hw_if_index);
1885                     bei = pool_elt_at_index
1886                       (em->interfaces, bhi->hw_instance);
1887                     clib_memcpy (bhi->hw_address, addr, 6);
1888                     clib_memcpy (bei->address, addr, 6);
1889
1890                     /* Init l3 packet size allowed on bonded interface */
1891                     bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
1892                     bhi->max_l3_packet_bytes[VLIB_RX] =
1893                       bhi->max_l3_packet_bytes[VLIB_TX] =
1894                       ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1895                     while (nlink >= 1)
1896                       {         /* for all slave links */
1897                         int slave = slink[--nlink];
1898                         dpdk_device_t *sdev = &dm->devices[slave];
1899                         vnet_hw_interface_t *shi;
1900                         vnet_sw_interface_t *ssi;
1901                         ethernet_interface_t *sei;
1902                         /* Add MAC to all slave links except the first one */
1903                         if (nlink)
1904                           {
1905                             clib_warning ("Add MAC for slave dev# %d", slave);
1906                             rv = rte_eth_dev_mac_addr_add
1907                               (slave, (struct ether_addr *) addr, 0);
1908                             if (rv)
1909                               clib_warning ("Add MAC addr failure rv=%d", rv);
1910                           }
1911                         /* Set slaves bitmap for bonded interface */
1912                         bhi->bond_info = clib_bitmap_set
1913                           (bhi->bond_info, sdev->vlib_hw_if_index, 1);
1914                         /* Set slave link flags on slave interface */
1915                         shi = vnet_get_hw_interface
1916                           (vnm, sdev->vlib_hw_if_index);
1917                         ssi = vnet_get_sw_interface
1918                           (vnm, sdev->vlib_sw_if_index);
1919                         sei = pool_elt_at_index
1920                           (em->interfaces, shi->hw_instance);
1921
1922                         shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1923                         ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1924                         clib_memcpy (shi->hw_address, addr, 6);
1925                         clib_memcpy (sei->address, addr, 6);
1926
1927                         /* Set l3 packet size allowed as the lowest of slave */
1928                         if (bhi->max_l3_packet_bytes[VLIB_RX] >
1929                             shi->max_l3_packet_bytes[VLIB_RX])
1930                           bhi->max_l3_packet_bytes[VLIB_RX] =
1931                             bhi->max_l3_packet_bytes[VLIB_TX] =
1932                             shi->max_l3_packet_bytes[VLIB_RX];
1933
1934                         /* Set max packet size allowed as the lowest of slave */
1935                         if (bhi->max_packet_bytes > shi->max_packet_bytes)
1936                           bhi->max_packet_bytes = shi->max_packet_bytes;
1937                       }
1938                   }
1939               }
1940           }
1941       }
1942   }
1943
1944   while (1)
1945     {
1946       /*
1947        * check each time through the loop in case intervals are changed
1948        */
1949       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1950         dm->link_state_poll_interval : dm->stat_poll_interval;
1951
1952       vlib_process_wait_for_event_or_clock (vm, min_wait);
1953
1954       if (dm->admin_up_down_in_progress)
1955         /* skip the poll if an admin up down is in progress (on any interface) */
1956         continue;
1957
1958       vec_foreach (xd, dm->devices)
1959       {
1960         f64 now = vlib_time_now (vm);
1961         if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1962           dpdk_update_counters (xd, now);
1963         if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1964           dpdk_update_link_state (xd, now);
1965
1966       }
1967     }
1968
1969   return 0;
1970 }
1971
1972 /* *INDENT-OFF* */
1973 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1974     .function = dpdk_process,
1975     .type = VLIB_NODE_TYPE_PROCESS,
1976     .name = "dpdk-process",
1977     .process_log2_n_stack_bytes = 17,
1978 };
1979 /* *INDENT-ON* */
1980
1981 int
1982 dpdk_set_stat_poll_interval (f64 interval)
1983 {
1984   if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1985     return (VNET_API_ERROR_INVALID_VALUE);
1986
1987   dpdk_main.stat_poll_interval = interval;
1988
1989   return 0;
1990 }
1991
1992 int
1993 dpdk_set_link_state_poll_interval (f64 interval)
1994 {
1995   if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1996     return (VNET_API_ERROR_INVALID_VALUE);
1997
1998   dpdk_main.link_state_poll_interval = interval;
1999
2000   return 0;
2001 }
2002
2003 clib_error_t *
2004 dpdk_init (vlib_main_t * vm)
2005 {
2006   dpdk_main_t *dm = &dpdk_main;
2007   vlib_node_t *ei;
2008   clib_error_t *error = 0;
2009   vlib_thread_main_t *tm = vlib_get_thread_main ();
2010
2011   /* verify that structs are cacheline aligned */
2012   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
2013                  "Cache line marker must be 1st element in dpdk_device_t");
2014   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
2015                  CLIB_CACHE_LINE_BYTES,
2016                  "Data in cache line 0 is bigger than cache line size");
2017   STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
2018                  "Cache line marker must be 1st element in frame_queue_trace_t");
2019
2020   u8 *name;
2021   name = format (0, "dpdk_%08x%c", api_version, 0);
2022
2023   /* Ask for a correctly-sized block of API message decode slots */
2024   dm->msg_id_base = vl_msg_api_get_msg_ids
2025     ((char *) name, VL_MSG_FIRST_AVAILABLE);
2026   vec_free (name);
2027
2028   dm->vlib_main = vm;
2029   dm->vnet_main = vnet_get_main ();
2030   dm->conf = &dpdk_config_main;
2031
2032   error = dpdk_plugin_api_hookup (vm);
2033
2034   /* Add our API messages to the global name_crc hash table */
2035   setup_message_id_table (dm, &api_main);
2036
2037 //  TODO
2038 //  plugin_custom_dump_configure (dm);
2039
2040   ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
2041   if (ei == 0)
2042     return clib_error_return (0, "ethernet-input node AWOL");
2043
2044   dm->ethernet_input_node_index = ei->index;
2045
2046   dm->conf->nchannels = 4;
2047   dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
2048   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
2049
2050   dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
2051   dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
2052   dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
2053
2054   /* $$$ use n_thread_stacks since it's known-good at this point */
2055   vec_validate (dm->recycle, tm->n_thread_stacks - 1);
2056
2057   /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
2058   dm->buffer_flags_template =
2059     (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_EXT_HDR_VALID
2060      | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
2061
2062   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
2063   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
2064
2065   /* init CLI */
2066   if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
2067     return error;
2068
2069   return error;
2070 }
2071
2072 VLIB_INIT_FUNCTION (dpdk_init);
2073
2074
2075 /*
2076  * fd.io coding-style-patch-verification: ON
2077  *
2078  * Local Variables:
2079  * eval: (c-set-style "gnu")
2080  * End:
2081  */