FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * af_packet.c - linux kernel packet interface
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <linux/if_packet.h>
21 
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/devices/devices.h>
27 #include <vnet/feature/feature.h>
28 
30 
31 #define foreach_af_packet_input_error
32 
33 typedef enum
34 {
35 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
37 #undef _
40 
42 #define _(n,s) s,
44 #undef _
45 };
46 
47 typedef struct
48 {
51  int block;
52  struct tpacket2_hdr tph;
54 
55 static u8 *
56 format_af_packet_input_trace (u8 * s, va_list * args)
57 {
58  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60  af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
61  uword indent = format_get_indent (s);
62 
63  s = format (s, "af_packet: hw_if_index %d next-index %d",
64  t->hw_if_index, t->next_index);
65 
66  s =
67  format (s,
68  "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
69  "\n%Usec 0x%x nsec 0x%x vlan %U"
70 #ifdef TP_STATUS_VLAN_TPID_VALID
71  " vlan_tpid %u"
72 #endif
73  ,
74  format_white_space, indent + 2,
75  format_white_space, indent + 4,
76  t->tph.tp_status,
77  t->tph.tp_len,
78  t->tph.tp_snaplen,
79  t->tph.tp_mac,
80  t->tph.tp_net,
81  format_white_space, indent + 4,
82  t->tph.tp_sec,
83  t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.tp_vlan_tci
84 #ifdef TP_STATUS_VLAN_TPID_VALID
85  , t->tph.tp_vlan_tpid
86 #endif
87  );
88  return s;
89 }
90 
91 always_inline void
92 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
93 {
94  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
95  vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
96  vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
97 
98  /* update first buffer */
100 
101  /* update previous buffer */
102  prev_b->next_buffer = bi;
103  prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
104 
105  /* update current buffer */
106  b->next_buffer = 0;
107 
108 #if DPDK > 0
109  struct rte_mbuf *mbuf = rte_mbuf_from_vlib_buffer (b);
110  struct rte_mbuf *first_mbuf = rte_mbuf_from_vlib_buffer (first_b);
111  struct rte_mbuf *prev_mbuf = rte_mbuf_from_vlib_buffer (prev_b);
112  first_mbuf->nb_segs++;
113  prev_mbuf->next = mbuf;
114  mbuf->data_len = b->current_length;
115  mbuf->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
116  mbuf->next = 0;
117 #endif
118 }
119 
122  vlib_frame_t * frame, u32 device_idx)
123 {
125  af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, device_idx);
126  struct tpacket2_hdr *tph;
128  u32 block = 0;
129  u32 rx_frame;
130  u32 n_free_bufs;
131  u32 n_rx_packets = 0;
132  u32 n_rx_bytes = 0;
133  u32 *to_next = 0;
134  u32 block_size = apif->rx_req->tp_block_size;
135  u32 frame_size = apif->rx_req->tp_frame_size;
136  u32 frame_num = apif->rx_req->tp_frame_nr;
137  u8 *block_start = apif->rx_ring + block * block_size;
138  uword n_trace = vlib_get_trace_count (vm, node);
139  u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
141  u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes;
142  int cpu_index = node->cpu_index;
143 
144  if (apif->per_interface_next_index != ~0)
145  next_index = apif->per_interface_next_index;
146 
147  n_free_bufs = vec_len (apm->rx_buffers[cpu_index]);
148  if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
149  {
150  vec_validate (apm->rx_buffers[cpu_index],
151  VLIB_FRAME_SIZE + n_free_bufs - 1);
152  n_free_bufs +=
153  vlib_buffer_alloc (vm, &apm->rx_buffers[cpu_index][n_free_bufs],
155  _vec_len (apm->rx_buffers[cpu_index]) = n_free_bufs;
156  }
157 
158  rx_frame = apif->next_rx_frame;
159  tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
160  while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
161  {
162  vlib_buffer_t *b0 = 0, *first_b0 = 0;
163  u32 next0 = next_index;
164 
165  u32 n_left_to_next;
166  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
167  while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
168  n_left_to_next)
169  {
170  u32 data_len = tph->tp_snaplen;
171  u32 offset = 0;
172  u32 bi0 = 0, first_bi0 = 0, prev_bi0;
173 
174  while (data_len)
175  {
176  /* grab free buffer */
177  u32 last_empty_buffer =
178  vec_len (apm->rx_buffers[cpu_index]) - 1;
179  prev_bi0 = bi0;
180  bi0 = apm->rx_buffers[cpu_index][last_empty_buffer];
181  b0 = vlib_get_buffer (vm, bi0);
182  _vec_len (apm->rx_buffers[cpu_index]) = last_empty_buffer;
183  n_free_bufs--;
184 
185  /* copy data */
186  u32 bytes_to_copy =
187  data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
188  b0->current_data = 0;
190  (u8 *) tph + tph->tp_mac + offset, 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] = apif->sw_if_index;
205  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
206  first_bi0 = bi0;
207  first_b0 = vlib_get_buffer (vm, first_bi0);
208  }
209  else
210  buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
211 
212  offset += bytes_to_copy;
213  data_len -= bytes_to_copy;
214  }
215  n_rx_packets++;
216  n_rx_bytes += tph->tp_snaplen;
217  to_next[0] = first_bi0;
218  to_next += 1;
219  n_left_to_next--;
220 
221  /* trace */
223  if (PREDICT_FALSE (n_trace > 0))
224  {
226  vlib_trace_buffer (vm, node, next0, first_b0, /* follow_chain */
227  0);
228  vlib_set_trace_count (vm, node, --n_trace);
229  tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
230  tr->next_index = next0;
231  tr->hw_if_index = apif->hw_if_index;
232  clib_memcpy (&tr->tph, tph, sizeof (struct tpacket2_hdr));
233  }
234 
235  /* redirect if feature path enabled */
237  &next0, b0, 0);
238 
239  /* enque and take next packet */
240  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
241  n_left_to_next, first_bi0, next0);
242 
243  /* next packet */
244  tph->tp_status = TP_STATUS_KERNEL;
245  rx_frame = (rx_frame + 1) % frame_num;
246  tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
247  }
248 
249  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
250  }
251 
252  apif->next_rx_frame = rx_frame;
253 
255  (vnet_get_main ()->interface_main.combined_sw_if_counters
257  os_get_cpu_number (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
258 
259  return n_rx_packets;
260 }
261 
262 static uword
264  vlib_frame_t * frame)
265 {
266  int i;
267  u32 n_rx_packets = 0;
268 
270 
271  /* *INDENT-OFF* */
273  ({
274  clib_bitmap_set (apm->pending_input_bitmap, i, 0);
275  n_rx_packets += af_packet_device_input_fn(vm, node, frame, i);
276  }));
277  /* *INDENT-ON* */
278 
279  return n_rx_packets;
280 }
281 
282 /* *INDENT-OFF* */
284  .function = af_packet_input_fn,
285  .name = "af-packet-input",
286  .format_trace = format_af_packet_input_trace,
287  .type = VLIB_NODE_TYPE_INPUT,
288  .state = VLIB_NODE_STATE_INTERRUPT,
289  .n_errors = AF_PACKET_INPUT_N_ERROR,
290  .error_strings = af_packet_input_error_strings,
291 
292  .n_next_nodes = VNET_DEVICE_INPUT_N_NEXT_NODES,
293  .next_nodes = VNET_DEVICE_INPUT_NEXT_NODES,
294 };
295 
297 /* *INDENT-ON* */
298 
299 
300 /*
301  * fd.io coding-style-patch-verification: ON
302  *
303  * Local Variables:
304  * eval: (c-set-style "gnu")
305  * End:
306  */
u32 ** rx_buffers
Definition: af_packet.h:49
#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
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
uword * pending_input_bitmap
Definition: af_packet.h:46
static uword af_packet_device_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u32 device_idx)
Definition: node.c:121
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:402
static uword af_packet_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:263
struct tpacket_req * rx_req
Definition: af_packet.h:25
#define foreach_af_packet_input_error
Definition: node.c:31
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
af_packet_if_t * interfaces
Definition: af_packet.h:43
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
#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
u32 next_rx_frame
Definition: af_packet.h:33
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 VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:97
#define clib_bitmap_foreach(i, ai, body)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
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
static char * af_packet_input_error_strings[]
Definition: node.c:41
#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
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
Definition: node.c:283
#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
#define clib_memcpy(a, b, c)
Definition: string.h:64
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:99
u32 per_interface_next_index
Definition: af_packet.h:36
#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
unsigned int u32
Definition: types.h:88
struct tpacket2_hdr tph
Definition: node.c:52
#define vnet_buffer(b)
Definition: buffer.h:333
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:117
af_packet_main_t af_packet_main
Definition: af_packet.h:55
af_packet_input_error_t
Definition: node.c:33
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
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
u8 * format_ethernet_vlan_tci(u8 *s, va_list *va)
Definition: format.c:73
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:159
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
static void buffer_add_to_chain(vlib_main_t *vm, u32 bi, u32 first_bi, u32 prev_bi)
Definition: node.c:92
static u8 * format_af_packet_input_trace(u8 *s, va_list *args)
Definition: node.c:56
Definition: defs.h:46