FD.io VPP  v19.01.2-3-gf61a1a8
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 <vlib/unix/cj.h>
20 #include <assert.h>
21 
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
25 #include <dpdk/device/dpdk.h>
26 
27 #include <dpdk/device/dpdk_priv.h>
28 #include <vppinfra/error.h>
29 
30 void
31 dpdk_device_error (dpdk_device_t * xd, char *str, int rv)
32 {
33  dpdk_log_err ("Interface %U error %d: %s",
34  format_dpdk_device_name, xd->port_id, rv, rte_strerror (rv));
35  xd->errors = clib_error_return (xd->errors, "%s[port:%d, errno:%d]: %s",
36  str, xd->port_id, rv, rte_strerror (rv));
37 }
38 
39 void
41 {
42  dpdk_main_t *dm = &dpdk_main;
43  vnet_main_t *vnm = vnet_get_main ();
46  struct rte_eth_dev_info dev_info;
47  u64 bitmap;
48  int rv;
49  int j;
50 
51  ASSERT (vlib_get_thread_index () == 0);
52 
53  clib_error_free (xd->errors);
55 
56  if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
57  {
59  dpdk_device_stop (xd);
60  }
61 
62  /* Enable flow director when flows exist */
63  if (xd->pmd == VNET_DPDK_PMD_I40E)
64  {
65  if ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) != 0)
66  xd->port_conf.fdir_conf.mode = RTE_FDIR_MODE_PERFECT;
67  else
68  xd->port_conf.fdir_conf.mode = RTE_FDIR_MODE_NONE;
69  }
70 
71  rte_eth_dev_info_get (xd->port_id, &dev_info);
72 
73  bitmap = xd->port_conf.txmode.offloads & ~dev_info.tx_offload_capa;
74  if (bitmap)
75  {
76  dpdk_log_warn ("unsupported tx offloads requested on port %u: %U",
78  xd->port_conf.txmode.offloads ^= bitmap;
79  }
80 
81  bitmap = xd->port_conf.rxmode.offloads & ~dev_info.rx_offload_capa;
82  if (bitmap)
83  {
84  dpdk_log_warn ("unsupported rx offloads requested on port %u: %U",
86  xd->port_conf.rxmode.offloads ^= bitmap;
87  }
88 
89  rv = rte_eth_dev_configure (xd->port_id, xd->rx_q_used,
90  xd->tx_q_used, &xd->port_conf);
91 
92  if (rv < 0)
93  {
94  dpdk_device_error (xd, "rte_eth_dev_configure", rv);
95  goto error;
96  }
97 
98  /* Set up one TX-queue per worker thread */
99  for (j = 0; j < xd->tx_q_used; j++)
100  {
101  rv =
102  rte_eth_tx_queue_setup (xd->port_id, j, xd->nb_tx_desc,
103  xd->cpu_socket, &xd->tx_conf);
104 
105  /* retry with any other CPU socket */
106  if (rv < 0)
107  rv =
108  rte_eth_tx_queue_setup (xd->port_id, j,
109  xd->nb_tx_desc, SOCKET_ID_ANY,
110  &xd->tx_conf);
111  if (rv < 0)
112  dpdk_device_error (xd, "rte_eth_tx_queue_setup", rv);
113  }
114 
117  for (j = 0; j < xd->rx_q_used; j++)
118  {
119  dpdk_mempool_private_t *privp;
121  xd->hw_if_index, j);
122  unsigned lcore = vlib_worker_threads[tidx].cpu_id;
123  u16 socket_id = rte_lcore_to_socket_id (lcore);
124 
125  rv =
126  rte_eth_rx_queue_setup (xd->port_id, j, xd->nb_rx_desc,
127  xd->cpu_socket, 0,
128  dm->pktmbuf_pools[socket_id]);
129 
130  /* retry with any other CPU socket */
131  if (rv < 0)
132  rv =
133  rte_eth_rx_queue_setup (xd->port_id, j,
134  xd->nb_rx_desc, SOCKET_ID_ANY, 0,
135  dm->pktmbuf_pools[socket_id]);
136 
137  privp = rte_mempool_get_priv (dm->pktmbuf_pools[socket_id]);
139 
140  if (rv < 0)
141  dpdk_device_error (xd, "rte_eth_rx_queue_setup", rv);
142  }
143 
144  if (vec_len (xd->errors))
145  goto error;
146 
147  rte_eth_dev_set_mtu (xd->port_id, hi->max_packet_bytes);
148 
149  if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
150  dpdk_device_start (xd);
151 
152  if (vec_len (xd->errors))
153  goto error;
154 
155  return;
156 
157 error:
158  xd->flags |= DPDK_DEVICE_FLAG_PMD_INIT_FAIL;
160 }
161 
162 void
164 {
165  int rv;
166 
167  if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
168  return;
169 
170  rv = rte_eth_dev_start (xd->port_id);
171 
172  if (rv)
173  {
174  dpdk_device_error (xd, "rte_eth_dev_start", rv);
175  return;
176  }
177 
178  if (xd->default_mac_address)
179  rv =
180  rte_eth_dev_default_mac_addr_set (xd->port_id,
181  (struct ether_addr *)
182  xd->default_mac_address);
183 
184  if (rv)
185  dpdk_device_error (xd, "rte_eth_dev_default_mac_addr_set", rv);
186 
187  if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
188  rte_eth_promiscuous_enable (xd->port_id);
189  else
190  rte_eth_promiscuous_disable (xd->port_id);
191 
192  rte_eth_allmulticast_enable (xd->port_id);
193 
194  if (xd->pmd == VNET_DPDK_PMD_BOND)
195  {
196  dpdk_portid_t slink[16];
197  int nlink = rte_eth_bond_slaves_get (xd->port_id, slink, 16);
198  while (nlink >= 1)
199  {
200  dpdk_portid_t dpdk_port = slink[--nlink];
201  rte_eth_allmulticast_enable (dpdk_port);
202  }
203  }
204 
205  dpdk_log_info ("Interface %U started",
207 }
208 
209 void
211 {
212  if (xd->flags & DPDK_DEVICE_FLAG_PMD_INIT_FAIL)
213  return;
214 
215  rte_eth_allmulticast_disable (xd->port_id);
216  rte_eth_dev_stop (xd->port_id);
217  clib_memset (&xd->link, 0, sizeof (struct rte_eth_link));
218 
219  /* For bonded interface, stop slave links */
220  if (xd->pmd == VNET_DPDK_PMD_BOND)
221  {
222  dpdk_portid_t slink[16];
223  int nlink = rte_eth_bond_slaves_get (xd->port_id, slink, 16);
224  while (nlink >= 1)
225  {
226  dpdk_portid_t dpdk_port = slink[--nlink];
227  rte_eth_dev_stop (dpdk_port);
228  }
229  }
230  dpdk_log_info ("Interface %U stopped",
232 }
233 
234 /* Even type for send_garp_na_process */
235 enum
236 {
239 
241 
242 static uword
245 {
246  uword event_type, *event_data = 0;
247 
248  while (1)
249  {
250  u32 i;
251  uword dpdk_port;
253  event_type = vlib_process_get_events (vm, &event_data);
254  ASSERT (event_type == SEND_GARP_NA);
255  for (i = 0; i < vec_len (event_data); i++)
256  {
257  dpdk_port = event_data[i];
258  if (i < 5) /* wait 0.2 sec for link to settle, max total 1 sec */
259  vlib_process_suspend (vm, 0.2);
260  dpdk_device_t *xd = &dpdk_main.devices[dpdk_port];
262  send_ip4_garp (vm, xd->sw_if_index);
263  send_ip6_na (vm, xd->sw_if_index);
264  }
265  vec_reset_length (event_data);
266  }
267  return 0;
268 }
269 
270 /* *INDENT-OFF* */
271 VLIB_REGISTER_NODE (send_garp_na_proc_node, static) = {
272  .function = send_garp_na_process,
273  .type = VLIB_NODE_TYPE_PROCESS,
274  .name = "send-garp-na-process",
275 };
276 /* *INDENT-ON* */
277 
278 void vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
279 
280 static void
282 {
284  ASSERT (vlib_get_thread_index () == 0);
286  (vm, send_garp_na_proc_node.index, SEND_GARP_NA, *dpdk_port);
287 }
288 
289 always_inline int
291  enum rte_eth_event_type type, void *param)
292 {
293  struct rte_eth_link link;
294  dpdk_device_t *xd = &dpdk_main.devices[port_id];
295 
296  RTE_SET_USED (param);
297  if (type != RTE_ETH_EVENT_INTR_LSC)
298  {
299  dpdk_log_info ("Unknown event %d received for port %d", type, port_id);
300  return -1;
301  }
302 
303  rte_eth_link_get_nowait (port_id, &link);
304  u8 link_up = link.link_status;
305 
306  if (xd->flags & DPDK_DEVICE_FLAG_BOND_SLAVE)
307  {
308  uword bd_port = xd->bond_port;
309  int bd_mode = rte_eth_bond_mode_get (bd_port);
310  dpdk_log_info ("Port %d state to %s, "
311  "slave of port %d BondEthernet%d in mode %d",
312  port_id, (link_up) ? "UP" : "DOWN",
313  bd_port, xd->bond_instance_num, bd_mode);
314  if (bd_mode == BONDING_MODE_ACTIVE_BACKUP)
315  {
317  (garp_na_proc_callback, (u8 *) & bd_port, sizeof (uword));
318  }
319 
320  if (link_up)
321  xd->flags |= DPDK_DEVICE_FLAG_BOND_SLAVE_UP;
322  else
323  xd->flags &= ~DPDK_DEVICE_FLAG_BOND_SLAVE_UP;
324  }
325  else /* Should not happen as callback not setup for "normal" links */
326  {
327  if (link_up)
328  dpdk_log_info ("Port %d Link Up - speed %u Mbps - %s",
329  port_id, (unsigned) link.link_speed,
330  (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
331  "full-duplex" : "half-duplex");
332  else
333  dpdk_log_info ("Port %d Link Down\n\n", port_id);
334  }
335 
336  return 0;
337 }
338 
339 int
341  enum rte_eth_event_type type,
342  void *param,
343  void *ret_param __attribute__ ((unused)))
344 {
345  return dpdk_port_state_callback_inline (port_id, type, param);
346 }
347 
348 /* If this device is PCI return pointer to info, otherwise NULL */
349 struct rte_pci_device *
350 dpdk_get_pci_device (const struct rte_eth_dev_info *info)
351 {
352  const struct rte_bus *bus;
353 
354  bus = rte_bus_find_by_device (info->device);
355  if (bus && !strcmp (bus->name, "pci"))
356  return RTE_DEV_TO_PCI (info->device);
357  else
358  return NULL;
359 }
360 
361 /*
362  * fd.io coding-style-patch-verification: ON
363  *
364  * Local Variables:
365  * eval: (c-set-style "gnu")
366  * End:
367  */
u8 * default_mac_address
Definition: dpdk.h:268
vmrglw vmrglh hi
format_function_t format_dpdk_tx_offload_caps
Definition: dpdk.h:525
void vl_api_force_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:634
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:623
dpdk_main_t dpdk_main
Definition: init.c:43
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
unsigned long u64
Definition: types.h:89
u32 sw_if_index
Definition: dpdk.h:208
#define NULL
Definition: clib.h:58
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:232
struct rte_pci_device * dpdk_get_pci_device(const struct rte_eth_dev_info *info)
Definition: common.c:350
u16 flags
Definition: dpdk.h:216
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
clib_error_t * errors
Definition: dpdk.h:271
int i
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_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:340
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:450
void send_ip6_na(vlib_main_t *vm, u32 sw_if_index)
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:452
#define dpdk_log_warn(...)
Definition: dpdk.h:506
dpdk_portid_t port_id
Definition: dpdk.h:205
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:546
#define always_inline
Definition: clib.h:98
#define clib_error_return(e, args...)
Definition: error.h:99
u16 rx_q_used
Definition: dpdk.h:229
unsigned int u32
Definition: types.h:88
void dpdk_device_setup(dpdk_device_t *xd)
Definition: common.c:40
struct rte_eth_conf port_conf
Definition: dpdk.h:233
static uword send_garp_na_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: common.c:243
struct rte_eth_txconf tx_conf
Definition: dpdk.h:234
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:36
void dpdk_device_start(dpdk_device_t *xd)
Definition: common.c:163
static_always_inline uword vnet_get_device_input_thread_index(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:127
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:964
unsigned short u16
Definition: types.h:57
vnet_sw_interface_flags_t flags
Definition: interface.h:706
u16 tx_q_used
Definition: dpdk.h:228
u16 nb_rx_desc
Definition: dpdk.h:230
uint16_t dpdk_portid_t
Definition: dpdk.h:124
static void garp_na_proc_callback(uword *dpdk_port)
Definition: common.c:281
#define dpdk_log_info(...)
Definition: dpdk.h:510
u32 hw_if_index
Definition: dpdk.h:207
void send_ip4_garp(vlib_main_t *vm, u32 sw_if_index)
Definition: arp.c:2554
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:212
dpdk_device_t * devices
Definition: dpdk.h:411
vlib_main_t * vm
Definition: buffer.c:301
#define dpdk_log_err(...)
Definition: dpdk.h:504
dpdk_pmd_t pmd
Definition: dpdk.h:213
static vlib_node_registration_t send_garp_na_proc_node
(constructor) VLIB_REGISTER_NODE (send_garp_na_proc_node)
Definition: common.c:240
static int dpdk_port_state_callback_inline(dpdk_portid_t port_id, enum rte_eth_event_type type, void *param)
Definition: common.c:290
void dpdk_device_stop(dpdk_device_t *xd)
Definition: common.c:210
void dpdk_device_error(dpdk_device_t *xd, char *str, int rv)
Definition: common.c:31
#define ASSERT(truth)
format_function_t format_dpdk_device_name
Definition: dpdk.h:515
struct rte_mempool ** pktmbuf_pools
Definition: dpdk.h:447
void dpdk_update_link_state(dpdk_device_t *xd, f64 now)
Definition: init.c:1494
struct rte_eth_link link
Definition: dpdk.h:256
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
struct _vlib_node_registration vlib_node_registration_t
enum @419 dpdk_send_garp_na_process_event_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
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:504
u64 uword
Definition: types.h:112
format_function_t format_dpdk_rx_offload_caps
Definition: dpdk.h:524
#define clib_error_free(e)
Definition: error.h:86
i8 cpu_socket
Definition: dpdk.h:214
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
vnet_main_t * vnet_main
Definition: dpdk.h:443
u16 nb_tx_desc
Definition: dpdk.h:218
u8 * buffer_pool_for_queue
Definition: dpdk.h:232
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".