2016-04-20 05:04:20 +02:00
|
|
|
/*
|
|
|
|
*------------------------------------------------------------------
|
|
|
|
* 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.
|
|
|
|
*------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-05-11 04:49:46 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2016-04-20 05:04:20 +02:00
|
|
|
#include <vlib/vlib.h>
|
|
|
|
#include <vlib/unix/unix.h>
|
|
|
|
#include <vnet/ethernet/ethernet.h>
|
2016-11-02 14:48:21 +01:00
|
|
|
#include <vnet/devices/devices.h>
|
2016-10-28 20:30:15 +02:00
|
|
|
#include <vnet/feature/feature.h>
|
2016-04-20 05:04:20 +02:00
|
|
|
|
2016-05-11 04:49:46 -07:00
|
|
|
#include <vnet/devices/netmap/net_netmap.h>
|
2016-04-20 05:04:20 +02:00
|
|
|
#include <vnet/devices/netmap/netmap.h>
|
|
|
|
|
|
|
|
#define foreach_netmap_input_error
|
|
|
|
|
2016-08-17 17:05:46 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
2016-04-20 05:04:20 +02:00
|
|
|
#define _(f,s) NETMAP_INPUT_ERROR_##f,
|
|
|
|
foreach_netmap_input_error
|
|
|
|
#undef _
|
2016-08-17 17:05:46 +02:00
|
|
|
NETMAP_INPUT_N_ERROR,
|
2016-04-20 05:04:20 +02:00
|
|
|
} netmap_input_error_t;
|
|
|
|
|
2016-08-17 17:05:46 +02:00
|
|
|
static char *netmap_input_error_strings[] = {
|
2016-04-20 05:04:20 +02:00
|
|
|
#define _(n,s) s,
|
2016-08-17 17:05:46 +02:00
|
|
|
foreach_netmap_input_error
|
2016-04-20 05:04:20 +02:00
|
|
|
#undef _
|
|
|
|
};
|
|
|
|
|
2016-08-17 17:05:46 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2016-04-20 05:04:20 +02:00
|
|
|
u32 next_index;
|
|
|
|
u32 hw_if_index;
|
|
|
|
struct netmap_slot slot;
|
|
|
|
} netmap_input_trace_t;
|
|
|
|
|
2016-08-17 17:05:46 +02:00
|
|
|
static u8 *
|
|
|
|
format_netmap_input_trace (u8 * s, va_list * args)
|
2016-04-20 05:04:20 +02:00
|
|
|
{
|
|
|
|
CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
|
|
|
|
CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
|
2016-08-17 17:05:46 +02:00
|
|
|
netmap_input_trace_t *t = va_arg (*args, netmap_input_trace_t *);
|
2017-10-02 18:10:54 +02:00
|
|
|
u32 indent = format_get_indent (s);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
s = format (s, "netmap: hw_if_index %d next-index %d",
|
|
|
|
t->hw_if_index, t->next_index);
|
|
|
|
s = format (s, "\n%Uslot: flags 0x%x len %u buf_idx %u",
|
|
|
|
format_white_space, indent + 2,
|
|
|
|
t->slot.flags, t->slot.len, t->slot.buf_idx);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
always_inline void
|
2016-08-17 17:05:46 +02:00
|
|
|
buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
|
2016-04-20 05:04:20 +02:00
|
|
|
{
|
2016-08-17 17:05:46 +02:00
|
|
|
vlib_buffer_t *b = vlib_get_buffer (vm, bi);
|
|
|
|
vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
|
|
|
|
vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
/* update first buffer */
|
2016-08-17 17:05:46 +02:00
|
|
|
first_b->total_length_not_including_first_buffer += b->current_length;
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
/* update previous buffer */
|
|
|
|
prev_b->next_buffer = bi;
|
|
|
|
prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
|
|
|
|
|
|
|
|
/* update current buffer */
|
|
|
|
b->next_buffer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
always_inline uword
|
2016-08-17 17:05:46 +02:00
|
|
|
netmap_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
|
|
|
|
vlib_frame_t * frame, netmap_if_t * nif)
|
2016-04-20 05:04:20 +02:00
|
|
|
{
|
2016-11-02 14:48:21 +01:00
|
|
|
u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
|
2016-04-20 05:04:20 +02:00
|
|
|
uword n_trace = vlib_get_trace_count (vm, node);
|
2016-08-17 17:05:46 +02:00
|
|
|
netmap_main_t *nm = &netmap_main;
|
2016-04-20 05:04:20 +02:00
|
|
|
u32 n_rx_packets = 0;
|
|
|
|
u32 n_rx_bytes = 0;
|
2016-08-17 17:05:46 +02:00
|
|
|
u32 *to_next = 0;
|
2016-04-20 05:04:20 +02:00
|
|
|
u32 n_free_bufs;
|
2016-08-17 17:05:46 +02:00
|
|
|
struct netmap_ring *ring;
|
2016-04-20 05:04:20 +02:00
|
|
|
int cur_ring;
|
2018-07-11 12:47:43 +02:00
|
|
|
u32 thread_index = vm->thread_index;
|
2019-02-09 23:29:26 +01:00
|
|
|
u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
if (nif->per_interface_next_index != ~0)
|
2016-08-17 17:05:46 +02:00
|
|
|
next_index = nif->per_interface_next_index;
|
2016-04-20 05:04:20 +02:00
|
|
|
|
2017-04-05 19:18:20 +02:00
|
|
|
n_free_bufs = vec_len (nm->rx_buffers[thread_index]);
|
2016-08-17 17:05:46 +02:00
|
|
|
if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
|
2016-04-20 05:04:20 +02:00
|
|
|
{
|
2017-04-05 19:18:20 +02:00
|
|
|
vec_validate (nm->rx_buffers[thread_index],
|
2016-08-17 17:05:46 +02:00
|
|
|
VLIB_FRAME_SIZE + n_free_bufs - 1);
|
|
|
|
n_free_bufs +=
|
2017-04-05 19:18:20 +02:00
|
|
|
vlib_buffer_alloc (vm, &nm->rx_buffers[thread_index][n_free_bufs],
|
2016-08-17 17:05:46 +02:00
|
|
|
VLIB_FRAME_SIZE);
|
2017-04-05 19:18:20 +02:00
|
|
|
_vec_len (nm->rx_buffers[thread_index]) = n_free_bufs;
|
2016-04-20 05:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cur_ring = nif->first_rx_ring;
|
|
|
|
while (cur_ring <= nif->last_rx_ring && n_free_bufs)
|
|
|
|
{
|
|
|
|
int r = 0;
|
|
|
|
u32 cur_slot_index;
|
2016-08-17 17:05:46 +02:00
|
|
|
ring = NETMAP_RXRING (nif->nifp, cur_ring);
|
|
|
|
r = nm_ring_space (ring);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
cur_ring++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r > n_free_bufs)
|
|
|
|
r = n_free_bufs;
|
|
|
|
|
|
|
|
cur_slot_index = ring->cur;
|
|
|
|
while (r)
|
|
|
|
{
|
2016-08-17 17:05:46 +02:00
|
|
|
u32 n_left_to_next;
|
|
|
|
u32 next0 = next_index;
|
|
|
|
vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
while (r && n_left_to_next)
|
|
|
|
{
|
2016-11-07 14:05:34 +01:00
|
|
|
vlib_buffer_t *first_b0 = 0;
|
2016-04-20 05:04:20 +02:00
|
|
|
u32 offset = 0;
|
|
|
|
u32 bi0 = 0, first_bi0 = 0, prev_bi0;
|
|
|
|
u32 next_slot_index = (cur_slot_index + 1) % ring->num_slots;
|
|
|
|
u32 next2_slot_index = (cur_slot_index + 2) % ring->num_slots;
|
2016-08-17 17:05:46 +02:00
|
|
|
struct netmap_slot *slot = &ring->slot[cur_slot_index];
|
2016-04-20 05:04:20 +02:00
|
|
|
u32 data_len = slot->len;
|
|
|
|
|
|
|
|
/* prefetch 2 slots in advance */
|
2016-08-17 17:05:46 +02:00
|
|
|
CLIB_PREFETCH (&ring->slot[next2_slot_index],
|
|
|
|
CLIB_CACHE_LINE_BYTES, LOAD);
|
2016-04-20 05:04:20 +02:00
|
|
|
/* prefetch start of next packet */
|
2016-08-17 17:05:46 +02:00
|
|
|
CLIB_PREFETCH (NETMAP_BUF
|
|
|
|
(ring, ring->slot[next_slot_index].buf_idx),
|
2016-04-20 05:04:20 +02:00
|
|
|
CLIB_CACHE_LINE_BYTES, LOAD);
|
|
|
|
|
|
|
|
while (data_len && n_free_bufs)
|
|
|
|
{
|
2016-11-07 14:05:34 +01:00
|
|
|
vlib_buffer_t *b0;
|
2016-04-20 05:04:20 +02:00
|
|
|
/* grab free buffer */
|
2016-08-17 17:05:46 +02:00
|
|
|
u32 last_empty_buffer =
|
2017-04-05 19:18:20 +02:00
|
|
|
vec_len (nm->rx_buffers[thread_index]) - 1;
|
2016-04-20 05:04:20 +02:00
|
|
|
prev_bi0 = bi0;
|
2017-04-05 19:18:20 +02:00
|
|
|
bi0 = nm->rx_buffers[thread_index][last_empty_buffer];
|
2016-04-20 05:04:20 +02:00
|
|
|
b0 = vlib_get_buffer (vm, bi0);
|
2017-04-05 19:18:20 +02:00
|
|
|
_vec_len (nm->rx_buffers[thread_index]) = last_empty_buffer;
|
2016-04-20 05:04:20 +02:00
|
|
|
n_free_bufs--;
|
|
|
|
|
|
|
|
/* copy data */
|
2016-08-17 17:05:46 +02:00
|
|
|
u32 bytes_to_copy =
|
|
|
|
data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
|
2016-04-20 05:04:20 +02:00
|
|
|
b0->current_data = 0;
|
2018-11-13 16:34:13 -05:00
|
|
|
clib_memcpy_fast (vlib_buffer_get_current (b0),
|
|
|
|
(u8 *) NETMAP_BUF (ring, slot->buf_idx) +
|
|
|
|
offset, bytes_to_copy);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
/* fill buffer header */
|
|
|
|
b0->current_length = bytes_to_copy;
|
|
|
|
|
|
|
|
if (offset == 0)
|
|
|
|
{
|
|
|
|
b0->total_length_not_including_first_buffer = 0;
|
|
|
|
b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
|
2016-08-17 17:05:46 +02:00
|
|
|
vnet_buffer (b0)->sw_if_index[VLIB_RX] =
|
|
|
|
nif->sw_if_index;
|
|
|
|
vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
|
2016-04-20 05:04:20 +02:00
|
|
|
first_bi0 = bi0;
|
2016-08-17 17:05:46 +02:00
|
|
|
first_b0 = vlib_get_buffer (vm, first_bi0);
|
2016-04-20 05:04:20 +02:00
|
|
|
}
|
|
|
|
else
|
2016-08-17 17:05:46 +02:00
|
|
|
buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
offset += bytes_to_copy;
|
|
|
|
data_len -= bytes_to_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* trace */
|
2016-08-17 17:05:46 +02:00
|
|
|
if (PREDICT_FALSE (n_trace > 0))
|
|
|
|
{
|
|
|
|
if (PREDICT_TRUE (first_b0 != 0))
|
|
|
|
{
|
|
|
|
netmap_input_trace_t *tr;
|
|
|
|
vlib_trace_buffer (vm, node, next0, first_b0,
|
|
|
|
/* follow_chain */ 0);
|
|
|
|
vlib_set_trace_count (vm, node, --n_trace);
|
|
|
|
tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
|
|
|
|
tr->next_index = next0;
|
|
|
|
tr->hw_if_index = nif->hw_if_index;
|
|
|
|
memcpy (&tr->slot, slot, sizeof (struct netmap_slot));
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 20:30:15 +02:00
|
|
|
|
|
|
|
/* redirect if feature path enabled */
|
2016-11-04 11:00:27 +01:00
|
|
|
vnet_feature_start_device_input_x1 (nif->sw_if_index, &next0,
|
2017-03-06 12:02:50 +01:00
|
|
|
first_b0);
|
2016-10-28 20:30:15 +02:00
|
|
|
|
2016-04-20 05:04:20 +02:00
|
|
|
/* enque and take next packet */
|
|
|
|
vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
|
2016-08-17 17:05:46 +02:00
|
|
|
n_left_to_next, first_bi0,
|
|
|
|
next0);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
/* next packet */
|
|
|
|
n_rx_packets++;
|
|
|
|
n_rx_bytes += slot->len;
|
|
|
|
to_next[0] = first_bi0;
|
|
|
|
to_next += 1;
|
|
|
|
n_left_to_next--;
|
|
|
|
cur_slot_index = next_slot_index;
|
|
|
|
|
|
|
|
r--;
|
|
|
|
}
|
2016-08-17 17:05:46 +02:00
|
|
|
vlib_put_next_frame (vm, node, next_index, n_left_to_next);
|
2016-04-20 05:04:20 +02:00
|
|
|
}
|
2016-08-17 17:05:46 +02:00
|
|
|
ring->head = ring->cur = cur_slot_index;
|
|
|
|
cur_ring++;
|
2016-04-20 05:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n_rx_packets)
|
2016-08-17 17:05:46 +02:00
|
|
|
ioctl (nif->fd, NIOCRXSYNC, NULL);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
vlib_increment_combined_counter
|
2016-08-17 17:05:46 +02:00
|
|
|
(vnet_get_main ()->interface_main.combined_sw_if_counters
|
2016-04-20 05:04:20 +02:00
|
|
|
+ VNET_INTERFACE_COUNTER_RX,
|
2017-04-05 19:18:20 +02:00
|
|
|
vlib_get_thread_index (), nif->hw_if_index, n_rx_packets, n_rx_bytes);
|
2016-04-20 05:04:20 +02:00
|
|
|
|
2017-04-05 19:18:20 +02:00
|
|
|
vnet_device_increment_rx_packets (thread_index, n_rx_packets);
|
2017-02-28 21:55:28 +01:00
|
|
|
|
2016-04-20 05:04:20 +02:00
|
|
|
return n_rx_packets;
|
|
|
|
}
|
|
|
|
|
2019-03-04 03:03:13 -08:00
|
|
|
VLIB_NODE_FN (netmap_input_node) (vlib_main_t * vm,
|
|
|
|
vlib_node_runtime_t * node,
|
|
|
|
vlib_frame_t * frame)
|
2016-04-20 05:04:20 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 n_rx_packets = 0;
|
2018-07-11 12:47:43 +02:00
|
|
|
u32 thread_index = vm->thread_index;
|
2016-08-17 17:05:46 +02:00
|
|
|
netmap_main_t *nm = &netmap_main;
|
|
|
|
netmap_if_t *nmi;
|
2016-04-20 05:04:20 +02:00
|
|
|
|
2016-08-17 17:05:46 +02:00
|
|
|
for (i = 0; i < vec_len (nm->interfaces); i++)
|
2016-06-16 06:58:34 +02:00
|
|
|
{
|
2016-08-17 17:05:46 +02:00
|
|
|
nmi = vec_elt_at_index (nm->interfaces, i);
|
2016-06-16 06:58:34 +02:00
|
|
|
if (nmi->is_admin_up &&
|
2016-08-17 17:05:46 +02:00
|
|
|
(i % nm->input_cpu_count) ==
|
2017-04-05 19:18:20 +02:00
|
|
|
(thread_index - nm->input_cpu_first_index))
|
2016-08-17 17:05:46 +02:00
|
|
|
n_rx_packets += netmap_device_input_fn (vm, node, frame, nmi);
|
2016-06-16 06:58:34 +02:00
|
|
|
}
|
2016-04-20 05:04:20 +02:00
|
|
|
|
|
|
|
return n_rx_packets;
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:05:46 +02:00
|
|
|
/* *INDENT-OFF* */
|
2016-04-20 05:04:20 +02:00
|
|
|
VLIB_REGISTER_NODE (netmap_input_node) = {
|
|
|
|
.name = "netmap-input",
|
2016-11-09 11:59:42 +01:00
|
|
|
.sibling_of = "device-input",
|
2019-09-24 18:10:49 +02:00
|
|
|
.flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
|
2016-04-20 05:04:20 +02:00
|
|
|
.format_trace = format_netmap_input_trace,
|
|
|
|
.type = VLIB_NODE_TYPE_INPUT,
|
2016-06-16 06:58:34 +02:00
|
|
|
/* default state is INTERRUPT mode, switch to POLLING if worker threads are enabled */
|
2016-04-20 05:04:20 +02:00
|
|
|
.state = VLIB_NODE_STATE_INTERRUPT,
|
|
|
|
.n_errors = NETMAP_INPUT_N_ERROR,
|
|
|
|
.error_strings = netmap_input_error_strings,
|
|
|
|
};
|
2016-08-17 17:05:46 +02:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
2016-05-11 23:07:18 +02:00
|
|
|
|
2016-08-17 17:05:46 +02:00
|
|
|
/*
|
|
|
|
* fd.io coding-style-patch-verification: ON
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* eval: (c-set-style "gnu")
|
|
|
|
* End:
|
|
|
|
*/
|