aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/odp/odp_packet.c
blob: 29b2ef01e540f089b68ea8ac2acb10fd55442832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* Copyright (c) 2017, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>
#include <odp/ipsec/ipsec.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>
#include <odp/odp_packet.h>
#include <odp/ipsec/ipsec.h>

odp_packet_main_t *odp_packet_main;
u32 rx_sched_wait;
u32 tx_burst_size;
u32 num_pkts_in_pool = SHM_PKT_POOL_NB_PKTS;
odp_if_mode_t def_if_mode;
odp_if_config_t *if_config;
odp_crypto_main_t odp_crypto_main;
u8 enable_odp_crypto;
u8 ipsec_api;
u8 is_async;
u8 is_inline;

static u32
odp_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
			    u32 flags)
{
  /* nothing for now */
  return 0;
}

static odp_pktio_t
create_pktio (const char *dev, odp_pool_t pool, odp_packet_if_t * oif)
{
  odp_pktio_t pktio;
  int ret;
  odp_pktio_param_t pktio_param;
  odp_pktin_queue_param_t pktin_param;
  odp_pktout_queue_param_t pktout_param;
  odp_pktio_config_t pktio_config;
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  odp_pktio_capability_t capa;

  odp_pktio_param_init (&pktio_param);
  odp_pktio_config_init (&pktio_config);
  odp_pktin_queue_param_init (&pktin_param);
  odp_pktout_queue_param_init (&pktout_param);

  switch (oif->m.tx_mode)
    {
    case APPL_MODE_PKT_BURST:
      pktio_param.out_mode = ODP_PKTOUT_MODE_DIRECT;
      break;
    case APPL_MODE_PKT_QUEUE:
      pktio_param.out_mode = ODP_PKTOUT_MODE_QUEUE;
      break;
    case APPL_MODE_PKT_TM:
      clib_error ("Traffic Manager mode not supported!\n");
      pktio_param.out_mode = ODP_PKTOUT_MODE_TM;
      break;
    default:
      clib_warning ("Invalid TX mode\n");
    }

  switch (oif->m.rx_mode)
    {
    case APPL_MODE_PKT_BURST:
      pktio_param.in_mode = ODP_PKTIN_MODE_DIRECT;
      break;
    case APPL_MODE_PKT_QUEUE:
      pktio_param.in_mode = ODP_PKTIN_MODE_QUEUE;
      break;
    case APPL_MODE_PKT_SCHED_ATOMIC:
      pktio_param.in_mode = ODP_PKTIN_MODE_SCHED;
      pktin_param.queue_param.sched.sync = ODP_SCHED_SYNC_ATOMIC;
      pktin_param.queue_param.sched.group = oif->sched_group;
      break;
    case APPL_MODE_PKT_SCHED_ORDERED:
      pktio_param.in_mode = ODP_PKTIN_MODE_SCHED;
      pktin_param.queue_param.sched.sync = ODP_SCHED_SYNC_ORDERED;
      pktin_param.queue_param.sched.group = oif->sched_group;
      /* Force Tx Queue mode */
      oif->m.tx_mode = APPL_MODE_PKT_QUEUE;
      pktio_param.out_mode = ODP_PKTOUT_MODE_QUEUE;
      break;
    case APPL_MODE_PKT_SCHED_PARALLEL:
      pktio_param.in_mode = ODP_PKTIN_MODE_SCHED;
      pktin_param.queue_param.sched.sync = ODP_SCHED_SYNC_PARALLEL;
      pktin_param.queue_param.sched.group = oif->sched_group;
      break;
    default:
      clib_warning ("Invalid RX mode\n");
    }

  /* Open a packet IO instance */
  pktio = odp_pktio_open (dev, pool, &pktio_param);

  if (pktio == ODP_PKTIO_INVALID)
    {
      clib_warning ("Error: pktio create failed for %s", dev);
      return 0;
    }

  if (odp_pktio_capability (pktio, &capa))
    {
      clib_warning ("Error: capability query failed %s\n", dev);
      return 0;
    }

  /* Disable ODP parser for performance */
  pktio_config.parser.layer = ODP_PKTIO_PARSER_LAYER_NONE;
  odp_pktio_config (pktio, &pktio_config);

  if (oif->m.num_rx_queues > capa.max_input_queues)
    {
      oif->m.num_rx_queues = capa.max_input_queues;
      clib_warning ("pktio %s: Number of RX queues limited to %d\n",
		    dev, oif->m.num_rx_queues);
    }

  if (oif->m.num_rx_queues > 1)
    {
      if (oif->m.num_rx_queues > MAX_QUEUES)
	oif->m.num_rx_queues = MAX_QUEUES;

      pktin_param.num_queues = oif->m.num_rx_queues;
      pktin_param.classifier_enable = 0;
      pktin_param.hash_enable = 1;
      pktin_param.hash_proto.proto.ipv4_udp = 1;
      pktin_param.hash_proto.proto.ipv4_tcp = 1;
      pktin_param.hash_proto.proto.ipv4 = 1;
    }
  else
    oif->m.num_rx_queues = 1;

  pktin_param.op_mode = ODP_PKTIO_OP_MT_UNSAFE;
  if (odp_pktin_queue_config (pktio, &pktin_param))
    {
      clib_warning ("Error: pktin config failed");
    }

  if ((capa.max_output_queues >= tm->n_vlib_mains) &&
      (oif->m.num_tx_queues == 0))
    {
      pktout_param.op_mode = ODP_PKTIO_OP_MT_UNSAFE;
      pktout_param.num_queues = tm->n_vlib_mains;
      oif->m.num_tx_queues = tm->n_vlib_mains;
    }
  else
    {
      oif->m.num_tx_queues = 1;
    }

  if (odp_pktout_queue_config (pktio, &pktout_param))
    {
      clib_warning ("Error: pktout config failed");
    }

  ret = odp_pktio_start (pktio);
  if (ret != 0)
    {
      clib_warning ("Error: unable to start");
    }

  return pktio;
}

u32
odp_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
		      u32 * sw_if_index, odp_if_mode_t * mode)
{
  odp_packet_main_t *om = odp_packet_main;
  int ret = 0, j;
  odp_packet_if_t *oif = 0;
  u8 hw_addr[6];
  clib_error_t *error = 0;
  vnet_sw_interface_t *sw;
  vnet_main_t *vnm = vnet_get_main ();
  uword *p;
  odp_thrmask_t thrmask;

  p = mhash_get (&om->if_index_by_host_if_name, host_if_name);
  if (p)
    return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;

  pool_get (om->interfaces, oif);
  oif->if_index = oif - om->interfaces;
  oif->host_if_name = vec_dup (host_if_name);
  oif->per_interface_next_index = ~0;
  oif->m = *mode;

  /* Create new schedule group */
  if ((oif->m.rx_mode != APPL_MODE_PKT_BURST) &&
      (oif->m.rx_mode != APPL_MODE_PKT_QUEUE))
    {
      odp_thrmask_zero (&thrmask);
      oif->sched_group = odp_schedule_group_create (NULL, &thrmask);
    }

  /* Create a pktio instance */
  oif->pktio = create_pktio ((char *) host_if_name, om->pool, oif);
  if (oif->pktio == 0)
    {
      ret = VNET_API_ERROR_INVALID_INTERFACE;
      goto error;
    }

  om->if_count++;

  if (oif->m.rx_mode == APPL_MODE_PKT_BURST)
    odp_pktin_queue (oif->pktio, oif->inq, oif->m.num_rx_queues);
  else if (oif->m.rx_mode == APPL_MODE_PKT_QUEUE)
    odp_pktin_event_queue (oif->pktio, oif->rxq, oif->m.num_rx_queues);
  if (oif->m.tx_mode == APPL_MODE_PKT_BURST)
    odp_pktout_queue (oif->pktio, oif->outq, oif->m.num_tx_queues);
  else if (oif->m.tx_mode == APPL_MODE_PKT_QUEUE)
    odp_pktout_event_queue (oif->pktio, oif->txq, oif->m.num_tx_queues);

  /* Use configured MAC or read MAC from DPDK */
  if (hw_addr_set)
    clib_memcpy (hw_addr, hw_addr_set, 6);
  else if (odp_pktio_mac_addr (oif->pktio, hw_addr, sizeof (hw_addr)) < 6)
    {
      /* Failed to read MAC from driver, set random MAC */
      f64 now = vlib_time_now (vm);
      u32 rnd;
      rnd = (u32) (now * 1e6);
      rnd = random_u32 (&rnd);

      clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
      hw_addr[0] = 2;
      hw_addr[1] = 0xfe;
    }

  error = ethernet_register_interface (vnm, odp_packet_device_class.index,
				       oif->if_index, hw_addr,
				       &oif->hw_if_index,
				       odp_packet_eth_flag_change);

  if (error)
    {
      clib_error_report (error);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  sw = vnet_get_hw_sw_interface (vnm, oif->hw_if_index);
  oif->sw_if_index = sw->sw_if_index;
  vnet_hw_interface_set_input_node (vnm, oif->hw_if_index,
				    odp_packet_input_node.index);

  vnet_hw_interface_set_flags (vnm, oif->hw_if_index,
			       VNET_HW_INTERFACE_FLAG_LINK_UP);

  mhash_set_mem (&om->if_index_by_host_if_name, oif->host_if_name,
		 &oif->if_index, 0);
  if (sw_if_index)
    *sw_if_index = oif->sw_if_index;

  /* Assign queues of the new interface to first available worker thread */
  for (j = 0; j < oif->m.num_rx_queues; j++)
    vnet_hw_interface_assign_rx_thread (vnm, oif->hw_if_index, j, -1);

  return 0;

error:
  vec_free (oif->host_if_name);
  memset (oif, 0, sizeof (*oif));
  pool_put (om->interfaces, oif);

  return ret;
}

u32
odp_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
{
  vnet_main_t *vnm = vnet_get_main ();
  odp_packet_main_t *om = odp_packet_main;
  odp_packet_if_t *oif = 0;
  uword *p;
  int q;

  p = mhash_get (&om->if_index_by_host_if_name, host_if_name);

  if (p == NULL)
    {
      clib_warning ("Host interface %s does not exist", host_if_name);
      return VNET_API_ERROR_SYSCALL_ERROR_1;
    }

  oif = pool_elt_at_index (om->interfaces, p[0]);
  vnet_hw_interface_set_flags (vnm, oif->hw_if_index, 0);

  for (q = 0; q < oif->m.num_rx_queues; q++)
    vnet_hw_interface_unassign_rx_thread (vnm, oif->hw_if_index, q);

  om->if_count--;

  odp_pktio_stop (odp_pktio_lookup ((char *) host_if_name));
  odp_pktio_close (odp_pktio_lookup ((char *) host_if_name));

  vec_free (oif->host_if_name);
  oif->host_if_name = NULL;

  mhash_unset (&om->if_index_by_host_if_name, host_if_name, &oif->if_index);
  ethernet_delete_interface (vnm, oif->hw_if_index);

  pool_put (om->interfaces, oif);

  return 0;
}

static clib_error_t *
odp_device_config (char *name, unformat_input_t * input)
{
  odp_if_mode_t mode;
  odp_if_config_t *config;
  char *val;
  u32 num;
  u8 set_hw_addr = 0;
  u8 hw_addr[6];

  mode = def_if_mode;

  if (input)
    {
      unformat_skip_white_space (input);
      while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
	{
	  if (unformat (input, "rx-mode %s", &val))
	    {
	      if (!strcmp (val, "burst") || !strcmp (val, "0"))
		mode.rx_mode = APPL_MODE_PKT_BURST;
	      else if (!strcmp (val, "queue") || !strcmp (val, "1"))
		mode.rx_mode = APPL_MODE_PKT_QUEUE;
	      else if (!strcmp (val, "sched-p") || !strcmp (val, "4"))
		mode.rx_mode = APPL_MODE_PKT_SCHED_PARALLEL;
	      else if (!strcmp (val, "sched-o") || !strcmp (val, "3"))
		mode.rx_mode = APPL_MODE_PKT_SCHED_ORDERED;
	      else if (!strcmp (val, "sched") || !strcmp (val, "2"))
		mode.rx_mode = APPL_MODE_PKT_SCHED_ATOMIC;
	      vec_free (val);
	    }
	  else if (unformat (input, "tx-mode %s", &val))
	    {
	      if (!strcmp (val, "burst") || !strcmp (val, "0"))
		mode.tx_mode = APPL_MODE_PKT_BURST;
	      else if (!strcmp (val, "queue") || !strcmp (val, "1"))
		mode.tx_mode = APPL_MODE_PKT_QUEUE;
	      else if (!strcmp (val, "tm") || !strcmp (val, "2"))
		mode.tx_mode = APPL_MODE_PKT_TM;
	      vec_free (val);
	    }
	  else if (unformat (input, "num-rx-queues %u", &num))
	    mode.num_rx_queues = num;
	  else if (unformat (input, "num-tx-queues %u", &num))
	    mode.num_tx_queues = num;
	  else if (unformat (input, "num-tx-queues %s", &val))
	    {
	      if (!strcmp (val, "max"))
		mode.num_tx_queues = 0;
	      vec_free (val);
	    }
	  else if (unformat (input, "hw-addr %U", unformat_ethernet_address,
			     hw_addr))
	    set_hw_addr = 1;
	  else if (unformat (input, "%s", &val))
	    {
	      clib_warning ("%s: Unknown option %s\n", __func__, val);
	      vec_free (val);
	    }
	}
    }

  if (!name)
    {
      def_if_mode = mode;
      return 0;
    }

  /* Save configuration */
  pool_get (if_config, config);
  config->name = (u8 *) vec_dup (name);
  config->mode = mode;
  clib_memcpy (config->hw_addr, hw_addr, 6);
  config->set_hw_addr = set_hw_addr;

  return 0;
}

static clib_error_t *
odp_config (vlib_main_t * vm, unformat_input_t * input)
{
  char *param = NULL;
  unformat_input_t sub_input;

  unformat_skip_white_space (input);

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "rx-sched-wait %u", &rx_sched_wait))
	;
      else if (unformat (input, "tx-burst-size %u", &tx_burst_size))
	;
      else if (unformat (input, "num-pkts-in-pool %u", &num_pkts_in_pool))
	;
      else if (unformat (input, "default %U", unformat_vlib_cli_sub_input,
			 &sub_input))
	{
	  odp_device_config (NULL, &sub_input);
	  unformat_free (&sub_input);
	}
      else if (unformat (input, "dev %s %U", &param,
			 unformat_vlib_cli_sub_input, &sub_input))
	{
	  odp_device_config (param, &sub_input);
	  vec_free (param);
	  unformat_free (&sub_input);
	}
      else if (unformat (input, "dev %s", &param))
	{
	  odp_device_config (param, NULL);
	  vec_free (param);
	}
      else if (unformat (input, "enable-odp-crypto"))
	{
	  enable_odp_crypto = 1;
	}
      else if (unformat (input, "enable-odp-ipsec"))
	{
	  ipsec_api = 1;
	}
      else if (unformat (input, "async"))
	{
	  is_async = 1;
	}
      else if (unformat (input, "inline"))
	{
	  is_inline = 1;
	}
      else if (unformat (input, "%s", &param))
	{
	  clib_warning ("%s: Unknown option %s\n", __func__, param);
	  vec_free (param);
	}
    }

  return 0;
}

VLIB_EARLY_CONFIG_FUNCTION (odp_config, "odp");

static uword
odp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
{
  u32 i, ret;
  odp_if_config_t *config;
  vlib_thread_main_t *tm = vlib_get_thread_main ();

  /* Create interfaces defined in startup configuration file */
  for (i = 0; i < pool_len (if_config); i++)
    {
      config = pool_elt_at_index (if_config, i);
      ret = odp_packet_create_if (vm, config->name,
				  (config->set_hw_addr ? config->hw_addr :
				   NULL), NULL, &config->mode);
      vec_free (config->name);

      if (ret == VNET_API_ERROR_SYSCALL_ERROR_1)
	clib_warning ("%s (errno %d)", strerror (errno), errno);

      if (ret == VNET_API_ERROR_INVALID_INTERFACE)
	clib_warning ("Invalid interface name");

      if (ret == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
	clib_warning ("Interface already exists");
    }
  pool_free (if_config);

  /* Initialization complete and worker threads can start */
  tm->worker_thread_release = 1;

  ipsec_init (vlib_get_main (), ipsec_api);

  return 0;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (odp_process_node,static) = {
    .function = odp_process,
    .type = VLIB_NODE_TYPE_PROCESS,
    .name = "odp-process",
};
/* *INDENT-ON* */

static clib_error_t *
odp_packet_init (vlib_main_t * vm)
{
  odp_packet_main_t *om;
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  vlib_thread_registration_t *tr;
  vlib_physmem_main_t *vpm = &vm->physmem_main;
  uword *p;
  odp_pool_param_t params;
  odp_pool_capability_t capa;
  odp_shm_t shm;
  odp_instance_t instance;

  if (odp_init_global (&instance, NULL, NULL))
    clib_warning ("Error: ODP global init failed");

  if (odp_init_local (instance, ODP_THREAD_CONTROL) != 0)
    {
      clib_warning ("Error: ODP local init failed");
      odp_term_global (instance);

    }

  shm = odp_shm_reserve ("odp_packet_main", sizeof (odp_packet_main_t),
			 ODP_CACHE_LINE_SIZE, 0);
  odp_packet_main = odp_shm_addr (shm);
  if (odp_packet_main == NULL)
    {
      return clib_error_return (0, "Failed to initialize odp_packet");
    }

  om = odp_packet_main;
  memset (om, 0, sizeof (odp_packet_main_t));
  om->input_cpu_first_index = 0;
  om->input_cpu_count = 1;
  om->if_count = 0;
  om->instance = instance;

  odp_pool_capability (&capa);
  if (capa.pkt.min_headroom != VLIB_BUFFER_PRE_DATA_SIZE)
    {
      return clib_error_return (0,
				"Packet Headroom for VPP and ODP must be equal");
    }

  /* Create packet pool */
  odp_pool_param_init (&params);
  params.pkt.seg_len = SHM_PKT_POOL_BUF_SIZE;
  params.pkt.len = SHM_PKT_POOL_BUF_SIZE;
  params.type = ODP_POOL_PACKET;
  params.pkt.num = num_pkts_in_pool;
  params.pkt.uarea_size = sizeof (vlib_buffer_t) - VLIB_BUFFER_PRE_DATA_SIZE;

  om->pool = odp_pool_create (SHM_PKT_POOL_NAME, &params);

  if (om->pool == ODP_POOL_INVALID)
    {
      return clib_error_return (0, "Packet pool create failed");
    }

  /* find out which cpus will be used for input */
  p = hash_get_mem (tm->thread_registrations_by_name, "workers");
  tr = p ? (vlib_thread_registration_t *) p[0] : 0;

  if (tr && tr->count > 0)
    {
      om->input_cpu_first_index = tr->first_index;
      om->input_cpu_count = tr->count;
    }

  mhash_init_vec_string (&om->if_index_by_host_if_name, sizeof (uword));

  /* Get address range of the pool */
  odp_pool_info_t info;
  odp_pool_info (om->pool, &info);
  vpm->virtual.start = info.min_data_addr;
  vpm->virtual.end = info.max_data_addr;
  vpm->virtual.size = info.max_data_addr - info.min_data_addr + 1;

  return 0;
}

VLIB_INIT_FUNCTION (odp_packet_init);

static clib_error_t *
odp_packet_exit (vlib_main_t * vm)
{
  odp_packet_main_t *om = odp_packet_main;
  odp_packet_if_t *port;

  vlib_worker_thread_barrier_sync (vm);

  /* *INDENT-OFF* */
  pool_foreach (port, om->interfaces,
    ({
      odp_packet_delete_if (vm, port->host_if_name);
    }));
  /* *INDENT-ON* */

  odp_pool_destroy (om->pool);
  odp_shm_free (odp_shm_lookup ("odp_packet_main"));
  odp_packet_main = 0x0;

  return 0;
}

VLIB_MAIN_LOOP_EXIT_FUNCTION (odp_packet_exit);

/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () = {
    .version = VPP_BUILD_VER,
    .description = "ODP",
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */