FD.io VPP  v21.10.1-2-g0a485f517
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  {
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  }
225  f.read_function = dpdk_rx_read_ready;
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 =
238  pool_elt_at_index (fm->file_pool, rxq->clib_file_index);
239  fm->file_update (f, UNIX_FILE_UPDATE_DELETE);
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  */
clib_spinlock_init
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
clib_file::file_descriptor
u32 file_descriptor
Definition: file.h:54
dpdk.h
file_main
clib_file_main_t file_main
Definition: main.c:63
VNET_SW_INTERFACE_FLAG_ERROR
@ VNET_SW_INTERFACE_FLAG_ERROR
Definition: interface.h:859
vnet_sw_interface_t
Definition: interface.h:869
dpdk_device_start
void dpdk_device_start(dpdk_device_t *xd)
Definition: common.c:247
vlib_buffer_pool_t::index
u8 index
Definition: buffer.h:456
dpdk_device_t::flags
u16 flags
Definition: dpdk.h:200
dpdk_main_t::devices
dpdk_device_t * devices
Definition: dpdk.h:361
dpdk_rx_queue_t::clib_file_index
uword clib_file_index
Definition: dpdk.h:173
dpdk_get_pci_device
struct rte_pci_device * dpdk_get_pci_device(const struct rte_eth_dev_info *info)
Definition: common.c:335
vnet_hw_if_rx_queue_t::mode
vnet_hw_if_rx_mode mode
Definition: interface.h:597
dpdk_portid_t
uint16_t dpdk_portid_t
Definition: dpdk.h:130
f
vlib_frame_t * f
Definition: interface_output.c:1098
vnet_hw_if_get_rx_queue_numa_node
static_always_inline u8 vnet_hw_if_get_rx_queue_numa_node(vnet_main_t *vnm, u32 queue_index)
Definition: rx_queue_funcs.h:84
dpdk_rx_queue_t
Definition: dpdk.h:167
vnet_hw_if_get_rx_queue
static_always_inline vnet_hw_if_rx_queue_t * vnet_hw_if_get_rx_queue(vnet_main_t *vnm, u32 queue_index)
Definition: rx_queue_funcs.h:43
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
dpdk_main_t::vnet_main
vnet_main_t * vnet_main
Definition: dpdk.h:379
vlib_buffer_pool_t
Definition: buffer.h:450
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
dpdk_device_t::default_mac_address
u8 * default_mac_address
Definition: dpdk.h:242
dpdk_setup_interrupts
static void dpdk_setup_interrupts(dpdk_device_t *xd)
Definition: common.c:186
vnet_hw_if_rx_queue_t::dev_instance
u32 dev_instance
Definition: interface.h:585
clib_file::private_data
u64 private_data
Definition: file.h:64
VNET_HW_IF_RX_MODE_POLLING
@ VNET_HW_IF_RX_MODE_POLLING
Definition: interface.h:56
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
fm
vnet_feature_main_t * fm
Definition: nat44_ei_hairpinning.c:592
vnet_get_sw_interface
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:58
dpdk_device_t::port_id
dpdk_portid_t port_id
Definition: dpdk.h:203
dpdk_device_setup
void dpdk_device_setup(dpdk_device_t *xd)
Definition: common.c:42
dpdk_port_state_callback
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
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
dpdk_device_t::errors
clib_error_t * errors
Definition: dpdk.h:245
clib_file_main_t
Definition: file.h:85
vlib_get_buffer_pool
static_always_inline vlib_buffer_pool_t * vlib_get_buffer_pool(vlib_main_t *vm, u8 buffer_pool_index)
Definition: buffer_funcs.h:552
ethernet.h
error
Definition: cJSON.c:88
vnet_sw_interface_t::flags
vnet_sw_interface_flags_t flags
Definition: interface.h:873
dpdk_log_info
#define dpdk_log_info(...)
Definition: dpdk.h:444
dpdk_device_t::tx_queues
dpdk_tx_queue_t * tx_queues
Definition: dpdk.h:187
vec_elt
#define vec_elt(v, i)
Get vector value at index i.
Definition: vec_bootstrap.h:210
vlib_thread_main_t::n_vlib_mains
u32 n_vlib_mains
Definition: threads.h:262
vnet_hw_if_rx_queue_t::queue_id
u32 queue_id
Definition: interface.h:594
dpdk_device_t::hw_if_index
u32 hw_if_index
Definition: dpdk.h:192
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
UNIX_FILE_EVENT_EDGE_TRIGGERED
#define UNIX_FILE_EVENT_EDGE_TRIGGERED
Definition: file.h:58
error.h
dpdk_get_vmbus_device
struct rte_vmbus_device * dpdk_get_vmbus_device(const struct rte_eth_dev_info *info)
Definition: common.c:348
dpdk_device_t::rx_q_used
u16 rx_q_used
Definition: dpdk.h:198
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vnet_get_hw_interface
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:44
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
clib_file
Definition: file.h:51
vlib_get_thread_index
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:187
dpdk_main
dpdk_main_t dpdk_main
Definition: init.c:48
dpdk_device_t::port_conf
struct rte_eth_conf port_conf
Definition: dpdk.h:218
rx_queue_funcs.h
dpdk_main_t
Definition: dpdk.h:357
vec_validate_aligned
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:534
dpdk_log_warn
#define dpdk_log_warn(...)
Definition: dpdk.h:440
buffer.h
vnet_hw_if_update_runtime_data
void vnet_hw_if_update_runtime_data(vnet_main_t *vnm, u32 hw_if_index)
Definition: runtime.c:58
dpdk_device_t::tx_conf
struct rte_eth_txconf tx_conf
Definition: dpdk.h:219
format.h
vnet_hw_if_rx_queue_set_int_pending
static_always_inline void vnet_hw_if_rx_queue_set_int_pending(vnet_main_t *vnm, u32 queue_index)
Definition: rx_queue_funcs.h:52
dpdk_device_t::tx_q_used
u16 tx_q_used
Definition: dpdk.h:199
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:58
dpdk_device_t::nb_rx_desc
u16 nb_rx_desc
Definition: dpdk.h:209
vnet_hw_if_set_rx_queue_file_index
void vnet_hw_if_set_rx_queue_file_index(vnet_main_t *vnm, u32 queue_index, u32 file_index)
Definition: rx_queue.c:144
UNIX_FILE_UPDATE_DELETE
@ UNIX_FILE_UPDATE_DELETE
Definition: file.h:82
data
u8 data[128]
Definition: ipsec_types.api:95
dpdk_mempool_by_buffer_pool_index
struct rte_mempool ** dpdk_mempool_by_buffer_pool_index
Definition: buffer.c:33
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
size
u32 size
Definition: vhost_user.h:125
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
dpdk_log_err
#define dpdk_log_err(...)
Definition: dpdk.h:438
dpdk_port_state_callback_inline
static int dpdk_port_state_callback_inline(dpdk_portid_t port_id, enum rte_eth_event_type type, void *param)
Definition: common.c:299
u64
unsigned long u64
Definition: types.h:89
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
dpdk_device_t::link
struct rte_eth_link link
Definition: dpdk.h:232
vlib_frame_t::flags
u16 flags
Definition: node.h:378
dpdk_device_stop
void dpdk_device_stop(dpdk_device_t *xd)
Definition: common.c:283
ip.h
u32
unsigned int u32
Definition: types.h:88
dpdk_device_error
void dpdk_device_error(dpdk_device_t *xd, char *str, int rv)
Definition: common.c:33
vlib_thread_main_t
Definition: threads.h:243
dpdk_device_t::cpu_socket
i8 cpu_socket
Definition: dpdk.h:205
dpdk_rx_read_ready
static clib_error_t * dpdk_rx_read_ready(clib_file_t *uf)
Definition: common.c:166
clib_file_add
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
dpdk_device_t::pmd
dpdk_pmd_t pmd
Definition: dpdk.h:204
vec.h
dpdk_rx_queue_t::efd
int efd
Definition: dpdk.h:172
vlib_buffer_pool_get_default_for_numa
static u8 vlib_buffer_pool_get_default_for_numa(vlib_main_t *vm, u32 numa_node)
Definition: buffer_funcs.h:189
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vnet_hw_interface_set_flags
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
format_dpdk_tx_offload_caps
format_function_t format_dpdk_tx_offload_caps
Definition: dpdk.h:487
unix.h
arp_packet.h
file.h
vnet_hw_if_rx_queue_t
Definition: interface.h:579
VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE
@ VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE
Definition: interface.h:550
clib_error_free
#define clib_error_free(e)
Definition: error.h:86
dpdk_device_t::nb_tx_desc
u16 nb_tx_desc
Definition: dpdk.h:208
rv
int __clib_unused rv
Definition: application.c:491
dpdk_rx_queue_t::buffer_pool_index
u8 buffer_pool_index
Definition: dpdk.h:170
vnet.h
vlib_get_thread_main
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
format_dpdk_device_name
format_function_t format_dpdk_device_name
Definition: dpdk.h:477
dpdk_rx_queue_t::queue_index
u32 queue_index
Definition: dpdk.h:171
vl_api_force_rpc_call_main_thread
void vl_api_force_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:631
dpdk_device_t::sw_if_index
u32 sw_if_index
Definition: dpdk.h:193
bus
u8 bus
Definition: pci_types.api:21
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
dpdk_priv.h
format_dpdk_rx_offload_caps
format_function_t format_dpdk_rx_offload_caps
Definition: dpdk.h:486
dpdk_device_t::rx_queues
dpdk_rx_queue_t * rx_queues
Definition: dpdk.h:186
dpdk_device_t
Definition: dpdk.h:182