FD.io VPP  v16.06
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 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
34 #undef _
37 
38 static char * af_packet_input_error_strings[] = {
39 #define _(n,s) s,
41 #undef _
42 };
43 
44 enum {
48 };
49 
50 typedef struct {
53  int block;
54  struct tpacket2_hdr tph;
56 
57 static u8 * format_af_packet_input_trace (u8 * s, va_list * args)
58 {
59  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61  af_packet_input_trace_t * t = va_arg (*args, af_packet_input_trace_t *);
62  uword indent = format_get_indent (s);
63 
64  s = format (s, "af_packet: hw_if_index %d next-index %d",
65  t->hw_if_index, t->next_index);
66 
67  s = format (s, "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
68  "\n%Usec 0x%x nsec 0x%x vlan_tci %u"
69 #ifdef TP_STATUS_VLAN_TPID_VALID
70  " vlan_tpid %u"
71 #endif
72  ,
73  format_white_space, indent + 2,
74  format_white_space, indent + 4,
75  t->tph.tp_status,
76  t->tph.tp_len,
77  t->tph.tp_snaplen,
78  t->tph.tp_mac,
79  t->tph.tp_net,
80  format_white_space, indent + 4,
81  t->tph.tp_sec,
82  t->tph.tp_nsec,
83  t->tph.tp_vlan_tci,
84 #ifdef TP_STATUS_VLAN_TPID_VALID
85  t->tph.tp_vlan_tpid,
86 #endif
87  t->tph.tp_net);
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 
143  if (apif->per_interface_next_index != ~0)
144  next_index = apif->per_interface_next_index;
145 
146  n_free_bufs = vec_len (apm->rx_buffers);
147  if (PREDICT_FALSE(n_free_bufs < VLIB_FRAME_SIZE))
148  {
149  vec_validate(apm->rx_buffers, VLIB_FRAME_SIZE + n_free_bufs - 1);
150  n_free_bufs += vlib_buffer_alloc(vm, &apm->rx_buffers[n_free_bufs], VLIB_FRAME_SIZE);
151  _vec_len (apm->rx_buffers) = n_free_bufs;
152  }
153 
154  rx_frame = apif->next_rx_frame;
155  tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
156  while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
157  {
158  vlib_buffer_t * b0, * first_b0 = 0;
159  u32 next0 = next_index;
160 
161  u32 n_left_to_next;
162  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
163  while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
164  n_left_to_next)
165  {
166  u32 data_len = tph->tp_snaplen;
167  u32 offset = 0;
168  u32 bi0 = 0, first_bi0 = 0, prev_bi0;
169 
170  while (data_len)
171  {
172  /* grab free buffer */
173  u32 last_empty_buffer = vec_len (apm->rx_buffers) - 1;
174  prev_bi0 = bi0;
175  bi0 = apm->rx_buffers[last_empty_buffer];
176  b0 = vlib_get_buffer (vm, bi0);
177  _vec_len (apm->rx_buffers) = last_empty_buffer;
178  n_free_bufs--;
179 
180  /* copy data */
181  u32 bytes_to_copy = data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
182  b0->current_data = 0;
183  clib_memcpy (vlib_buffer_get_current (b0), (u8 *) tph + tph->tp_mac + offset, bytes_to_copy);
184 
185  /* fill buffer header */
186  b0->clone_count = 0;
187  b0->current_length = bytes_to_copy;
188 
189  if (offset == 0)
190  {
191 #if DPDK > 0
192  struct rte_mbuf * mb = rte_mbuf_from_vlib_buffer(b0);
193  rte_pktmbuf_data_len (mb) = b0->current_length;
194  rte_pktmbuf_pkt_len (mb) = b0->current_length;
195 #endif
198  vnet_buffer(b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
199  vnet_buffer(b0)->sw_if_index[VLIB_TX] = (u32)~0;
200  first_bi0 = bi0;
201  first_b0 = vlib_get_buffer(vm, first_bi0);
202  }
203  else
204  buffer_add_to_chain(vm, bi0, first_bi0, prev_bi0);
205 
206  offset += bytes_to_copy;
207  data_len -= bytes_to_copy;
208  }
209  n_rx_packets++;
210  n_rx_bytes += tph->tp_snaplen;
211  to_next[0] = first_bi0;
212  to_next += 1;
213  n_left_to_next--;
214 
215  /* trace */
217  if (PREDICT_FALSE(n_trace > 0))
218  {
220  vlib_trace_buffer (vm, node, next0, first_b0, /* follow_chain */ 0);
221  vlib_set_trace_count (vm, node, --n_trace);
222  tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
223  tr->next_index = next0;
224  tr->hw_if_index = apif->hw_if_index;
225  clib_memcpy(&tr->tph, tph, sizeof(struct tpacket2_hdr));
226  }
227  /* enque and take next packet */
228  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
229  n_left_to_next, first_bi0, next0);
230 
231  /* next packet */
232  tph->tp_status = TP_STATUS_KERNEL;
233  rx_frame = (rx_frame + 1) % frame_num;
234  tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
235  }
236 
237  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
238  }
239 
240  apif->next_rx_frame = rx_frame;
241 
243  (vnet_get_main()->interface_main.combined_sw_if_counters
246  apif->hw_if_index,
247  n_rx_packets, n_rx_bytes);
248 
249  return n_rx_packets;
250 }
251 
252 static uword
254  vlib_frame_t * frame)
255 {
256  int i;
257  u32 n_rx_packets = 0;
258 
260 
262  ({
263  clib_bitmap_set (apm->pending_input_bitmap, i, 0);
264  n_rx_packets += af_packet_device_input_fn(vm, node, frame, i);
265  }));
266 
267  return n_rx_packets;
268 }
269 
270 
272  .function = af_packet_input_fn,
273  .name = "af-packet-input",
274  .format_trace = format_af_packet_input_trace,
275  .type = VLIB_NODE_TYPE_INPUT,
276  .state = VLIB_NODE_STATE_INTERRUPT,
277  .n_errors = AF_PACKET_INPUT_N_ERROR,
278  .error_strings = af_packet_input_error_strings,
279 
280  .n_next_nodes = AF_PACKET_INPUT_N_NEXT,
281  .next_nodes = {
282  [AF_PACKET_INPUT_NEXT_DROP] = "error-drop",
283  [AF_PACKET_INPUT_NEXT_ETHERNET_INPUT] = "ethernet-input",
284  },
285 };
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Definition: main.c:459
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
#define CLIB_UNUSED(x)
Definition: clib.h:79
uword * pending_input_bitmap
Definition: af_packet.h:44
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:383
static uword af_packet_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:253
always_inline void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:160
struct tpacket_req * rx_req
Definition: af_packet.h:24
#define foreach_af_packet_input_error
Definition: node.c:29
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
always_inline u32 vlib_buffer_free_list_buffer_size(vlib_main_t *vm, u32 free_list_index)
Definition: buffer_funcs.h:346
af_packet_if_t * interfaces
Definition: af_packet.h:41
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:77
always_inline u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:144
#define always_inline
Definition: clib.h:84
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:107
u32 next_rx_frame
Definition: af_packet.h:32
always_inline void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Definition: counter.h:210
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:93
always_inline 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 clib_bitmap_foreach(i, ai, body)
Definition: bitmap.h:308
#define pool_elt_at_index(p, i)
Definition: pool.h:346
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:81
uword os_get_cpu_number(void)
Definition: unix-misc.c:206
static char * af_packet_input_error_strings[]
Definition: node.c:38
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define VLIB_FRAME_SIZE
Definition: node.h:292
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
Definition: node.c:271
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Definition: buffer_node.h:83
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:95
u32 per_interface_next_index
Definition: af_packet.h:35
u32 * rx_buffers
Definition: af_packet.h:47
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:296
unsigned int u32
Definition: types.h:88
struct tpacket2_hdr tph
Definition: node.c:54
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
always_inline void buffer_add_to_chain(vlib_main_t *vm, u32 bi, u32 first_bi, u32 prev_bi)
Definition: node.c:92
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:112
af_packet_main_t af_packet_main
Definition: af_packet.h:53
u32 clone_count
Specifies whether this buffer should be reinitialized when freed.
Definition: buffer.h:121
u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer.c:770
af_packet_input_error_t
Definition: node.c:31
always_inline uword format_get_indent(u8 *s)
Definition: format.h:72
u64 uword
Definition: types.h:112
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:106
Definition: defs.h:46
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
always_inline 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
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline 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:106
always_inline 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 u8 * format_af_packet_input_trace(u8 *s, va_list *args)
Definition: node.c:57
Definition: defs.h:45