FD.io VPP  v21.06
Vector Packet Processing
common.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 
16 #include <vnet/vnet.h>
17 #include <vppinfra/vec.h>
18 #include <vppinfra/format.h>
19 #include <vppinfra/file.h>
20 #include <vlib/unix/unix.h>
21 #include <assert.h>
22 
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
27 #include <dpdk/buffer.h>
28 #include <dpdk/device/dpdk.h>
29 #include <dpdk/device/dpdk_priv.h>
30 #include <vppinfra/error.h>
31 
32 void
33 dpdk_device_error (dpdk_device_t * xd, char *str, int rv)
34 {
35  dpdk_log_err ("Interface %U error %d: %s",
36  format_dpdk_device_name, xd->port_id, rv, rte_strerror (rv));
37  xd->errors = clib_error_return (xd->errors, "%s[port:%d, errno:%d]: %s",
38  str, xd->port_id, rv, rte_strerror (rv));
39 }
40 
41 void
43 {
44  dpdk_main_t *dm = &dpdk_main;
46  vnet_main_t *vnm = vnet_get_main ();
50  struct rte_eth_dev_info dev_info;
51  u64 bitmap;
52  int rv;
53  int j;
54 
55  ASSERT (vlib_get_thread_index () == 0);
56 
57  clib_error_free (xd->errors);
59 
60  if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
61  {
63  dpdk_device_stop (xd);
64  }
65 
66  /* Enable flow director when flows exist */
67  if (xd->pmd == VNET_DPDK_PMD_I40E)
68  {
69  if ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) != 0)
70  xd->port_conf.fdir_conf.mode = RTE_FDIR_MODE_PERFECT;
71  else
72  xd->port_conf.fdir_conf.mode = RTE_FDIR_MODE_NONE;
73  }
74 
75  rte_eth_dev_info_get (xd->port_id, &dev_info);
76 
77  bitmap = xd->port_conf.txmode.offloads & ~dev_info.tx_offload_capa;
78  if (bitmap)
79  {
80  dpdk_log_warn ("unsupported tx offloads requested on port %u: %U",
82  xd->port_conf.txmode.offloads ^= bitmap;
83  }
84 
85  bitmap = xd->port_conf.rxmode.offloads & ~dev_info.rx_offload_capa;
86  if (bitmap)
87  {
88  dpdk_log_warn ("unsupported rx offloads requested on port %u: %U",
90  xd->port_conf.rxmode.offloads ^= bitmap;
91  }
92 
93  rv = rte_eth_dev_configure (xd->port_id, xd->rx_q_used,
94  xd->tx_q_used, &xd->port_conf);
95 
96  if (rv < 0)
97  {
98  dpdk_device_error (xd, "rte_eth_dev_configure", rv);
99  goto error;
100  }
101 
104  for (j = 0; j < xd->tx_q_used; j++)
105  {
106  rv =
107  rte_eth_tx_queue_setup (xd->port_id, j, xd->nb_tx_desc,
108  xd->cpu_socket, &xd->tx_conf);
109 
110  /* retry with any other CPU socket */
111  if (rv < 0)
112  rv =
113  rte_eth_tx_queue_setup (xd->port_id, j,
114  xd->nb_tx_desc, SOCKET_ID_ANY,
115  &xd->tx_conf);
116  if (rv < 0)
117  dpdk_device_error (xd, "rte_eth_tx_queue_setup", rv);
118 
119  if (xd->tx_q_used < tm->n_vlib_mains)
120  clib_spinlock_init (&vec_elt (xd->tx_queues, j).lock);
121  }
122 
125  for (j = 0; j < xd->rx_q_used; j++)
126  {
130  vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, bpidx);
131  struct rte_mempool *mp = dpdk_mempool_by_buffer_pool_index[bpidx];
132 
133  rv = rte_eth_rx_queue_setup (xd->port_id, j, xd->nb_rx_desc,
134  xd->cpu_socket, 0, mp);
135 
136  /* retry with any other CPU socket */
137  if (rv < 0)
138  rv = rte_eth_rx_queue_setup (xd->port_id, j, xd->nb_rx_desc,
139  SOCKET_ID_ANY, 0, mp);
140 
141  rxq->buffer_pool_index = bp->index;
142 
143  if (rv < 0)
144  dpdk_device_error (xd, "rte_eth_rx_queue_setup", rv);
145  }
146 
147  if (vec_len (xd->errors))
148  goto error;
149 
150  rte_eth_dev_set_mtu (xd->port_id, hi->max_packet_bytes);
151 
152  if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
153  dpdk_device_start (xd);
154 
155  if (vec_len (xd->errors))
156  goto error;
157 
158  return;
159 
160 error:
161  xd->flags |= DPDK_DEVICE_FLAG_PMD_INIT_FAIL;
163 }
164 
165 static clib_error_t *
167 {
168  vnet_main_t *vnm = vnet_get_main ();
169  dpdk_main_t *dm = &dpdk_main;
170  u32 qidx = uf->private_data;
173 
174  u64 b;
175  CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b));
176  if (rxq->mode != VNET_HW_IF_RX_MODE_POLLING)
177  {
179  rte_eth_dev_rx_intr_enable (xd->port_id, rxq->queue_id);
180  }
181 
182  return 0;
183 }
184 
185 static void
187 {
188  vnet_main_t *vnm = vnet_get_main ();
190  if (!hi)
191  return;
192 
193  if (!xd->port_conf.intr_conf.rxq)
194  return;
195 
196  /* Probe for interrupt support */
197  if (rte_eth_dev_rx_intr_enable (xd->port_id, 0))
198  {
199  dpdk_log_info ("probe for interrupt mode for device %U. Failed.\n",
201  }
202  else
203  {
204  xd->flags |= DPDK_DEVICE_FLAG_INT_SUPPORTED;
205  if (!(xd->flags & DPDK_DEVICE_FLAG_INT_UNMASKABLE))
206  rte_eth_dev_rx_intr_disable (xd->port_id, 0);
207  dpdk_log_info ("Probe for interrupt mode for device %U. Success.\n",
209  }
210 
211  if (xd->flags & DPDK_DEVICE_FLAG_INT_SUPPORTED)
212  {
214  for (int q = 0; q < xd->rx_q_used; q++)
215  {
217  clib_file_t f = { 0 };
218  rxq->efd = rte_eth_dev_rx_intr_ctl_q_get_fd (xd->port_id, q);
219  if (rxq->efd < 0)
220  {
221  xd->flags &= ~DPDK_DEVICE_FLAG_INT_SUPPORTED;
223  break;
224  }
227  f.file_descriptor = rxq->efd;
228  f.private_data = rxq->queue_index;
229  f.description =
230  format (0, "%U queue %u", format_dpdk_device_name, xd->port_id, q);
233  rxq->clib_file_index);
234  if (xd->flags & DPDK_DEVICE_FLAG_INT_UNMASKABLE)
235  {
237  clib_file_t *f =
240  }
241  }
242  }
244 }
245 
246 void
248 {
249  int rv;
250 
251  if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
252  return;
253 
254  rv = rte_eth_dev_start (xd->port_id);
255 
256  if (rv)
257  {
258  dpdk_device_error (xd, "rte_eth_dev_start", rv);
259  return;
260  }
261 
263 
264  if (xd->default_mac_address)
265  rv = rte_eth_dev_default_mac_addr_set (xd->port_id,
266  (void *) xd->default_mac_address);
267 
268  if (rv)
269  dpdk_device_error (xd, "rte_eth_dev_default_mac_addr_set", rv);
270 
271  if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
272  rte_eth_promiscuous_enable (xd->port_id);
273  else
274  rte_eth_promiscuous_disable (xd->port_id);
275 
276  rte_eth_allmulticast_enable (xd->port_id);
277 
278  dpdk_log_info ("Interface %U started",
280 }
281 
282 void
284 {
285  if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
286  return;
287 
288  rte_eth_allmulticast_disable (xd->port_id);
289  rte_eth_dev_stop (xd->port_id);
290  clib_memset (&xd->link, 0, sizeof (struct rte_eth_link));
291 
292  dpdk_log_info ("Interface %U stopped",
294 }
295 
296 void vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
297 
298 always_inline int
300  enum rte_eth_event_type type, void *param)
301 {
302  struct rte_eth_link link;
303 
304  RTE_SET_USED (param);
305  if (type != RTE_ETH_EVENT_INTR_LSC)
306  {
307  dpdk_log_info ("Unknown event %d received for port %d", type, port_id);
308  return -1;
309  }
310 
311  rte_eth_link_get_nowait (port_id, &link);
312  u8 link_up = link.link_status;
313  if (link_up)
314  dpdk_log_info ("Port %d Link Up - speed %u Mbps - %s",
315  port_id, (unsigned) link.link_speed,
316  (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
317  "full-duplex" : "half-duplex");
318  else
319  dpdk_log_info ("Port %d Link Down\n\n", port_id);
320 
321  return 0;
322 }
323 
324 int
326  enum rte_eth_event_type type,
327  void *param,
328  void *ret_param __attribute__ ((unused)))
329 {
330  return dpdk_port_state_callback_inline (port_id, type, param);
331 }
332 
333 /* If this device is PCI return pointer to info, otherwise NULL */
334 struct rte_pci_device *
335 dpdk_get_pci_device (const struct rte_eth_dev_info *info)
336 {
337  const struct rte_bus *bus;
338 
339  bus = rte_bus_find_by_device (info->device);
340  if (bus && !strcmp (bus->name, "pci"))
341  return RTE_DEV_TO_PCI (info->device);
342  else
343  return NULL;
344 }
345 
346 /* If this device is VMBUS return pointer to info, otherwise NULL */
347 struct rte_vmbus_device *
348 dpdk_get_vmbus_device (const struct rte_eth_dev_info *info)
349 {
350  const struct rte_bus *bus;
351 
352  bus = rte_bus_find_by_device (info->device);
353  if (bus && !strcmp (bus->name, "vmbus"))
354  return container_of (info->device, struct rte_vmbus_device, device);
355  else
356  return NULL;
357 }
358 
359 /*
360  * fd.io coding-style-patch-verification: ON
361  *
362  * Local Variables:
363  * eval: (c-set-style "gnu")
364  * End:
365  */
u8 * default_mac_address
Definition: dpdk.h:241
#define UNIX_FILE_EVENT_EDGE_TRIGGERED
Definition: file.h:58
#define CLIB_UNUSED(x)
Definition: clib.h:90
format_function_t format_dpdk_tx_offload_caps
Definition: dpdk.h:482
void vl_api_force_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:631
dpdk_main_t dpdk_main
Definition: init.c:48
unsigned long u64
Definition: types.h:89
u32 sw_if_index
Definition: dpdk.h:192
static_always_inline vlib_buffer_pool_t * vlib_get_buffer_pool(vlib_main_t *vm, u8 buffer_pool_index)
Definition: buffer_funcs.h:552
u8 buffer_pool_index
Definition: dpdk.h:169
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vnet_hw_interface_capabilities_t caps
Definition: interface.h:645
struct rte_pci_device * dpdk_get_pci_device(const struct rte_eth_dev_info *info)
Definition: common.c:335
u64 private_data
Definition: file.h:64
static void dpdk_setup_interrupts(dpdk_device_t *xd)
Definition: common.c:186
u16 flags
Definition: dpdk.h:199
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 file_descriptor
Definition: file.h:54
clib_error_t * errors
Definition: dpdk.h:244
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
static_always_inline void vnet_hw_if_rx_queue_set_int_pending(vnet_main_t *vnm, u32 queue_index)
int dpdk_port_state_callback(dpdk_portid_t port_id, enum rte_eth_event_type type, void *param, void *ret_param)
Definition: common.c:325
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:535
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:513
unsigned char u8
Definition: types.h:56
vlib_buffer_t ** b
u8 data[128]
Definition: ipsec_types.api:92
struct rte_vmbus_device * dpdk_get_vmbus_device(const struct rte_eth_dev_info *info)
Definition: common.c:348
clib_file_function_t * read_function
Definition: file.h:67
unsigned int u32
Definition: types.h:88
vlib_frame_t * f
clib_file_t * file_pool
Definition: file.h:88
vnet_feature_main_t * fm
#define dpdk_log_warn(...)
Definition: dpdk.h:436
dpdk_portid_t port_id
Definition: dpdk.h:202
uword clib_file_index
Definition: dpdk.h:172
description fragment has unexpected format
Definition: map.api:433
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
u16 rx_q_used
Definition: dpdk.h:197
vnet_main_t * vnet_get_main(void)
clib_file_main_t file_main
Definition: main.c:63
static clib_error_t * dpdk_rx_read_ready(clib_file_t *uf)
Definition: common.c:166
void dpdk_device_setup(dpdk_device_t *xd)
Definition: common.c:42
int __clib_unused rv
Definition: application.c:491
static_always_inline u8 vnet_hw_if_get_rx_queue_numa_node(vnet_main_t *vnm, u32 queue_index)
struct rte_eth_conf port_conf
Definition: dpdk.h:217
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
struct rte_eth_txconf tx_conf
Definition: dpdk.h:218
Definition: cJSON.c:88
u8 * description
Definition: file.h:70
void dpdk_device_start(dpdk_device_t *xd)
Definition: common.c:247
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
static_always_inline vnet_hw_if_rx_queue_t * vnet_hw_if_get_rx_queue(vnet_main_t *vnm, u32 queue_index)
u32 size
Definition: vhost_user.h:125
u32 flags
Definition: file.h:56
vnet_hw_if_rx_mode mode
Definition: interface.h:597
vnet_sw_interface_flags_t flags
Definition: interface.h:872
dpdk_tx_queue_t * tx_queues
Definition: dpdk.h:186
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
void vnet_hw_if_update_runtime_data(vnet_main_t *vnm, u32 hw_if_index)
Definition: runtime.c:58
void(* file_update)(clib_file_t *file, clib_file_update_type_t update_type)
Definition: file.h:90
u16 tx_q_used
Definition: dpdk.h:198
u16 nb_rx_desc
Definition: dpdk.h:208
uint16_t dpdk_portid_t
Definition: dpdk.h:129
#define dpdk_log_info(...)
Definition: dpdk.h:440
u32 hw_if_index
Definition: dpdk.h:191
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:208
dpdk_device_t * devices
Definition: dpdk.h:357
#define dpdk_log_err(...)
Definition: dpdk.h:434
dpdk_pmd_t pmd
Definition: dpdk.h:203
static int dpdk_port_state_callback_inline(dpdk_portid_t port_id, enum rte_eth_event_type type, void *param)
Definition: common.c:299
void dpdk_device_stop(dpdk_device_t *xd)
Definition: common.c:283
void dpdk_device_error(dpdk_device_t *xd, char *str, int rv)
Definition: common.c:33
#define ASSERT(truth)
format_function_t format_dpdk_device_name
Definition: dpdk.h:472
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
#define always_inline
Definition: rdma_mlx5dv.h:23
struct rte_eth_link link
Definition: dpdk.h:231
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
vl_api_ip4_address_t hi
Definition: arp.api:37
#define vec_elt(v, i)
Get vector value at index i.
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u32 queue_index
Definition: dpdk.h:170
format_function_t format_dpdk_rx_offload_caps
Definition: dpdk.h:481
#define clib_error_free(e)
Definition: error.h:86
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
i8 cpu_socket
Definition: dpdk.h:204
Definition: file.h:51
dpdk_rx_queue_t * rx_queues
Definition: dpdk.h:185
struct rte_mempool ** dpdk_mempool_by_buffer_pool_index
Definition: buffer.c:33
u8 bus
Definition: pci_types.api:21
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
vnet_main_t * vnet_main
Definition: dpdk.h:375
u16 nb_tx_desc
Definition: dpdk.h:207
static u8 vlib_buffer_pool_get_default_for_numa(vlib_main_t *vm, u32 numa_node)
Definition: buffer_funcs.h:189
void vnet_hw_if_set_rx_queue_file_index(vnet_main_t *vnm, u32 queue_index, u32 file_index)
Definition: rx_queue.c:144
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".