FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
pp2.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 
23 #include <vlib/vlib.h>
24 #include <vlib/unix/unix.h>
25 #include <vppinfra/linux/syscall.h>
26 #include <vnet/plugin/plugin.h>
27 #include <marvell/pp2/pp2.h>
28 
29 /* size of DMA memory used by musdk (not used for buffers) */
30 #define MV_SYS_DMA_MEM_SZ (2 << 20)
31 /* number of HIFs reserved (first X) */
32 #define NUM_HIFS_RSVD 4
33 /* number of buffer pools reserved (first X) */
34 #define NUM_BPOOLS_RSVD 7
35 
38 
39 static void
41 {
43  int i;
45  {
47  i);
48  if (ptd->hif)
49  pp2_hif_deinit (ptd->hif);
50  vec_free (ptd->descs);
51  }
53  pp2_deinit ();
54  mv_sys_dma_mem_destroy ();
55 }
56 
57 static clib_error_t *
59 {
62  clib_error_t *err = 0;
63  struct pp2_init_params init_params = { 0 };
64  int i, rv;
65  u8 *s = 0;
66 
67  rv = mv_sys_dma_mem_init (MV_SYS_DMA_MEM_SZ);
68  if (rv)
69  return clib_error_return (0, "mv_sys_dma_mem_init failed, rv = %u", rv);
70 
71  init_params.hif_reserved_map = ((1 << NUM_HIFS_RSVD) - 1);
72  init_params.bm_pool_reserved_map = ((1 << NUM_BPOOLS_RSVD) - 1);
73  rv = pp2_init (&init_params);
74  if (rv)
75  {
76  err = clib_error_return (0, "mrvl_pp2_init failed, rv = %u", rv);
77  goto done;
78  }
79 
82 
84  {
86  i);
87  struct pp2_hif_params hif_params = { 0 };
88  vec_reset_length (s);
89  s = format (s, "hif-%d%c", NUM_HIFS_RSVD + i, 0);
90  hif_params.match = (char *) s;
91  hif_params.out_size = 2048; /* FIXME */
92  if (pp2_hif_init (&hif_params, &ptd->hif))
93  {
94  err = clib_error_return (0, "hif '%s' init failed", s);
95  goto done;
96  }
97  }
98 
99 done:
100  if (err)
102  vec_free (s);
103  return err;
104 }
105 
106 static u32
108  u32 flags)
109 {
110  /* nothing for now */
111  return 0;
112 }
113 
114 void
116 {
118  vnet_main_t *vnm = vnet_get_main ();
120  mrvl_pp2_outq_t *outq;
121  mrvl_pp2_inq_t *inq;
122  int i;
123 
124  if (ppif->hw_if_index != ~0)
125  {
126  vec_foreach_index (i, ppif->inqs)
129  }
130 
131  if (ppif->ppio)
132  {
133  pp2_ppio_disable (ppif->ppio);
134  pp2_ppio_deinit (ppif->ppio);
135  }
136 
137  /* *INDENT-OFF* */
138  /* free buffers hanging in the tx ring */
139  vec_foreach (outq, ppif->outqs)
140  {
141  while (outq->tail < outq->head)
142  {
143  u16 slot = outq->tail & (outq->size - 1);
144  vlib_buffer_free (vm, outq->buffers + slot, 1);
145  outq->tail++;
146  }
147  vec_free (outq->buffers);
148  }
149  vec_free (ppif->outqs);
150 
151  /* free buffers hangin in the rx buffer pool */
152  vec_foreach (inq, ppif->inqs)
153  if (inq->bpool)
154  {
155  u32 n_bufs = 0;
156  pp2_bpool_get_num_buffs (inq->bpool, &n_bufs);
157  while (n_bufs--)
158  {
159  struct pp2_buff_inf binf;
160  if (pp2_bpool_get_buff
161  (ppm->per_thread_data[0].hif, inq->bpool, &binf) == 0)
162  vlib_buffer_free (vm, &binf.cookie, 1);
163  }
164  pp2_bpool_deinit (inq->bpool);
165  }
166  vec_free (ppif->inqs);
167  /* *INDENT-ON* */
168 
169 
170  pool_put (ppm->interfaces, ppif);
171 
172  if (pool_elts (ppm->interfaces) == 0)
174 }
175 
176 void
178 {
179  vnet_main_t *vnm = vnet_get_main ();
182  struct pp2_bpool_params bpool_params = { 0 };
183  struct pp2_ppio_params ppio_params = { 0 };
184  struct pp2_ppio_inq_params inq_params = { 0 };
186  mrvl_pp2_if_t *ppif = 0;
187  u8 pp2_id, port_id, *s = 0;
188  eth_addr_t mac_addr;
189  u8 n_outqs, n_inqs = 1;
190  int i;
191 
192  if (tm->n_vlib_mains > PP2_PPIO_MAX_NUM_OUTQS)
193  {
194  args->rv = VNET_API_ERROR_INIT_FAILED;
195  args->error = clib_error_return (0, "number of threads (main + workers)"
196  " is bigger than number of output "
197  "queues (%u)", PP2_PPIO_MAX_NUM_OUTQS);
198  return;
199  }
200  n_outqs = tm->n_vlib_mains;
201 
202  /* defaults */
203  args->tx_q_sz = args->tx_q_sz ? args->tx_q_sz : 2048;
204  args->rx_q_sz = args->rx_q_sz ? args->rx_q_sz : 2048;
205 
206  if (vec_len (ppm->per_thread_data) == 0)
207  {
208  if ((args->error = mrvl_pp2_main_init ()) != 0)
209  {
210  args->rv = VNET_API_ERROR_INIT_FAILED;
211  return;
212  }
213  }
214 
215  pool_get (ppm->interfaces, ppif);
216  memset (ppif, 0, sizeof (*ppif));
217  ppif->dev_instance = ppif - ppm->interfaces;
218  ppif->hw_if_index = ~0;
219  vec_validate_aligned (ppif->inqs, n_inqs - 1, CLIB_CACHE_LINE_BYTES);
220  vec_validate_aligned (ppif->outqs, n_outqs - 1, CLIB_CACHE_LINE_BYTES);
221 
222  for (i = 0; i < n_inqs; i++)
223  {
224  mrvl_pp2_inq_t *inq = vec_elt_at_index (ppif->inqs, i);
225  inq->size = args->rx_q_sz;
226  }
227  for (i = 0; i < n_outqs; i++)
228  {
229  mrvl_pp2_outq_t *outq = vec_elt_at_index (ppif->outqs, i);
230  outq->size = args->tx_q_sz;
232  }
233 
234  if (pp2_netdev_get_ppio_info ((char *) args->name, &pp2_id, &port_id))
235  {
236  args->rv = VNET_API_ERROR_INVALID_INTERFACE;
237  args->error = clib_error_return (0, "Invalid interface '%s'",
238  args->name);
239  goto error;
240  }
241 
242  /* FIXME bpool bit select per pp */
243  s = format (s, "pool-%d:%d%c", pp2_id, pp2_id + 8, 0);
244  bpool_params.match = (char *) s;
245  bpool_params.buff_len = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES;
246  /* FIXME +64 ? */
247  if (pp2_bpool_init (&bpool_params, &ppif->inqs[0].bpool))
248  {
249  args->rv = VNET_API_ERROR_INIT_FAILED;
250  args->error = clib_error_return (0, "bpool '%s' init failed", s);
251  goto error;
252  }
253  vec_reset_length (s);
254 
255  s = format (s, "ppio-%d:%d%c", pp2_id, port_id, 0);
256  ppio_params.match = (char *) s;
257  ppio_params.type = PP2_PPIO_T_NIC;
258  inq_params.size = 2048;
259  ppio_params.inqs_params.num_tcs = 1;
260  ppio_params.inqs_params.tcs_params[0].pkt_offset = 0;
261  ppio_params.inqs_params.tcs_params[0].num_in_qs = n_inqs;
262  ppio_params.inqs_params.tcs_params[0].inqs_params = &inq_params;
263  ppio_params.inqs_params.tcs_params[0].pools[0] = ppif->inqs[0].bpool;
264  ppio_params.outqs_params.num_outqs = n_outqs;
265  for (i = 0; i < n_outqs; i++)
266  {
267  ppio_params.outqs_params.outqs_params[i].weight = 1;
268  ppio_params.outqs_params.outqs_params[i].size = args->tx_q_sz;
269  }
270  if (pp2_ppio_init (&ppio_params, &ppif->ppio))
271  {
272  args->rv = VNET_API_ERROR_INIT_FAILED;
273  args->error = clib_error_return (0, "ppio '%s' init failed", s);
274  goto error;
275  }
276  vec_reset_length (s);
277 
278  if (pp2_ppio_get_mac_addr (ppif->ppio, mac_addr))
279  {
280  args->rv = VNET_API_ERROR_INIT_FAILED;
281  args->error =
282  clib_error_return (0, "%s: pp2_ppio_get_mac_addr failed", s);
283  goto error;
284  }
285 
287  ppif->dev_instance,
288  mac_addr,
289  &ppif->hw_if_index,
291  if (args->error)
292  {
293  args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
294  goto error;
295  }
296 
297  sw = vnet_get_hw_sw_interface (vnm, ppif->hw_if_index);
298  ppif->sw_if_index = sw->sw_if_index;
299  ppif->per_interface_next_index = ~0;
301  mrvl_pp2_input_node.index);
307  goto done;
308 
309 error:
310  mrvl_pp2_delete_if (ppif);
311 done:
312  vec_free (s);
313 }
314 
315 static clib_error_t *
317  u32 flags)
318 {
320  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
322  static clib_error_t *error = 0;
323  int is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
324  int rv;
325 
326  if (is_up)
327  rv = pp2_ppio_enable (ppif->ppio);
328  else
329  rv = pp2_ppio_disable (ppif->ppio);
330 
331  if (rv)
332  return clib_error_return (0, "failed to %s interface",
333  is_up ? "enable" : "disable");
334 
335  if (is_up)
336  ppif->flags |= MRVL_PP2_IF_F_ADMIN_UP;
337  else
338  ppif->flags &= ~MRVL_PP2_IF_F_ADMIN_UP;
339 
340  return error;
341 }
342 
343 static void
345 {
347  mrvl_pp2_if_t *ppif = pool_elt_at_index (ppm->interfaces, instance);
348  struct pp2_ppio_statistics stats;
349 
350  pp2_ppio_get_statistics (ppif->ppio, &stats, 1);
351 }
352 
353 static void
355  u32 node_index)
356 {
358  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
360 
361  /* Shut off redirection */
362  if (node_index == ~0)
363  {
364  ppif->per_interface_next_index = node_index;
365  return;
366  }
367 
370  node_index);
371 }
372 
374 #define _(n,s) s,
376 #undef _
377 };
378 
379 /* *INDENT-OFF* */
381 {
382  .name = "Marvell PPv2 interface",
383  .format_device_name = format_mrvl_pp2_interface_name,
384  .format_device = format_mrvl_pp2_interface,
385  .tx_function = mrvl_pp2_interface_tx,
386  .tx_function_n_errors = MRVL_PP2_TX_N_ERROR,
387  .tx_function_error_strings = mrvl_pp2_tx_func_error_strings,
388  .admin_up_down_function = mrvl_pp2_interface_admin_up_down,
389  .clear_counters = mrvl_pp2_clear_interface_counters,
390  .rx_redirect_to_node = mrvl_pp2_set_interface_next_node,
391 };
392 /* *INDENT-ON* */
393 
394 static clib_error_t *
396 {
397  return 0;
398 }
399 
401 
402 /*
403  * fd.io coding-style-patch-verification: ON
404  *
405  * Local Variables:
406  * eval: (c-set-style "gnu")
407  * End:
408  */
struct pp2_ppio * ppio
Definition: pp2.h:53
static u32 mrvl_pp2_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
Definition: pp2.c:107
mrvl_pp2_outq_t * outqs
Definition: pp2.h:57
vmrglw vmrglh hi
#define vec_foreach_index(var, v)
Iterate over vector indices.
#define NUM_BPOOLS_RSVD
Definition: pp2.c:34
u32 dev_instance
Definition: pp2.h:59
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:541
u16 head
Definition: pp2.h:44
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:534
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:319
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
struct pp2_ppio_desc * descs
Definition: pp2.h:70
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 per_interface_next_index
Definition: pp2.h:54
mrvl_pp2_main_t mrvl_pp2_main
Definition: pp2.c:36
int i
vnet_device_class_t mrvl_pp2_device_class
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:458
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:448
#define MV_SYS_DMA_MEM_SZ
Definition: pp2.c:30
static clib_error_t * mrvl_pp2_main_init()
Definition: pp2.c:58
struct _vnet_device_class vnet_device_class_t
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:227
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1110
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
u32 hw_if_index
Definition: pp2.h:61
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
static clib_error_t * mrvl_pp2_init(vlib_main_t *vm)
Definition: pp2.c:395
struct pp2_bpool * bpool
Definition: pp2.h:36
static char * mrvl_pp2_tx_func_error_strings[]
Definition: pp2.c:373
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
#define foreach_mrvl_pp2_tx_func_error
Definition: pp2.h:100
format_function_t format_mrvl_pp2_interface
Definition: pp2.h:129
u32 sw_if_index
Definition: pp2.h:60
#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
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:440
unsigned int u32
Definition: types.h:88
u16 size
Definition: pp2.h:42
struct pp2_hif * hif
Definition: pp2.h:69
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
unsigned short u16
Definition: types.h:57
static clib_error_t * mrvl_pp2_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: pp2.c:316
u32 flags
Definition: pp2.h:51
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:273
vlib_node_registration_t mrvl_pp2_input_node
(constructor) VLIB_REGISTER_NODE (mrvl_pp2_input_node)
Definition: input.c:374
VNET_DEVICE_CLASS(mrvl_pp2_device_class,)
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
u32 * buffers
Definition: pp2.h:43
void mrvl_pp2_delete_if(mrvl_pp2_if_t *ppif)
Definition: pp2.c:115
#define NUM_HIFS_RSVD
Definition: pp2.c:32
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:660
void vnet_hw_interface_assign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, uword thread_index)
Definition: devices.c:138
static void mrvl_pp2_clear_interface_counters(u32 instance)
Definition: pp2.c:344
void mrvl_pp2_create_if(mrvl_pp2_create_if_args_t *args)
Definition: pp2.c:177
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:273
u16 tail
Definition: pp2.h:45
static void mrvl_pp2_set_interface_next_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: pp2.c:354
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
format_function_t format_mrvl_pp2_interface_name
Definition: pp2.h:130
uword mrvl_pp2_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: output.c:31
int vnet_hw_interface_unassign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.c:187
clib_error_t * error
Definition: pp2.h:92
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
#define vec_foreach(var, vec)
Vector iterator.
int vnet_hw_interface_set_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode mode)
Definition: devices.c:252
mrvl_pp2_if_t * interfaces
Definition: pp2.h:77
mrvl_pp2_per_thread_data_t * per_thread_data
Definition: pp2.h:78
static void mrvl_pp2_main_deinit()
Definition: pp2.c:40
u32 flags
Definition: vhost-user.h:77
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:62
mrvl_pp2_inq_t * inqs
Definition: pp2.h:56
vnet_device_class_t ppa2_device_class
static void vnet_hw_interface_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: devices.h:79
#define MRVL_PP2_IF_F_ADMIN_UP
Definition: pp2.h:52
u16 size
Definition: pp2.h:35
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128