aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/odp/odp_packet.c
blob: a8c606d833657be97199d53da015261174f290a1 (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
/*
 *------------------------------------------------------------------
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#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/odp_packet.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>

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

/**
 * Drop packets which input parsing marked as containing errors.
 *
 * Frees packets with error and modifies pkt_tbl[] to only contain packets with
 * no detected errors.
 *
 * @param pkt_tbl  Array of packet
 * @param len      Length of pkt_tbl[]
 *
 * @return Number of packets with no detected error
 */
u32
drop_err_pkts(odp_packet_t pkt_tbl[], unsigned len)
{
  odp_packet_t pkt;
  unsigned pkt_cnt = len;
  unsigned i, j;

  for (i = 0, j = 0; i < len; ++i)
    {
      pkt = pkt_tbl[i];

      if (odp_unlikely(odp_packet_has_error(pkt)))
        {
          odp_packet_free(pkt); /* Drop */
          pkt_cnt--;
        }
      else if (odp_unlikely(i != j++))
        {
          pkt_tbl[j-1] = pkt;
        }
    }

  return pkt_cnt;
}

static odp_pktio_t
create_pktio(const char *dev, odp_pool_t pool, u32 mode)
{
  odp_pktio_t pktio;
  int ret;
  odp_pktio_param_t pktio_param;
  odp_pktin_queue_param_t pktin_param;

  odp_pktio_param_init(&pktio_param);

  switch(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:
      pktio_param.in_mode = ODP_PKTIN_MODE_SCHED;
      break;
      default:
      clib_warning("Invalid 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);
    }

  odp_pktin_queue_param_init(&pktin_param);

  if (mode == APPL_MODE_PKT_SCHED)
    pktin_param.queue_param.sched.sync = ODP_SCHED_SYNC_ATOMIC;

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

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

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

  return pktio;
}

int
odp_worker_thread_enable ()
{

  /*If worker threads are enabled, switch to polling mode */
  foreach_vlib_main ((
                      {
                       vlib_node_set_state (this_vlib_main,
                                            odp_packet_input_node.index,
                                            VLIB_NODE_STATE_POLLING);
                       }));
  return 0;
}

int
odp_worker_thread_disable ()
{
 foreach_vlib_main ((
                       {
                       vlib_node_set_state (this_vlib_main,
                                            odp_packet_input_node.index,
                                            VLIB_NODE_STATE_DISABLED);
                       }));

  return 0;
}

u32
odp_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
		     u32 * sw_if_index , u32  mode)
{
  odp_packet_main_t *om = &odp_packet_main;
  int ret = 0;
  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;
  u8 *host_if_name_dup=vec_dup(host_if_name);
  vlib_thread_main_t *tm= vlib_get_thread_main();

  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 = host_if_name_dup;
  oif->per_interface_next_index= ~0;

  /* Create a pktio instance */
  oif->pktio=create_pktio((char*)host_if_name, om->pool, mode);
  oif->mode=mode;
  om->if_count++;

  if (tm->n_vlib_mains > 1)
    {
      oif->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
                                           CLIB_CACHE_LINE_BYTES);
      memset ((void *) oif->lockp, 0, CLIB_CACHE_LINE_BYTES);
    }

  /*use configured or generate random MAC address */
  if (hw_addr_set)
    clib_memcpy (hw_addr, hw_addr_set, 6);
  else
    {
      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)
    {
      memset (oif, 0, sizeof (*oif));
      pool_put (om->interfaces, oif);
      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_flags (vnm, oif->hw_if_index,
			       VNET_HW_INTERFACE_FLAG_LINK_UP);

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

  if (tm->n_vlib_mains > 1 && pool_elts (om->interfaces) == 1)
    {
      /*Fixme :Workers support commented for now as vlib_buffer not thread safe*/
      //odp_worker_thread_enable ();
    }
    else
    {
       vlib_node_set_state (vm, odp_packet_input_node.index,
                            VLIB_NODE_STATE_POLLING);
    }
  return 0;

  error:
  vec_free (host_if_name_dup);

  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;
  vlib_thread_main_t *tm= vlib_get_thread_main();

  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);

  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);

  if (tm->n_vlib_mains > 1 && pool_elts (om->interfaces) == 0)
    {
      odp_pool_destroy(om->pool);
      /*Fixme :Workers support commented for now*/
      // odp_worker_thread_disable ();
    }

  return 0;

}

static clib_error_t *
odp_packet_init (vlib_main_t * vm)
{
  odp_packet_main_t *om = &odp_packet_main;
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  vlib_thread_registration_t *tr;
  uword *p;
  odp_platform_init_t platform_params;
  odp_pool_param_t params;

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

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

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

    }
  /* 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     = SHM_PKT_POOL_NB_PKTS;

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

  if (om->pool == ODP_POOL_INVALID)
    {
      clib_warning("Error: 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));

  vec_validate_aligned (om->rx_buffers, tm->n_vlib_mains - 1,
                        CLIB_CACHE_LINE_BYTES);
  return 0;
}

VLIB_INIT_FUNCTION (odp_packet_init);

/* *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:
 */