FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 
18 #include <stdint.h>
19 #include <net/if.h>
20 #include <sys/ioctl.h>
21 
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/devices/devices.h>
26 #include <vnet/feature/feature.h>
27 
30 
31 #define foreach_netmap_input_error
32 
33 typedef enum
34 {
35 #define _(f,s) NETMAP_INPUT_ERROR_##f,
37 #undef _
40 
41 static char *netmap_input_error_strings[] = {
42 #define _(n,s) s,
44 #undef _
45 };
46 
47 typedef struct
48 {
51  struct netmap_slot slot;
53 
54 static u8 *
55 format_netmap_input_trace (u8 * s, va_list * args)
56 {
57  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59  netmap_input_trace_t *t = va_arg (*args, netmap_input_trace_t *);
60  uword indent = format_get_indent (s);
61 
62  s = format (s, "netmap: hw_if_index %d next-index %d",
63  t->hw_if_index, t->next_index);
64  s = format (s, "\n%Uslot: flags 0x%x len %u buf_idx %u",
65  format_white_space, indent + 2,
66  t->slot.flags, t->slot.len, t->slot.buf_idx);
67  return s;
68 }
69 
70 always_inline void
71 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
72 {
73  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
74  vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
75  vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
76 
77  /* update first buffer */
79 
80  /* update previous buffer */
81  prev_b->next_buffer = bi;
83 
84  /* update current buffer */
85  b->next_buffer = 0;
86 
87 #if DPDK > 0
88  struct rte_mbuf *mbuf = rte_mbuf_from_vlib_buffer (b);
89  struct rte_mbuf *first_mbuf = rte_mbuf_from_vlib_buffer (first_b);
90  struct rte_mbuf *prev_mbuf = rte_mbuf_from_vlib_buffer (prev_b);
91  first_mbuf->nb_segs++;
92  prev_mbuf->next = mbuf;
93  mbuf->data_len = b->current_length;
94  mbuf->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
95  mbuf->next = 0;
96 #endif
97 }
98 
101  vlib_frame_t * frame, netmap_if_t * nif)
102 {
104  uword n_trace = vlib_get_trace_count (vm, node);
105  netmap_main_t *nm = &netmap_main;
106  u32 n_rx_packets = 0;
107  u32 n_rx_bytes = 0;
108  u32 *to_next = 0;
109  u32 n_free_bufs;
110  struct netmap_ring *ring;
111  int cur_ring;
112  u32 cpu_index = os_get_cpu_number ();
113  u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
115 
116  if (nif->per_interface_next_index != ~0)
117  next_index = nif->per_interface_next_index;
118 
119  n_free_bufs = vec_len (nm->rx_buffers[cpu_index]);
120  if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
121  {
122  vec_validate (nm->rx_buffers[cpu_index],
123  VLIB_FRAME_SIZE + n_free_bufs - 1);
124  n_free_bufs +=
125  vlib_buffer_alloc (vm, &nm->rx_buffers[cpu_index][n_free_bufs],
127  _vec_len (nm->rx_buffers[cpu_index]) = n_free_bufs;
128  }
129 
130  cur_ring = nif->first_rx_ring;
131  while (cur_ring <= nif->last_rx_ring && n_free_bufs)
132  {
133  int r = 0;
134  u32 cur_slot_index;
135  ring = NETMAP_RXRING (nif->nifp, cur_ring);
136  r = nm_ring_space (ring);
137 
138  if (!r)
139  {
140  cur_ring++;
141  continue;
142  }
143 
144  if (r > n_free_bufs)
145  r = n_free_bufs;
146 
147  cur_slot_index = ring->cur;
148  while (r)
149  {
150  u32 n_left_to_next;
151  u32 next0 = next_index;
152  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
153 
154  while (r && n_left_to_next)
155  {
156  vlib_buffer_t *b0 = 0, *first_b0 = 0;
157  u32 offset = 0;
158  u32 bi0 = 0, first_bi0 = 0, prev_bi0;
159  u32 next_slot_index = (cur_slot_index + 1) % ring->num_slots;
160  u32 next2_slot_index = (cur_slot_index + 2) % ring->num_slots;
161  struct netmap_slot *slot = &ring->slot[cur_slot_index];
162  u32 data_len = slot->len;
163 
164  /* prefetch 2 slots in advance */
165  CLIB_PREFETCH (&ring->slot[next2_slot_index],
166  CLIB_CACHE_LINE_BYTES, LOAD);
167  /* prefetch start of next packet */
168  CLIB_PREFETCH (NETMAP_BUF
169  (ring, ring->slot[next_slot_index].buf_idx),
170  CLIB_CACHE_LINE_BYTES, LOAD);
171 
172  while (data_len && n_free_bufs)
173  {
174  /* grab free buffer */
175  u32 last_empty_buffer =
176  vec_len (nm->rx_buffers[cpu_index]) - 1;
177  prev_bi0 = bi0;
178  bi0 = nm->rx_buffers[cpu_index][last_empty_buffer];
179  b0 = vlib_get_buffer (vm, bi0);
180  _vec_len (nm->rx_buffers[cpu_index]) = last_empty_buffer;
181  n_free_bufs--;
182 
183  /* copy data */
184  u32 bytes_to_copy =
185  data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
186  b0->current_data = 0;
188  (u8 *) NETMAP_BUF (ring,
189  slot->buf_idx) + offset,
190  bytes_to_copy);
191 
192  /* fill buffer header */
193  b0->current_length = bytes_to_copy;
194 
195  if (offset == 0)
196  {
197 #if DPDK > 0
198  struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer (b0);
199  rte_pktmbuf_data_len (mb) = b0->current_length;
200  rte_pktmbuf_pkt_len (mb) = b0->current_length;
201 #endif
204  vnet_buffer (b0)->sw_if_index[VLIB_RX] =
205  nif->sw_if_index;
206  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
207  first_bi0 = bi0;
208  first_b0 = vlib_get_buffer (vm, first_bi0);
209  }
210  else
211  buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
212 
213  offset += bytes_to_copy;
214  data_len -= bytes_to_copy;
215  }
216 
217  /* trace */
219  if (PREDICT_FALSE (n_trace > 0))
220  {
221  if (PREDICT_TRUE (first_b0 != 0))
222  {
224  vlib_trace_buffer (vm, node, next0, first_b0,
225  /* follow_chain */ 0);
226  vlib_set_trace_count (vm, node, --n_trace);
227  tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
228  tr->next_index = next0;
229  tr->hw_if_index = nif->hw_if_index;
230  memcpy (&tr->slot, slot, sizeof (struct netmap_slot));
231  }
232  }
233 
234  /* redirect if feature path enabled */
236  &next0, b0, 0);
237 
238  /* enque and take next packet */
239  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
240  n_left_to_next, first_bi0,
241  next0);
242 
243  /* next packet */
244  n_rx_packets++;
245  n_rx_bytes += slot->len;
246  to_next[0] = first_bi0;
247  to_next += 1;
248  n_left_to_next--;
249  cur_slot_index = next_slot_index;
250 
251  r--;
252  }
253  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
254  }
255  ring->head = ring->cur = cur_slot_index;
256  cur_ring++;
257  }
258 
259  if (n_rx_packets)
260  ioctl (nif->fd, NIOCRXSYNC, NULL);
261 
263  (vnet_get_main ()->interface_main.combined_sw_if_counters
265  os_get_cpu_number (), nif->hw_if_index, n_rx_packets, n_rx_bytes);
266 
267  return n_rx_packets;
268 }
269 
270 static uword
272  vlib_frame_t * frame)
273 {
274  int i;
275  u32 n_rx_packets = 0;
276  u32 cpu_index = os_get_cpu_number ();
277  netmap_main_t *nm = &netmap_main;
278  netmap_if_t *nmi;
279 
280  for (i = 0; i < vec_len (nm->interfaces); i++)
281  {
282  nmi = vec_elt_at_index (nm->interfaces, i);
283  if (nmi->is_admin_up &&
284  (i % nm->input_cpu_count) ==
285  (cpu_index - nm->input_cpu_first_index))
286  n_rx_packets += netmap_device_input_fn (vm, node, frame, nmi);
287  }
288 
289  return n_rx_packets;
290 }
291 
292 /* *INDENT-OFF* */
294  .function = netmap_input_fn,
295  .name = "netmap-input",
296  .format_trace = format_netmap_input_trace,
297  .type = VLIB_NODE_TYPE_INPUT,
298  /* default state is INTERRUPT mode, switch to POLLING if worker threads are enabled */
299  .state = VLIB_NODE_STATE_INTERRUPT,
300  .n_errors = NETMAP_INPUT_N_ERROR,
301  .error_strings = netmap_input_error_strings,
302 
303  .n_next_nodes = VNET_DEVICE_INPUT_N_NEXT_NODES,
304  .next_nodes = VNET_DEVICE_INPUT_NEXT_NODES,
305 };
306 
308 /* *INDENT-ON* */
309 
310 
311 /*
312  * fd.io coding-style-patch-verification: ON
313  *
314  * Local Variables:
315  * eval: (c-set-style "gnu")
316  * End:
317  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:457
VLIB_NODE_FUNCTION_MULTIARCH(ethernet_input_not_l2_node, ethernet_input_not_l2)
Definition: node.c:1054
struct netmap_slot slot[0]
Definition: net_netmap.h:287
const uint32_t num_slots
Definition: net_netmap.h:266
static uword netmap_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:271
struct netmap_slot slot
Definition: node.c:51
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define CLIB_UNUSED(x)
Definition: clib.h:79
#define rte_mbuf_from_vlib_buffer(x)
Definition: buffer.h:385
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:143
netmap_if_t * interfaces
Definition: netmap.h:78
static uword netmap_device_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, netmap_if_t *nif)
Definition: node.c:100
vlib_node_registration_t netmap_input_node
(constructor) VLIB_REGISTER_NODE (netmap_input_node)
Definition: node.c:293
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:402
#define PREDICT_TRUE(x)
Definition: clib.h:98
uint16_t flags
Definition: net_netmap.h:148
#define NULL
Definition: clib.h:55
uint32_t cur
Definition: net_netmap.h:272
uint16_t len
Definition: net_netmap.h:147
u32 sw_if_index
Definition: netmap.h:50
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:104
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:78
netmap_input_error_t
Definition: node.c:33
#define always_inline
Definition: clib.h:84
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:190
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
static_always_inline void vnet_feature_device_input_redirect_x1(vlib_node_runtime_t *node, u32 sw_if_index, u32 *next0, vlib_buffer_t *b0, u16 buffer_advanced0)
Definition: feature.h:158
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u32 per_interface_next_index
Definition: netmap.h:53
u32 input_cpu_count
Definition: netmap.h:96
struct netmap_if * nifp
Definition: netmap.h:60
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:97
static char * netmap_input_error_strings[]
Definition: node.c:41
static uword format_get_indent(u8 *s)
Definition: format.h:72
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:82
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
#define PREDICT_FALSE(x)
Definition: clib.h:97
static u32 vlib_buffer_free_list_buffer_size(vlib_main_t *vm, u32 free_list_index)
Definition: buffer_funcs.h:351
#define VNET_DEVICE_INPUT_NEXT_NODES
Definition: devices.h:32
#define VLIB_FRAME_SIZE
Definition: node.h:328
uint32_t buf_idx
Definition: net_netmap.h:146
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:216
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:350
static void buffer_add_to_chain(vlib_main_t *vm, u32 bi, u32 first_bi, u32 prev_bi)
Definition: node.c:71
u16 first_rx_ring
Definition: netmap.h:63
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
int fd
Definition: netmap.h:59
#define NIOCRXSYNC
Definition: net_netmap.h:597
uint32_t head
Definition: net_netmap.h:271
#define clib_memcpy(a, b, c)
Definition: string.h:64
u32 hw_if_index
Definition: netmap.h:49
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:99
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:306
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Increment a combined counter.
Definition: counter.h:241
netmap_main_t netmap_main
Definition: netmap.h:99
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:333
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:117
u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: dpdk_buffer.c:643
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:112
template key/value backing page structure
Definition: bihash_doc.h:44
Definition: defs.h:47
u32 input_cpu_first_index
Definition: netmap.h:93
u8 is_admin_up
Definition: netmap.h:54
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
u32 ** rx_buffers
Definition: netmap.h:84
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:159
#define foreach_netmap_input_error
Definition: node.c:31
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
Definition: defs.h:46
static u8 * format_netmap_input_trace(u8 *s, va_list *args)
Definition: node.c:55