aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/odp/device.c
blob: 4f809836d6849f1de0aa470c24dfd73a51a47e40 (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
/* Copyright (c) 2017, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#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>

#define foreach_odp_packet_tx_func_error               \
_(FRAME_NOT_READY, "tx frame not ready")              \
_(TXRING_EAGAIN,   "tx sendto temporary failure")     \
_(TXRING_FATAL,    "tx sendto fatal failure")         \
_(TXRING_OVERRUN,  "tx ring overrun")

typedef enum
{
#define _(f,s) ODP_PACKET_TX_ERROR_##f,
  foreach_odp_packet_tx_func_error
#undef _
    ODP_PACKET_TX_N_ERROR,
} odp_packet_tx_func_error_t;

static char *odp_packet_tx_func_error_strings[] = {
#define _(n,s) s,
  foreach_odp_packet_tx_func_error
#undef _
};

static_always_inline void
odp_prefetch_buffer_by_index (vlib_main_t * vm, u32 bi)
{
  vlib_buffer_t *b;
  odp_packet_t pkt;
  b = vlib_get_buffer (vm, bi);
  pkt = odp_packet_from_vlib_buffer (b);
  CLIB_PREFETCH (pkt, CLIB_CACHE_LINE_BYTES, LOAD);
  CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
}

static u8 *
format_odp_packet_device_name (u8 * s, va_list * args)
{
  u32 i = va_arg (*args, u32);
  odp_packet_main_t *om = odp_packet_main;
  odp_packet_if_t *oif = pool_elt_at_index (om->interfaces, i);

  s = format (s, "odp-%s", oif->host_if_name);
  return s;
}

static u8 *
format_odp_packet_device (u8 * s, va_list * args)
{
  s = format (s, "odp interface");
  return s;
}

static u8 *
format_odp_packet_tx_trace (u8 * s, va_list * args)
{
  s = format (s, "Unimplemented...");
  return s;
}

#define NEXT_BUFFER(b0, bi, j)	\
	  if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)	\
	    bi[j++] = b0->next_buffer;			\
	  else if (n_left)				\
	    {						\
	    bi[j++] = *buffers++;			\
	    n_left--;					\
	    }

static uword
odp_packet_interface_tx (vlib_main_t * vm,
			 vlib_node_runtime_t * node, vlib_frame_t * frame)
{

  odp_packet_main_t *om = odp_packet_main;
  u32 *buffers = vlib_frame_args (frame);
  u32 n_left = frame->n_vectors;
  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
  odp_packet_if_t *oif = pool_elt_at_index (om->interfaces, rd->dev_instance);
  uword queue_index = vlib_get_thread_index () % oif->m.num_tx_queues;
  u32 mode = oif->m.tx_mode;
  u32 burst_size = (tx_burst_size ? tx_burst_size : VLIB_FRAME_SIZE);
  union
  {
    odp_packet_t pkt[burst_size];
    odp_event_t evt[burst_size];
  } tbl;
  vlib_buffer_t *b0, *b1, *b2, *b3;
  u32 bi[4];
  u32 sent, count = 0, todo = 0;

  while (n_left > 0 || todo > 0)
    {
      int ret;

      for (; todo < 4 && todo < n_left; todo++, n_left--)
	bi[todo] = *buffers++;

      while ((todo == 4) && (count + 3 < burst_size))
	{
	  odp_packet_t pkt0, pkt1, pkt2, pkt3;

	  b0 = vlib_get_buffer (vm, bi[0]);
	  b1 = vlib_get_buffer (vm, bi[1]);
	  b2 = vlib_get_buffer (vm, bi[2]);
	  b3 = vlib_get_buffer (vm, bi[3]);

	  pkt0 = odp_packet_from_vlib_buffer (b0);
	  pkt1 = odp_packet_from_vlib_buffer (b1);
	  pkt2 = odp_packet_from_vlib_buffer (b2);
	  pkt3 = odp_packet_from_vlib_buffer (b3);

	  odp_adjust_data_pointers (b0, pkt0);
	  odp_adjust_data_pointers (b1, pkt1);
	  odp_adjust_data_pointers (b2, pkt2);
	  odp_adjust_data_pointers (b3, pkt3);

	  if (mode == APPL_MODE_PKT_QUEUE)
	    {
	      tbl.evt[count++] = odp_packet_to_event (pkt0);
	      tbl.evt[count++] = odp_packet_to_event (pkt1);
	      tbl.evt[count++] = odp_packet_to_event (pkt2);
	      tbl.evt[count++] = odp_packet_to_event (pkt3);
	    }
	  else
	    {
	      tbl.pkt[count++] = pkt0;
	      tbl.pkt[count++] = pkt1;
	      tbl.pkt[count++] = pkt2;
	      tbl.pkt[count++] = pkt3;
	    }

	  todo = 0;
	  NEXT_BUFFER (b0, bi, todo);
	  NEXT_BUFFER (b1, bi, todo);
	  NEXT_BUFFER (b2, bi, todo);
	  NEXT_BUFFER (b3, bi, todo);
	}

      while (todo && (count < burst_size))
	{
	  odp_packet_t pkt;

	  b0 = vlib_get_buffer (vm, bi[todo - 1]);

	  pkt = odp_packet_from_vlib_buffer (b0);

	  odp_adjust_data_pointers (b0, pkt);

	  if (mode == APPL_MODE_PKT_QUEUE)
	    tbl.evt[count++] = odp_packet_to_event (pkt);
	  else
	    tbl.pkt[count++] = pkt;

	  if (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
	    bi[todo - 1] = b0->next_buffer;
	  else if (n_left)
	    {
	      bi[todo - 1] = *buffers++;
	      n_left--;
	    }
	  else
	    todo--;
	}

      sent = 0;
      while (count > 0)
	{
	  switch (mode)
	    {
	    case APPL_MODE_PKT_BURST:
	      ret = odp_pktout_send (oif->outq[queue_index], &tbl.pkt[sent],
				     count);
	      break;
	    case APPL_MODE_PKT_QUEUE:
	      ret = odp_queue_enq_multi (oif->txq[queue_index],
					 &tbl.evt[sent], count);
	      break;
	    case APPL_MODE_PKT_TM:
	    default:
	      ret = 0;
	      clib_error ("Invalid mode\n");
	    }

	  if (odp_unlikely (ret < 0))
	    {
	      /* Drop one packet and try again */
	      odp_packet_free (tbl.pkt[sent]);
	      count--;
	      sent++;
	    }
	  else
	    {
	      count -= ret;
	      sent += ret;
	    }
	}
    }

  return (frame->n_vectors - n_left);
}

static void
odp_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
				    u32 node_index)
{
  odp_packet_main_t *om = odp_packet_main;
  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
  odp_packet_if_t *oif = pool_elt_at_index (om->interfaces, hw->dev_instance);

  if (node_index == ~0)
    {
      oif->per_interface_next_index = node_index;
      return;
    }

  oif->per_interface_next_index = vlib_node_add_next (vlib_get_main (),
						      odp_packet_input_node.
						      index, node_index);
}

static void
odp_packet_clear_hw_interface_counters (u32 instance)
{
  /* Nothing for now */
}

static clib_error_t *
odp_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
				    u32 flags)
{
  odp_packet_main_t *om = odp_packet_main;
  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
  odp_packet_if_t *oif = pool_elt_at_index (om->interfaces, hw->dev_instance);
  u32 hw_flags;

  oif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;

  if (oif->is_admin_up)
    hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
  else
    hw_flags = 0;

  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);

  return 0;
}

static clib_error_t *
odp_packet_subif_add_del_function (vnet_main_t * vnm,
				   u32 hw_if_index,
				   struct vnet_sw_interface_t *st, int is_add)
{
/* Nothing for now */
  return 0;
}

/* *INDENT-OFF* */
VNET_DEVICE_CLASS (odp_packet_device_class) = {
  .name = "odp-packet",
  .tx_function = odp_packet_interface_tx,
  .format_device_name = format_odp_packet_device_name,
  .format_device = format_odp_packet_device,
  .format_tx_trace = format_odp_packet_tx_trace,
  .tx_function_n_errors = ODP_PACKET_TX_N_ERROR,
  .tx_function_error_strings = odp_packet_tx_func_error_strings,
  .rx_redirect_to_node = odp_packet_set_interface_next_node,
  .clear_counters = odp_packet_clear_hw_interface_counters,
  .admin_up_down_function = odp_packet_interface_admin_up_down,
  .subif_add_del_function = odp_packet_subif_add_del_function,
};

VLIB_DEVICE_TX_FUNCTION_MULTIARCH (odp_packet_device_class,
				   odp_packet_interface_tx)
/* *INDENT-ON* */

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