FD.io VPP  v17.04-9-g99c0734
Vector Packet Processing
device.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_tx_func_error \
30 _(FRAME_NOT_READY, "tx frame not ready") \
31 _(TXRING_EAGAIN, "tx sendto temporary failure") \
32 _(TXRING_FATAL, "tx sendto fatal failure") \
33 _(TXRING_OVERRUN, "tx ring overrun")
34 
35 typedef enum
36 {
37 #define _(f,s) AF_PACKET_TX_ERROR_##f,
39 #undef _
42 
44 #define _(n,s) s,
46 #undef _
47 };
48 
49 
50 static u8 *
51 format_af_packet_device_name (u8 * s, va_list * args)
52 {
53  u32 i = va_arg (*args, u32);
56 
57  s = format (s, "host-%s", apif->host_if_name);
58  return s;
59 }
60 
61 static u8 *
62 format_af_packet_device (u8 * s, va_list * args)
63 {
64  s = format (s, "Linux PACKET socket interface");
65  return s;
66 }
67 
68 static u8 *
69 format_af_packet_tx_trace (u8 * s, va_list * args)
70 {
71  s = format (s, "Unimplemented...");
72  return s;
73 }
74 
75 static uword
77  vlib_node_runtime_t * node, vlib_frame_t * frame)
78 {
80  u32 *buffers = vlib_frame_args (frame);
81  u32 n_left = frame->n_vectors;
82  u32 n_sent = 0;
83  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
84  af_packet_if_t *apif =
86  int block = 0;
87  u32 block_size = apif->tx_req->tp_block_size;
88  u32 frame_size = apif->tx_req->tp_frame_size;
89  u32 frame_num = apif->tx_req->tp_frame_nr;
90  u8 *block_start = apif->tx_ring + block * block_size;
91  u32 tx_frame = apif->next_tx_frame;
92  struct tpacket2_hdr *tph;
93  u32 frame_not_ready = 0;
94 
95  if (PREDICT_FALSE (apif->lockp != 0))
96  {
97  while (__sync_lock_test_and_set (apif->lockp, 1))
98  ;
99  }
100 
101  while (n_left > 0)
102  {
103  u32 len;
104  u32 offset = 0;
105  vlib_buffer_t *b0;
106  n_left--;
107  u32 bi = buffers[0];
108  buffers++;
109 
110  tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
111 
112  if (PREDICT_FALSE
113  (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
114  {
115  frame_not_ready++;
116  goto next;
117  }
118 
119  do
120  {
121  b0 = vlib_get_buffer (vm, bi);
122  len = b0->current_length;
123  clib_memcpy ((u8 *) tph +
124  TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + offset,
125  vlib_buffer_get_current (b0), len);
126  offset += len;
127  }
128  while ((bi =
129  (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
130 
131  tph->tp_len = tph->tp_snaplen = offset;
132  tph->tp_status = TP_STATUS_SEND_REQUEST;
133  n_sent++;
134  next:
135  /* check if we've exhausted the ring */
136  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
137  break;
138 
139  tx_frame = (tx_frame + 1) % frame_num;
140  }
141 
143 
144  if (PREDICT_TRUE (n_sent))
145  {
146  apif->next_tx_frame = tx_frame;
147 
148  if (PREDICT_FALSE (sendto (apif->fd, NULL, 0,
149  MSG_DONTWAIT, NULL, 0) == -1))
150  {
151  /* Uh-oh, drop & move on, but count whether it was fatal or not.
152  * Note that we have no reliable way to properly determine the
153  * disposition of the packets we just enqueued for delivery.
154  */
155  vlib_error_count (vm, node->node_index,
156  unix_error_is_fatal (errno) ?
157  AF_PACKET_TX_ERROR_TXRING_FATAL :
158  AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent);
159  }
160  }
161 
162  if (PREDICT_FALSE (apif->lockp != 0))
163  *apif->lockp = 0;
164 
165  if (PREDICT_FALSE (frame_not_ready))
166  vlib_error_count (vm, node->node_index,
167  AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
168 
169  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
170  vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
171  n_left);
172 
173  vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
174  return frame->n_vectors;
175 }
176 
177 static void
179  u32 node_index)
180 {
182  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
183  af_packet_if_t *apif =
185 
186  /* Shut off redirection */
187  if (node_index == ~0)
188  {
189  apif->per_interface_next_index = node_index;
190  return;
191  }
192 
195  node_index);
196 }
197 
198 static void
200 {
201  /* Nothing for now */
202 }
203 
204 static clib_error_t *
206  u32 flags)
207 {
209  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
210  af_packet_if_t *apif =
212  u32 hw_flags;
213 
214  apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
215 
216  if (apif->is_admin_up)
218  else
219  hw_flags = 0;
220 
221  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
222 
223  return 0;
224 }
225 
226 static clib_error_t *
228  u32 hw_if_index,
229  struct vnet_sw_interface_t *st, int is_add)
230 {
231  /* Nothing for now */
232  return 0;
233 }
234 
235 /* *INDENT-OFF* */
237  .name = "af-packet",
238  .tx_function = af_packet_interface_tx,
239  .format_device_name = format_af_packet_device_name,
240  .format_device = format_af_packet_device,
241  .format_tx_trace = format_af_packet_tx_trace,
242  .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
243  .tx_function_error_strings = af_packet_tx_func_error_strings,
244  .rx_redirect_to_node = af_packet_set_interface_next_node,
245  .clear_counters = af_packet_clear_hw_interface_counters,
246  .admin_up_down_function = af_packet_interface_admin_up_down,
247  .subif_add_del_function = af_packet_subif_add_del_function,
248 };
249 
252 /* *INDENT-ON* */
253 
254 /*
255  * fd.io coding-style-patch-verification: ON
256  *
257  * Local Variables:
258  * eval: (c-set-style "gnu")
259  * End:
260  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:530
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:290
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:463
#define PREDICT_TRUE(x)
Definition: clib.h:98
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
#define NULL
Definition: clib.h:55
static u8 * format_af_packet_tx_trace(u8 *s, va_list *args)
Definition: device.c:69
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
static void af_packet_set_interface_next_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: device.c:178
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:379
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1062
VNET_DEVICE_CLASS(af_packet_device_class)
u8 * host_if_name
Definition: af_packet.h:24
af_packet_if_t * interfaces
Definition: af_packet.h:44
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:87
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:71
static void af_packet_clear_hw_interface_counters(u32 instance)
Definition: device.c:199
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:188
#define PREDICT_FALSE(x)
Definition: clib.h:97
static char * af_packet_tx_func_error_strings[]
Definition: device.c:43
static u8 * format_af_packet_device_name(u8 *s, va_list *args)
Definition: device.c:51
u32 node_index
Node index.
Definition: node.h:436
u16 n_vectors
Definition: node.h:344
vlib_main_t * vm
Definition: buffer.c:276
static clib_error_t * af_packet_subif_add_del_function(vnet_main_t *vnm, u32 hw_if_index, struct vnet_sw_interface_t *st, int is_add)
Definition: device.c:227
#define clib_memcpy(a, b, c)
Definition: string.h:69
u32 per_interface_next_index
Definition: af_packet.h:37
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
Definition: node.c:266
#define foreach_af_packet_tx_func_error
Definition: device.c:29
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:536
unsigned int u32
Definition: types.h:88
static word unix_error_is_fatal(word error)
Definition: error.h:130
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:109
af_packet_main_t af_packet_main
Definition: af_packet.h:56
static void * vlib_frame_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:270
af_packet_tx_func_error_t
Definition: device.c:35
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vnet_device_class_t af_packet_device_class
u64 uword
Definition: types.h:112
template key/value backing page structure
Definition: bihash_doc.h:44
unsigned char u8
Definition: types.h:56
static clib_error_t * af_packet_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: device.c:205
struct clib_bihash_value offset
template key/value backing page structure
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:101
static uword af_packet_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: device.c:76
u32 flags
Definition: vhost-user.h:78
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:74
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
static u8 * format_af_packet_device(u8 *s, va_list *args)
Definition: device.c:62
#define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)
Definition: interface.h:217