FD.io VPP  v18.04-17-g3a0d853
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 #include <vnet/ethernet/packet.h>
29 
31 
32 #define foreach_af_packet_input_error \
33  _(PARTIAL_PKT, "partial packet")
34 
35 typedef enum
36 {
37 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
39 #undef _
42 
44 #define _(n,s) s,
46 #undef _
47 };
48 
49 typedef struct
50 {
53  int block;
54  struct tpacket2_hdr tph;
56 
57 static u8 *
58 format_af_packet_input_trace (u8 * s, va_list * args)
59 {
60  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
62  af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
63  u32 indent = format_get_indent (s);
64 
65  s = format (s, "af_packet: hw_if_index %d next-index %d",
66  t->hw_if_index, t->next_index);
67 
68  s =
69  format (s,
70  "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
71  "\n%Usec 0x%x nsec 0x%x vlan %U"
72 #ifdef TP_STATUS_VLAN_TPID_VALID
73  " vlan_tpid %u"
74 #endif
75  ,
76  format_white_space, indent + 2,
77  format_white_space, indent + 4,
78  t->tph.tp_status,
79  t->tph.tp_len,
80  t->tph.tp_snaplen,
81  t->tph.tp_mac,
82  t->tph.tp_net,
83  format_white_space, indent + 4,
84  t->tph.tp_sec,
85  t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.tp_vlan_tci
86 #ifdef TP_STATUS_VLAN_TPID_VALID
87  , t->tph.tp_vlan_tpid
88 #endif
89  );
90  return s;
91 }
92 
93 always_inline void
94 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
95 {
96  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
97  vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
98  vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
99 
100  /* update first buffer */
102 
103  /* update previous buffer */
104  prev_b->next_buffer = bi;
105  prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
106 
107  /* update current buffer */
108  b->next_buffer = 0;
109 }
110 
113 {
115  if (clib_net_to_host_u16 (eth->type) == ETHERNET_TYPE_IP4)
116  {
117  ip4_header_t *ip4 =
119  b->flags |= VNET_BUFFER_F_IS_IP4;
120  if (ip4->protocol == IP_PROTOCOL_TCP)
121  {
122  b->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
123  ((tcp_header_t
124  *) (vlib_buffer_get_current (b) +
125  sizeof (ethernet_header_t) +
126  ip4_header_bytes (ip4)))->checksum = 0;
127  }
128  else if (ip4->protocol == IP_PROTOCOL_UDP)
129  {
130  b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
131  ((udp_header_t
132  *) (vlib_buffer_get_current (b) +
133  sizeof (ethernet_header_t) +
134  ip4_header_bytes (ip4)))->checksum = 0;
135  }
136  vnet_buffer (b)->l3_hdr_offset = sizeof (ethernet_header_t);
137  vnet_buffer (b)->l4_hdr_offset =
138  sizeof (ethernet_header_t) + ip4_header_bytes (ip4);
139  }
140  else if (clib_net_to_host_u16 (eth->type) == ETHERNET_TYPE_IP6)
141  {
142  ip6_header_t *ip6 =
144  b->flags |= VNET_BUFFER_F_IS_IP6;
145  u16 ip6_hdr_len = sizeof (ip6_header_t);
146  if (ip6_ext_hdr (ip6->protocol))
147  {
148  ip6_ext_header_t *p = (void *) (ip6 + 1);
149  ip6_hdr_len += ip6_ext_header_len (p);
150  while (ip6_ext_hdr (p->next_hdr))
151  {
152  ip6_hdr_len += ip6_ext_header_len (p);
153  p = ip6_ext_next_header (p);
154  }
155  }
156  if (ip6->protocol == IP_PROTOCOL_TCP)
157  {
158  b->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
159  ((tcp_header_t
160  *) (vlib_buffer_get_current (b) +
161  sizeof (ethernet_header_t) + ip6_hdr_len))->checksum = 0;
162  }
163  else if (ip6->protocol == IP_PROTOCOL_UDP)
164  {
165  b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
166  ((udp_header_t
167  *) (vlib_buffer_get_current (b) +
168  sizeof (ethernet_header_t) + ip6_hdr_len))->checksum = 0;
169  }
170  vnet_buffer (b)->l3_hdr_offset = sizeof (ethernet_header_t);
171  vnet_buffer (b)->l4_hdr_offset =
172  sizeof (ethernet_header_t) + ip6_hdr_len;
173  }
174 }
175 
178  vlib_frame_t * frame, af_packet_if_t * apif)
179 {
181  struct tpacket2_hdr *tph;
183  u32 block = 0;
184  u32 rx_frame;
185  u32 n_free_bufs;
186  u32 n_rx_packets = 0;
187  u32 n_rx_bytes = 0;
188  u32 *to_next = 0;
189  u32 block_size = apif->rx_req->tp_block_size;
190  u32 frame_size = apif->rx_req->tp_frame_size;
191  u32 frame_num = apif->rx_req->tp_frame_nr;
192  u8 *block_start = apif->rx_ring + block * block_size;
193  uword n_trace = vlib_get_trace_count (vm, node);
194  u32 thread_index = vlib_get_thread_index ();
195  u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
197  u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes;
198 
199  if (apif->per_interface_next_index != ~0)
200  next_index = apif->per_interface_next_index;
201 
202  n_free_bufs = vec_len (apm->rx_buffers[thread_index]);
203  if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
204  {
205  vec_validate (apm->rx_buffers[thread_index],
206  VLIB_FRAME_SIZE + n_free_bufs - 1);
207  n_free_bufs +=
208  vlib_buffer_alloc (vm, &apm->rx_buffers[thread_index][n_free_bufs],
210  _vec_len (apm->rx_buffers[thread_index]) = n_free_bufs;
211  }
212 
213  rx_frame = apif->next_rx_frame;
214  tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
215  while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
216  {
217  vlib_buffer_t *b0 = 0, *first_b0 = 0;
218  u32 next0 = next_index;
219 
220  u32 n_left_to_next;
221  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
222  while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
223  n_left_to_next)
224  {
225  u32 data_len = tph->tp_snaplen;
226  u32 offset = 0;
227  u32 bi0 = 0, first_bi0 = 0, prev_bi0;
228 
229  while (data_len)
230  {
231  /* grab free buffer */
232  u32 last_empty_buffer =
233  vec_len (apm->rx_buffers[thread_index]) - 1;
234  prev_bi0 = bi0;
235  bi0 = apm->rx_buffers[thread_index][last_empty_buffer];
236  b0 = vlib_get_buffer (vm, bi0);
237  _vec_len (apm->rx_buffers[thread_index]) = last_empty_buffer;
238  n_free_bufs--;
239 
240  /* copy data */
241  u32 bytes_to_copy =
242  data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
243  u32 vlan_len = 0;
244  u32 bytes_copied = 0;
245  b0->current_data = 0;
246  /* Kernel removes VLAN headers, so reconstruct VLAN */
247  if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID))
248  {
249  if (PREDICT_TRUE (offset == 0))
250  {
252  (u8 *) tph + tph->tp_mac,
253  sizeof (ethernet_header_t));
255  ethernet_vlan_header_t *vlan =
256  (ethernet_vlan_header_t *) (eth + 1);
257  vlan->priority_cfi_and_id =
258  clib_host_to_net_u16 (tph->tp_vlan_tci);
259  vlan->type = eth->type;
260  eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
261  vlan_len = sizeof (ethernet_vlan_header_t);
262  bytes_copied = sizeof (ethernet_header_t);
263  }
264  }
266  bytes_copied + vlan_len,
267  (u8 *) tph + tph->tp_mac + offset + bytes_copied,
268  (bytes_to_copy - bytes_copied));
269 
270  /* fill buffer header */
271  b0->current_length = bytes_to_copy + vlan_len;
272 
273  if (offset == 0)
274  {
276  b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
277  vnet_buffer (b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
278  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
279  first_bi0 = bi0;
280  first_b0 = vlib_get_buffer (vm, first_bi0);
281  if (tph->tp_status & TP_STATUS_CSUMNOTREADY)
282  mark_tcp_udp_cksum_calc (first_b0);
283  }
284  else
285  buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
286 
287  offset += bytes_to_copy;
288  data_len -= bytes_to_copy;
289  }
290  n_rx_packets++;
291  n_rx_bytes += tph->tp_snaplen;
292  to_next[0] = first_bi0;
293  to_next += 1;
294  n_left_to_next--;
295 
296  /* drop partial packets */
297  if (PREDICT_FALSE (tph->tp_len != tph->tp_snaplen))
298  {
300  first_b0->error =
301  node->errors[AF_PACKET_INPUT_ERROR_PARTIAL_PKT];
302  }
303  else if (apif->per_interface_next_index == ~0)
304  {
305  /* redirect if feature path enabled */
307  first_b0);
308  }
309 
310  /* trace */
312  if (PREDICT_FALSE (n_trace > 0))
313  {
315  vlib_trace_buffer (vm, node, next0, first_b0, /* follow_chain */
316  0);
317  vlib_set_trace_count (vm, node, --n_trace);
318  tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
319  tr->next_index = next0;
320  tr->hw_if_index = apif->hw_if_index;
321  clib_memcpy (&tr->tph, tph, sizeof (struct tpacket2_hdr));
322  }
323 
324  /* enque and take next packet */
325  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
326  n_left_to_next, first_bi0, next0);
327 
328  /* next packet */
329  tph->tp_status = TP_STATUS_KERNEL;
330  rx_frame = (rx_frame + 1) % frame_num;
331  tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
332  }
333 
334  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
335  }
336 
337  apif->next_rx_frame = rx_frame;
338 
340  (vnet_get_main ()->interface_main.combined_sw_if_counters
342  vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
343 
344  vnet_device_increment_rx_packets (thread_index, n_rx_packets);
345  return n_rx_packets;
346 }
347 
348 static uword
350  vlib_frame_t * frame)
351 {
352  u32 n_rx_packets = 0;
354  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
356 
358  {
359  af_packet_if_t *apif;
360  apif = vec_elt_at_index (apm->interfaces, dq->dev_instance);
361  if (apif->is_admin_up)
362  n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif);
363  }
364 
365  return n_rx_packets;
366 }
367 
368 /* *INDENT-OFF* */
370  .function = af_packet_input_fn,
371  .name = "af-packet-input",
372  .sibling_of = "device-input",
373  .format_trace = format_af_packet_input_trace,
374  .type = VLIB_NODE_TYPE_INPUT,
375  .state = VLIB_NODE_STATE_INTERRUPT,
376  .n_errors = AF_PACKET_INPUT_N_ERROR,
377  .error_strings = af_packet_input_error_strings,
378 };
379 
381 /* *INDENT-ON* */
382 
383 
384 /*
385  * fd.io coding-style-patch-verification: ON
386  *
387  * Local Variables:
388  * eval: (c-set-style "gnu")
389  * End:
390  */
u32 ** rx_buffers
Definition: af_packet.h:53
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:434
static void vnet_device_increment_rx_packets(u32 thread_index, u64 count)
Definition: devices.h:110
vnet_device_and_queue_t * devices_and_queues
Definition: devices.h:69
#define CLIB_UNUSED(x)
Definition: clib.h:79
static_always_inline void mark_tcp_udp_cksum_calc(vlib_buffer_t *b)
Definition: node.c:112
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:143
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:211
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:464
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
#define PREDICT_TRUE(x)
Definition: clib.h:106
static int ip4_header_bytes(ip4_header_t *i)
Definition: ip4_packet.h:227
static void * ip6_ext_next_header(ip6_ext_header_t *ext_hdr)
Definition: ip6_packet.h:489
af_packet_input_error_t
Definition: node.c:35
static u32 format_get_indent(u8 *s)
Definition: format.h:72
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
struct tpacket_req * rx_req
Definition: af_packet.h:29
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
Definition: node.c:369
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:415
struct _tcp_header tcp_header_t
static uword af_packet_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:349
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:47
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:104
#define static_always_inline
Definition: clib.h:93
#define always_inline
Definition: clib.h:92
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
u32 next_rx_frame
Definition: af_packet.h:37
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define VLIB_FRAME_SIZE
Definition: node.h:328
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:108
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:209
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:446
#define PREDICT_FALSE(x)
Definition: clib.h:105
#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:218
#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:364
static char * af_packet_input_error_strings[]
Definition: node.c:43
#define ip6_ext_header_len(p)
Definition: ip6_packet.h:485
#define foreach_af_packet_input_error
Definition: node.c:32
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
static u8 * format_af_packet_input_trace(u8 *s, va_list *args)
Definition: node.c:58
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:221
vlib_main_t * vm
Definition: buffer.c:294
#define clib_memcpy(a, b, c)
Definition: string.h:75
u32 per_interface_next_index
Definition: af_packet.h:40
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:454
static u8 ip6_ext_hdr(u8 nexthdr)
Definition: ip6_packet.h:473
af_packet_main_t af_packet_main
Definition: af_packet.c:35
unsigned int u32
Definition: types.h:88
struct tpacket2_hdr tph
Definition: node.c:54
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:126
static void buffer_add_to_chain(vlib_main_t *vm, u32 bi, u32 first_bi, u32 prev_bi)
Definition: node.c:94
static uword af_packet_device_input_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, af_packet_if_t *apif)
Definition: node.c:177
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:159
template key/value backing page structure
Definition: bihash_doc.h:44
#define foreach_device_and_queue(var, vec)
Definition: devices.h:156
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
#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_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:553
static u32 vlib_buffer_free_list_buffer_size(vlib_main_t *vm, vlib_buffer_free_list_index_t index)
Definition: buffer_funcs.h:533
#define vnet_buffer(b)
Definition: buffer.h:372
static_always_inline void vnet_feature_start_device_input_x1(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:234
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_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:111
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:347
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
VLIB_NODE_FUNCTION_MULTIARCH(ethernet_input_not_l2_node, ethernet_input_not_l2)
Definition: node.c:1207
Definition: defs.h:46