FD.io VPP  v16.09
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 
28 
29 #define foreach_af_packet_input_error
30 
31 typedef enum
32 {
33 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
35 #undef _
38 
40 #define _(n,s) s,
42 #undef _
43 };
44 
45 enum
46 {
50 };
51 
52 typedef struct
53 {
56  int block;
57  struct tpacket2_hdr tph;
59 
60 static u8 *
61 format_af_packet_input_trace (u8 * s, va_list * args)
62 {
63  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65  af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
66  uword indent = format_get_indent (s);
67 
68  s = format (s, "af_packet: hw_if_index %d next-index %d",
69  t->hw_if_index, t->next_index);
70 
71  s =
72  format (s,
73  "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
74  "\n%Usec 0x%x nsec 0x%x vlan %U"
75 #ifdef TP_STATUS_VLAN_TPID_VALID
76  " vlan_tpid %u"
77 #endif
78  ,
79  format_white_space, indent + 2,
80  format_white_space, indent + 4,
81  t->tph.tp_status,
82  t->tph.tp_len,
83  t->tph.tp_snaplen,
84  t->tph.tp_mac,
85  t->tph.tp_net,
86  format_white_space, indent + 4,
87  t->tph.tp_sec,
88  t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.tp_vlan_tci
89 #ifdef TP_STATUS_VLAN_TPID_VALID
90  , t->tph.tp_vlan_tpid
91 #endif
92  );
93  return s;
94 }
95 
96 always_inline void
97 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
98 {
99  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
100  vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
101  vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
102 
103  /* update first buffer */
105 
106  /* update previous buffer */
107  prev_b->next_buffer = bi;
108  prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
109 
110  /* update current buffer */
111  b->next_buffer = 0;
112 
113 #if DPDK > 0
114  struct rte_mbuf *mbuf = rte_mbuf_from_vlib_buffer (b);
115  struct rte_mbuf *first_mbuf = rte_mbuf_from_vlib_buffer (first_b);
116  struct rte_mbuf *prev_mbuf = rte_mbuf_from_vlib_buffer (prev_b);
117  first_mbuf->nb_segs++;
118  prev_mbuf->next = mbuf;
119  mbuf->data_len = b->current_length;
120  mbuf->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
121  mbuf->next = 0;
122 #endif
123 }
124 
127  vlib_frame_t * frame, u32 device_idx)
128 {
130  af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, device_idx);
131  struct tpacket2_hdr *tph;
133  u32 block = 0;
134  u32 rx_frame;
135  u32 n_free_bufs;
136  u32 n_rx_packets = 0;
137  u32 n_rx_bytes = 0;
138  u32 *to_next = 0;
139  u32 block_size = apif->rx_req->tp_block_size;
140  u32 frame_size = apif->rx_req->tp_frame_size;
141  u32 frame_num = apif->rx_req->tp_frame_nr;
142  u8 *block_start = apif->rx_ring + block * block_size;
143  uword n_trace = vlib_get_trace_count (vm, node);
144  u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
146  u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes;
147 
148  if (apif->per_interface_next_index != ~0)
149  next_index = apif->per_interface_next_index;
150 
151  n_free_bufs = vec_len (apm->rx_buffers);
152  if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
153  {
154  vec_validate (apm->rx_buffers, VLIB_FRAME_SIZE + n_free_bufs - 1);
155  n_free_bufs +=
156  vlib_buffer_alloc (vm, &apm->rx_buffers[n_free_bufs],
158  _vec_len (apm->rx_buffers) = n_free_bufs;
159  }
160 
161  rx_frame = apif->next_rx_frame;
162  tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
163  while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
164  {
165  vlib_buffer_t *b0, *first_b0 = 0;
166  u32 next0 = next_index;
167 
168  u32 n_left_to_next;
169  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
170  while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
171  n_left_to_next)
172  {
173  u32 data_len = tph->tp_snaplen;
174  u32 offset = 0;
175  u32 bi0 = 0, first_bi0 = 0, prev_bi0;
176 
177  while (data_len)
178  {
179  /* grab free buffer */
180  u32 last_empty_buffer = vec_len (apm->rx_buffers) - 1;
181  prev_bi0 = bi0;
182  bi0 = apm->rx_buffers[last_empty_buffer];
183  b0 = vlib_get_buffer (vm, bi0);
184  _vec_len (apm->rx_buffers) = last_empty_buffer;
185  n_free_bufs--;
186 
187  /* copy data */
188  u32 bytes_to_copy =
189  data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
190  b0->current_data = 0;
192  (u8 *) tph + tph->tp_mac + offset, bytes_to_copy);
193 
194  /* fill buffer header */
195  b0->current_length = bytes_to_copy;
196 
197  if (offset == 0)
198  {
199 #if DPDK > 0
200  struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer (b0);
201  rte_pktmbuf_data_len (mb) = b0->current_length;
202  rte_pktmbuf_pkt_len (mb) = b0->current_length;
203 #endif
206  vnet_buffer (b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
207  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
208  first_bi0 = bi0;
209  first_b0 = vlib_get_buffer (vm, first_bi0);
210  }
211  else
212  buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
213 
214  offset += bytes_to_copy;
215  data_len -= bytes_to_copy;
216  }
217  n_rx_packets++;
218  n_rx_bytes += tph->tp_snaplen;
219  to_next[0] = first_bi0;
220  to_next += 1;
221  n_left_to_next--;
222 
223  /* trace */
225  if (PREDICT_FALSE (n_trace > 0))
226  {
228  vlib_trace_buffer (vm, node, next0, first_b0, /* follow_chain */
229  0);
230  vlib_set_trace_count (vm, node, --n_trace);
231  tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
232  tr->next_index = next0;
233  tr->hw_if_index = apif->hw_if_index;
234  clib_memcpy (&tr->tph, tph, sizeof (struct tpacket2_hdr));
235  }
236  /* enque and take next packet */
237  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
238  n_left_to_next, first_bi0, next0);
239 
240  /* next packet */
241  tph->tp_status = TP_STATUS_KERNEL;
242  rx_frame = (rx_frame + 1) % frame_num;
243  tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
244  }
245 
246  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
247  }
248 
249  apif->next_rx_frame = rx_frame;
250 
252  (vnet_get_main ()->interface_main.combined_sw_if_counters
254  os_get_cpu_number (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
255 
256  return n_rx_packets;
257 }
258 
259 static uword
261  vlib_frame_t * frame)
262 {
263  int i;
264  u32 n_rx_packets = 0;
265 
267 
268  /* *INDENT-OFF* */
270  ({
271  clib_bitmap_set (apm->pending_input_bitmap, i, 0);
272  n_rx_packets += af_packet_device_input_fn(vm, node, frame, i);
273  }));
274  /* *INDENT-ON* */
275 
276  return n_rx_packets;
277 }
278 
279 /* *INDENT-OFF* */
281  .function = af_packet_input_fn,
282  .name = "af-packet-input",
283  .format_trace = format_af_packet_input_trace,
284  .type = VLIB_NODE_TYPE_INPUT,
285  .state = VLIB_NODE_STATE_INTERRUPT,
286  .n_errors = AF_PACKET_INPUT_N_ERROR,
287  .error_strings = af_packet_input_error_strings,
288 
289  .n_next_nodes = AF_PACKET_INPUT_N_NEXT,
290  .next_nodes = {
291  [AF_PACKET_INPUT_NEXT_DROP] = "error-drop",
292  [AF_PACKET_INPUT_NEXT_ETHERNET_INPUT] = "ethernet-input",
293  },
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  */
#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:1056
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:382
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:126
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:399
static uword af_packet_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:260
struct tpacket_req * rx_req
Definition: af_packet.h:25
#define foreach_af_packet_input_error
Definition: node.c:29
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:45
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:187
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
u32 next_rx_frame
Definition: af_packet.h:33
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:95
#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:39
#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 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:280
#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:130
#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:348
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:97
u32 per_interface_next_index
Definition: af_packet.h:36
u32 * rx_buffers
Definition: af_packet.h:49
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:303
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:57
#define vnet_buffer(b)
Definition: buffer.h:335
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:114
af_packet_main_t af_packet_main
Definition: af_packet.h:55
af_packet_input_error_t
Definition: node.c:31
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:109
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
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:97
static u8 * format_af_packet_input_trace(u8 *s, va_list *args)
Definition: node.c:61
Definition: defs.h:46