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
|
/*
*------------------------------------------------------------------
* 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.
*------------------------------------------------------------------
*/
#define _GNU_SOURCE
#include <stdint.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/ethernet/ethernet.h>
#include <memif/memif.h>
#include <memif/private.h>
#define foreach_memif_tx_func_error \
_(NO_FREE_SLOTS, "no free tx slots") \
_(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
_(PENDING_MSGS, "pending msgs in tx ring")
typedef enum
{
#define _(f,s) MEMIF_TX_ERROR_##f,
foreach_memif_tx_func_error
#undef _
MEMIF_TX_N_ERROR,
} memif_tx_func_error_t;
static __clib_unused char *memif_tx_func_error_strings[] = {
#define _(n,s) s,
foreach_memif_tx_func_error
#undef _
};
#ifndef CLIB_MULTIARCH_VARIANT
u8 *
format_memif_device_name (u8 * s, va_list * args)
{
u32 dev_instance = va_arg (*args, u32);
memif_main_t *mm = &memif_main;
memif_if_t *mif = pool_elt_at_index (mm->interfaces, dev_instance);
s = format (s, "memif%lu/%lu", mif->socket_file_index, mif->id);
return s;
}
#endif
static __clib_unused u8 *
format_memif_device (u8 * s, va_list * args)
{
u32 dev_instance = va_arg (*args, u32);
int verbose = va_arg (*args, int);
u32 indent = format_get_indent (s);
s = format (s, "MEMIF interface");
if (verbose)
{
s = format (s, "\n%U instance %u", format_white_space, indent + 2,
dev_instance);
}
return s;
}
static __clib_unused u8 *
format_memif_tx_trace (u8 * s, va_list * args)
{
s = format (s, "Unimplemented...");
return s;
}
static_always_inline void
memif_prefetch_buffer_and_data (vlib_main_t * vm, u32 bi)
{
vlib_buffer_t *b = vlib_get_buffer (vm, bi);
vlib_prefetch_buffer_header (b, LOAD);
CLIB_PREFETCH (b->data, CLIB_CACHE_LINE_BYTES, LOAD);
}
/**
* @brief Copy buffer to tx ring
*
* @param * vm (in)
* @param * node (in)
* @param * mif (in) pointer to memif interface
* @param bi (in) vlib buffer index
* @param * ring (in) pointer to memif ring
* @param * head (in/out) ring head
* @param mask (in) ring size - 1
*/
static_always_inline void
memif_copy_buffer_to_tx_ring (vlib_main_t * vm, vlib_node_runtime_t * node,
memif_if_t * mif, u32 bi, memif_ring_t * ring,
u16 * head, u16 mask)
{
vlib_buffer_t *b0;
void *mb0;
u32 total = 0, len;
u16 slot = (*head) & mask;
mb0 = memif_get_buffer (mif, ring, slot);
ring->desc[slot].flags = 0;
do
{
b0 = vlib_get_buffer (vm, bi);
len = b0->current_length;
if (PREDICT_FALSE (ring->desc[slot].buffer_length < (total + len)))
{
if (PREDICT_TRUE (total))
{
ring->desc[slot].length = total;
total = 0;
ring->desc[slot].flags |= MEMIF_DESC_FLAG_NEXT;
(*head)++;
slot = (*head) & mask;
mb0 = memif_get_buffer (mif, ring, slot);
ring->desc[slot].flags = 0;
}
}
if (PREDICT_TRUE (ring->desc[slot].buffer_length >= (total + len)))
{
clib_memcpy (mb0 + total, vlib_buffer_get_current (b0),
CLIB_CACHE_LINE_BYTES);
if (len > CLIB_CACHE_LINE_BYTES)
clib_memcpy (mb0 + CLIB_CACHE_LINE_BYTES + total,
vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
len - CLIB_CACHE_LINE_BYTES);
total += len;
}
else
{
vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_TRUNC_PACKET,
1);
break;
}
}
while ((bi = (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
if (PREDICT_TRUE (total))
{
ring->desc[slot].length = total;
(*head)++;
}
}
static_always_inline uword
memif_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
vlib_frame_t * frame, memif_if_t * mif,
memif_ring_type_t type)
{
u8 qid;
memif_ring_t *ring;
u32 *buffers = vlib_frame_args (frame);
u32 n_left = frame->n_vectors;
u16 ring_size, mask;
u16 head, tail;
u16 free_slots;
u32 thread_index = vlib_get_thread_index ();
u8 tx_queues = vec_len (mif->tx_queues);
memif_queue_t *mq;
int n_retries = 5;
if (tx_queues < vec_len (vlib_mains))
{
qid = thread_index % tx_queues;
clib_spinlock_lock_if_init (&mif->lockp);
}
else
qid = thread_index;
mq = vec_elt_at_index (mif->tx_queues, qid);
ring = mq->ring;
ring_size = 1 << mq->log2_ring_size;
mask = ring_size - 1;
retry:
/* free consumed buffers */
head = ring->head;
tail = ring->tail;
free_slots = ring_size - head + tail;
while (n_left > 5 && free_slots > 1)
{
CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 2) & mask),
CLIB_CACHE_LINE_BYTES, STORE);
CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 3) & mask),
CLIB_CACHE_LINE_BYTES, STORE);
CLIB_PREFETCH (&ring->desc[(head + 4) & mask], CLIB_CACHE_LINE_BYTES,
STORE);
CLIB_PREFETCH (&ring->desc[(head + 5) & mask], CLIB_CACHE_LINE_BYTES,
STORE);
memif_prefetch_buffer_and_data (vm, buffers[2]);
memif_prefetch_buffer_and_data (vm, buffers[3]);
memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[0], ring, &head,
mask);
memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[1], ring, &head,
mask);
buffers += 2;
n_left -= 2;
free_slots -= 2;
}
while (n_left && free_slots)
{
memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[0], ring, &head,
mask);
buffers++;
n_left--;
free_slots--;
}
CLIB_MEMORY_STORE_BARRIER ();
ring->head = head;
if (n_left && n_retries--)
goto retry;
clib_spinlock_unlock_if_init (&mif->lockp);
if (n_left)
{
vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_FREE_SLOTS,
n_left);
}
if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0 && mq->int_fd > -1)
{
u64 b = 1;
CLIB_UNUSED (int r) = write (mq->int_fd, &b, sizeof (b));
mq->int_count++;
}
vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
return frame->n_vectors;
}
uword
CLIB_MULTIARCH_FN (memif_interface_tx) (vlib_main_t * vm,
vlib_node_runtime_t * node,
vlib_frame_t * frame)
{
memif_main_t *nm = &memif_main;
vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
memif_if_t *mif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_S2M);
else
return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_M2S);
}
static __clib_unused void
memif_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
u32 node_index)
{
memif_main_t *apm = &memif_main;
vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
/* Shut off redirection */
if (node_index == ~0)
{
mif->per_interface_next_index = node_index;
return;
}
mif->per_interface_next_index =
vlib_node_add_next (vlib_get_main (), memif_input_node.index, node_index);
}
static __clib_unused void
memif_clear_hw_interface_counters (u32 instance)
{
/* Nothing for now */
}
static __clib_unused clib_error_t *
memif_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
vnet_hw_interface_rx_mode mode)
{
memif_main_t *mm = &memif_main;
vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, qid);
if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
mq->ring->flags |= MEMIF_RING_FLAG_MASK_INT;
else
mq->ring->flags &= ~MEMIF_RING_FLAG_MASK_INT;
return 0;
}
static __clib_unused clib_error_t *
memif_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
{
memif_main_t *mm = &memif_main;
vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
static clib_error_t *error = 0;
if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
mif->flags |= MEMIF_IF_FLAG_ADMIN_UP;
else
mif->flags &= ~MEMIF_IF_FLAG_ADMIN_UP;
return error;
}
static __clib_unused clib_error_t *
memif_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;
}
#ifndef CLIB_MULTIARCH_VARIANT
/* *INDENT-OFF* */
VNET_DEVICE_CLASS (memif_device_class) = {
.name = "memif",
.tx_function = memif_interface_tx,
.format_device_name = format_memif_device_name,
.format_device = format_memif_device,
.format_tx_trace = format_memif_tx_trace,
.tx_function_n_errors = MEMIF_TX_N_ERROR,
.tx_function_error_strings = memif_tx_func_error_strings,
.rx_redirect_to_node = memif_set_interface_next_node,
.clear_counters = memif_clear_hw_interface_counters,
.admin_up_down_function = memif_interface_admin_up_down,
.subif_add_del_function = memif_subif_add_del_function,
.rx_mode_change_function = memif_interface_rx_mode_change,
};
#if __x86_64__
vlib_node_function_t __clib_weak memif_interface_tx_avx512;
vlib_node_function_t __clib_weak memif_interface_tx_avx2;
static void __clib_constructor
dpdk_interface_tx_multiarch_select (void)
{
if (memif_interface_tx_avx512 && clib_cpu_supports_avx512f ())
memif_device_class.tx_function = memif_interface_tx_avx512;
else if (memif_interface_tx_avx2 && clib_cpu_supports_avx2 ())
memif_device_class.tx_function = memif_interface_tx_avx2;
}
#endif
#endif
/* *INDENT-ON* */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
|